-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.js
executable file
·265 lines (239 loc) · 8.23 KB
/
server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
// vector tile server
var fs = require('fs'); // todo : remove
var mapnik = require('mapnik');
var mercator = new(require('sphericalmercator'));
var pool = require('generic-pool').Pool;
var http = require('http');
var url = require("url");
var vector_ren = require('./lib/vector_server');
var eio = require('eio');
var path = require('path');
var exists = require('fs').exists || require('path').exists;
var mime = require('mime');
var mkdirp = require('mkdirp');
// Increase number of threads to 1.5x the number of logical CPUs.
var threads = Math.ceil(Math.max(4, require('os').cpus().length * 1.5));
eio.setMinParallel(threads);
// should we default to chunked encoding?
var chunked = false;
// should osmtiles be cached on demand?
var cache = false;
// should we print client details?
var debug = false;
var root = "./www";
var port = '8000';
var stylesheet = 'osm_vectors.xml';
var usage = 'usage: node server.js [stylesheet] [port] [root]';
usage += '\n default stylesheet: ' + stylesheet;
usage += '\n default port: ' + port;
usage += '\n default root directory: ' + root;
if (process.argv.indexOf('-h') > -1 || process.argv.indexOf('--help') > -1) {
console.log(usage);
process.exit(0);
}
if (process.argv[2]) {
stylesheet = process.argv[2];
}
if (process.argv[3]) {
port = process.argv[3];
}
if (process.argv[4]) {
root = process.argv[4];
}
var map_pool = pool({
create: function(callback) {
var obj = new mapnik.Map(256, 256);
var opts = { strict: false };
try {
obj.loadSync(stylesheet, opts);
return callback(null,obj);
} catch (err) {
return callback(err);
}
/*
// async loading will trigger when calling Mapnik's datasource_cache::create
// if multiple tiles are accessed at the same time at first load
// TODO - is this a bug in Mapnik singleton impl?
obj.load(stylesheet, opts, function(err,obj) {
if (err) return callback(err);
return callback(null,obj);
});
*/
},
destroy: function(obj) {
delete obj;
},
max: threads
});
var parse_url = function(uri, callback) {
var matches = uri.match(/(\d+)/g);
if (matches && matches.length == 3) {
var format = path.extname(uri);
if (!format || !(format.indexOf('png') > -1 || format == '.osmtile')) {
var msg = "Invalid format, only 'png' and 'osmtile' are supported";
msg += ' (expected a url like /0/0/0.png or /0/0/0.osmtile';
msg += ' but got: ' + req.url + ')';
return callback(new Error(msg));
}
try {
var x = parseInt(matches[1], 10);
var y = parseInt(matches[2], 10);
var z = parseInt(matches[0], 10);
return callback(null,
{ z: z,
x: x,
y: y,
format: format.slice(1)
});
} catch (err) {
return callback(err, null);
}
}
return callback(new Error('not a tile'),null);
}
var error = function(res,msg) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
return res.end(msg);
}
var renderer = function(map, params, res, filepath, cache) {
var bbox = mercator.bbox(params.x, params.y, params.z, false, '900913');
map.extent = bbox;
if (params.format == 'osmtile') {
vector_ren.render(map, function(err, output) {
process.nextTick(function() {
map_pool.release(map);
});
if (err) {
console.log(err);
return error(res,err.message)
}
console.log("TILE(%d/%d/%d) OUTPUT len=%d", params.z, params.x, params.y, output.length);
osmtile_response(res,output);
if (cache) {
var dirname = path.dirname(filepath);
mkdirp(dirname, function (err) {
if (err) {
console.log(err);
return error(res,err.message)
}
fs.writeFile(filepath,output,function(err) {
if (err) {
console.log(err);
return error(res,err.message)
}
return res.end(output);
});
});
} else {
return res.end(output);
}
});
} else {
var im = new mapnik.Image(map.width,map.height);
map.render(im,function(err,im) {
process.nextTick(function() {
map_pool.release(map);
});
if (err) {
console.log(err);
return error(res,err.message)
}
im.encode(params.format,function(err,buffer) {
if (err) {
console.log(err);
return error(res,err.message)
}
res.writeHead(200, {'Content-Type': 'image/png'});
if (!chunked) {
res.writeHead(200,{'Content-length': buffer.length});
res.useChunkedEncodingByDefault=false;
res.chunkedEncoding=false;
}
return res.end(buffer);
});
});
}
};
function osmtile_response(res,output) {
var content_length = output.length + 4;
var head = new Buffer(4);
head[0] = (output.length >> 24) & 0xff;
head[1] = (output.length >> 16) & 0xff;
head[2] = (output.length >> 8) & 0xff;
head[3] = output.length & 0xff;
res.writeHead(200,{'Content-type': 'application/osmtile'});
if (chunked) {
res.writeHead(200,{'Connection': 'close'});
} else {
var content_length = output.length + 4;
res.writeHead(200,{'Content-length': content_length});
res.useChunkedEncodingByDefault=false;
res.chunkedEncoding=false;
}
res.write(head);
}
var server = http.createServer(function(req, res) {
var uri = url.parse(req.url).pathname;
if (!uri || uri == '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
return res.end(fs.readFileSync(path.join(root,'index.html')));
}
if (uri == 'favicon.ico') {
return error(res,'');
}
var filepath = path.join(root, uri);
exists(filepath, function(exist) {
var format = path.extname(uri);
if (exist && (format == '.osmtile') && cache) {
if (format == '.osmtile') {
fs.readFile(filepath,function(err,output) {
if (err) {
console.log(err);
return error(res,err.message);
}
osmtile_response(res,output);
return res.end(output);
});
} else {
res.writeHead(200, {'Content-Type': mime.lookup(filepath)});
return res.end(fs.readFileSync(filepath));
}
} else if (exist && (format != '.osmtile')) {
res.writeHead(200, {'Content-Type': mime.lookup(filepath)});
return res.end(fs.readFileSync(filepath));
} else {
parse_url(uri, function(err,params) {
if (err) {
console.log(err);
return error(res,err.message);
}
map_pool.acquire(function(err, map) {
if (err) {
console.log(err);
return error(res,err.message)
}
return renderer(map,params,res,filepath,cache);
});
});
}
});
})
if (debug) {
server.on('connection',function(socket) {
console.log('new connection');
});
server.on('closing',function() {
console.log('closing')
});
server.on('connect',function() {
console.log('connect method')
});
server.on('clientError',function(err) {
console.log('clientError' + err.message)
});
}
server.listen(port);
console.log('Vector tile server listening on port %d | directory %s', port, root);