-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaxviz.js
142 lines (126 loc) · 3.84 KB
/
taxviz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var http=require("http");
var url=require("url");
var fs=require("fs");
var csv = require("fast-csv");
var allrecords=[];
var show_debug=false;
var webdir=process.cwd()+"/web/";
var webport = Number(process.env.PORT || 8081);
// set up web server
var webserver = http.createServer(webrequest);
webserver.on("error", function() {
// problem with the web server ... quite possibly another process already is listening on that port
console.log('cannot start server, aborting');
process.exit(1);
});
// load the layout file and insert into db
if (show_debug) console.log("Loading CSV data ... ")
csv(webdir+"labels_animalia.csv",{headers:true})
.on("data", function(data){
// convert from string to number as needed
data.x=Number(data.x);
data.y=Number(data.y);
data.zmin=Number(data.zmin);
data.zmax=Number(data.zmax);
data.red=Number(data.red);
data.green=Number(data.green);
data.blue=Number(data.blue);
data.size=Number(data.size);
allrecords.push(data);
})
.on("end", function(){
if (show_debug) console.log("done");
// start the web server now that we have the data loaded
webserver.listen(webport, function() {
if (show_debug) console.log("Server started on port " + webport);
});
})
.parse();
function webrequest(req,res) {
try {
dispatch(req,res);
} catch (err) {
console.log("Web error: " + err);
res.writeHead(500);
res.end('Internal Server Error');
}
}
dispatch = function(req, res) {
var serverError = function(code, content) {
res.writeHead(code, {'Content-Type': 'text/plain'});
res.end(content);
}
var renderHtml = function(content,fileext) {
var result;
if (fileext=="html") {
content=content.toString();
}
// specify appropriate mime-type and encoding, and deliver content
if (fileext=='png') {
res.writeHead(200, {'Content-Type': 'image/png'});
res.end(content);
} else if (fileext=='gif') {
res.writeHead(200, {'Content-Type': 'image/gif'});
res.end(content);
} else if (fileext=='jpg') {
res.writeHead(200, {'Content-Type': 'image/jpeg'});
res.end(content);
} else if (fileext=='js') {
res.writeHead(200, {'Content-Type': 'application/x-javascript'});
res.end(content, 'utf-8');
} else if (fileext=='css') {
res.writeHead(200, {'Content-Type': 'text/css'});
res.end(content, 'utf-8');
} else if (fileext=='json') {
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(content));
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(content, 'utf-8');
}
}
var url_parts=url.parse(req.url, true);
var find_all=function(query) {
console.log("query:");
console.dir(query);
var this_records=[];
for (var i=0; i<allrecords.length; i++) {
var doc=allrecords[i];
if (doc.zmin<=query.z && doc.zmax>=query.z && doc.x>=query.xmin && doc.x<=query.xmax && doc.y>=query.ymin && doc.y<=query.ymax) {
this_records.push(doc);
}
}
outstr=JSON.stringify(this_records);
renderHtml(outstr,"json");
}
// does action match a defined action?
if (url_parts.pathname=="/labels") {
// extract labels from allrecords
var query=url_parts.query;
query.xmin=Number(query.xmin);
query.xmax=Number(query.xmax);
query.ymin=Number(query.ymin);
query.ymax=Number(query.ymax);
query.z=Number(query.z);
find_all(query);
} else {
// deliver file content
var parts = req.url.split('/');
var filename=webdir + parts.slice(1).join("/");
if (req.url == "/") {
filename=webdir + "index.html";
}
if (! fs.existsSync(filename)) {
serverError(404, '404 Bad Request for ' + filename);
} else {
fs.readFile(filename, function(error, content) {
if (error) {
serverError(500);
} else {
var fileext=filename.split(".").slice(-1);
renderHtml(content, fileext);
}
});
}
}
}