forked from mapbox/tilelive-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
388 lines (342 loc) · 14.9 KB
/
index.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
var url = require('url');
var path = require('path');
var zlib = require('zlib');
var crypto = require('crypto');
var mapnik = require('mapnik');
var fs = require('fs');
var qs = require('querystring');
var sm = new (require('sphericalmercator'))();
var immediate = global.setImmediate || process.nextTick;
// Register datasource plugins
mapnik.register_default_input_plugins();
var mapnikPool = require('mapnik-pool')(mapnik);
module.exports = Bridge;
function Bridge(uri, callback) {
var source = this;
if (typeof uri === 'string' || (uri.protocol && !uri.xml)) {
uri = typeof uri === 'string' ? url.parse(uri) : uri;
uri.query = typeof uri.query === 'string' ? qs.parse(uri.query) : (uri.query || {});
var filepath = path.resolve(uri.pathname);
fs.readFile(filepath, 'utf8', function(err, xml) {
if (err) return callback(err);
var opts = Object.keys(uri.query).reduce(function(memo, key) {
memo[key] = !!parseInt(uri.query[key], 10);
return memo;
}, {xml:xml, base:path.dirname(filepath)});
init(opts);
});
return source;
} else {
init(uri);
return source;
}
function init(uri) {
if (!uri.xml) return callback && callback(new Error('No xml'));
source._uri = uri;
source._deflate = typeof uri.deflate === 'boolean' ? uri.deflate : true;
source._base = path.resolve(uri.base || __dirname);
// 'blank' option forces all solid tiles to be interpreted as blank.
source._blank = typeof uri.blank === 'boolean' ? uri.blank : false;
if (callback) source.once('open', callback);
source.update(uri, function(err) {
source.emit('open', err, source);
});
}
}
require('util').inherits(Bridge, require('events').EventEmitter);
Bridge.registerProtocols = function(tilelive) {
tilelive.protocols['bridge:'] = Bridge;
};
// Helper for callers to ensure source is open. This is not built directly
// into the constructor because there is no good auto cache-keying system
// for these tile sources (ie. sharing/caching is best left to the caller).
Bridge.prototype.open = function(callback) {
if (this._map) return callback(null, this);
this.once('open', callback);
};
// Allows in-place update of XML/backends.
Bridge.prototype.update = function(opts, callback) {
// If the XML has changed update the map.
if (opts.xml && this._xml !== opts.xml) {
// Unset maxzoom. Will be re-set on first getTile.
this._maxzoom = undefined;
// Unset type. Will be re-set on first getTile.
this._type = undefined;
this._xml = opts.xml;
this._map = mapnikPool.fromString(this._xml,
{ size: 256, bufferSize: 256 },
{ strict: false, base: this._base + '/' });
// If no nextTick the stale pool can be used to acquire new maps.
return immediate(function() {
this._map.destroyAllNow(callback);
}.bind(this));
}
return callback();
};
Bridge.prototype.close = function(callback) {
if (!this._map) return callback();
this._map.destroyAllNow(callback);
};
Bridge.prototype.getTile = function(z, x, y, callback) {
if (!this._map) return callback(new Error('Tilesource not loaded'));
var source = this;
source._map.acquire(function(err, map) {
if (err) return callback(err);
// set source _maxzoom cache to prevent repeat calls to map.parameters
if (source._maxzoom === undefined) {
source._maxzoom = map.parameters.maxzoom ? parseInt(map.parameters.maxzoom, 10) : 14;
}
// set source _type cache to prevent repeat calls to map layers
if (source._type === undefined) {
var layers = map.layers();
if (layers.length && layers.some(function(l) { return l.datasource.type === 'raster' })) {
source._type = 'raster';
} else {
source._type = 'vector';
}
}
if (source._type === 'raster') {
Bridge.getRaster(source, map, z, x, y, callback);
} else {
Bridge.getVector(source, map, z, x, y, callback);
}
});
};
Bridge.getRaster = function(source, map, z, x, y, callback) {
map.resize(512,512);
map.extent = sm.bbox(+x,+y,+z, false, '900913');
map.render(new mapnik.Image(512,512), function(err, image) {
immediate(function() { source._map.release(map); });
if (err) return callback(err);
var view = image.view(0,0,512,512);
view.isSolid(function(err, solid, pixel) {
if (err) return callback(err);
// If source is in blank mode any solid tile is empty.
if (solid && source._blank) return callback(new Error('Tile does not exist'));
var pixel_key = '';
if (solid) {
var a = (pixel>>>24) & 0xff;
var r = pixel & 0xff;
var g = (pixel>>>8) & 0xff;
var b = (pixel>>>16) & 0xff;
pixel_key = 'webp' + ',' + r +','+ g + ',' + b + ',' + a;
}
view.encode('webp', {}, function(err, buffer) {
if (err) return callback(err);
buffer.solid = pixel_key;
return callback(err, buffer, {'Content-Type':'image/webp'});
});
});
});
};
Bridge.getVector = function(source, map, z, x, y, callback) {
var opts = {};
// use tolerance of 32 for zoom levels below max
opts.tolerance = z < source._maxzoom ? 32 : 0;
// make larger than zero to enable
opts.simplify = 0;
// 'radial-distance', 'visvalingam-whyatt', 'zhao-saalfeld' (default)
opts.simplify_algorithm = 'radial-distance';
var headers = {};
headers['Content-Type'] = 'application/x-protobuf';
if (source._deflate) headers['Content-Encoding'] = 'deflate';
map.resize(256, 256);
map.extent = sm.bbox(+x,+y,+z, false, '900913');
// also pass buffer_size in options to be forward compatible with recent node-mapnik
// https://github.com/mapnik/node-mapnik/issues/175
opts.buffer_size = map.bufferSize;
map.render(new mapnik.VectorTile(+z,+x,+y), opts, function(err, image) {
immediate(function() { source._map.release(map); });
if (err) return callback(err);
// Fake empty RGBA to the rest of the tilelive API for now.
image.isSolid(function(err, solid, key) {
if (err) return callback(err);
// Solid handling.
var done = function(err, buffer) {
if (err) return callback(err);
if (solid === false) return callback(err, buffer, headers);
// Empty tiles are equivalent to no tile.
if (source._blank || !key) return callback(new Error('Tile does not exist'));
// Fake a hex code by md5ing the key.
var mockrgb = crypto.createHash('md5').update(buffer).digest('hex').substr(0,6);
buffer.solid = [
parseInt(mockrgb.substr(0,2),16),
parseInt(mockrgb.substr(2,2),16),
parseInt(mockrgb.substr(4,2),16),
1
].join(',');
return callback(err, buffer, headers);
};
// No deflate.
return !source._deflate
? done(null, image.getData())
: zlib.deflate(image.getData(), done);
});
});
};
Bridge.prototype.getInfo = function(callback) {
if (!this._map) return callback(new Error('Tilesource not loaded'));
this._map.acquire(function(err, map) {
if (err) return callback(err);
var params = map.parameters;
var info = Object.keys(params).reduce(function(memo, key) {
switch (key) {
// The special "json" key/value pair allows JSON to be serialized
// and merged into the metadata of a mapnik XML based source. This
// enables nested properties and non-string datatypes to be
// captured by mapnik XML.
case 'json':
try {
var jsondata = JSON.parse(params[key]);
Object.keys(jsondata).reduce(function(memo, key) {
memo[key] = memo[key] || jsondata[key];
return memo;
}, memo);
}
catch (err) { return callback(err); }
break;
case 'bounds':
case 'center':
memo[key] = params[key].split(',').map(function(v) { return parseFloat(v) });
break;
case 'minzoom':
case 'maxzoom':
case 'geocoder_shardlevel':
case 'geocoder_resolution':
memo[key] = parseInt(params[key], 10);
break;
default:
memo[key] = params[key];
break;
}
return memo;
}, {});
// Set an intelligent default for geocoder_shardlevel if not set.
if (info.geocoder_layer && !('geocoder_shardlevel' in info)) {
if (info.maxzoom > 12) {
info.geocoder_shardlevel = 3;
} else if (info.maxzoom > 8) {
info.geocoder_shardlevel = 2;
} else if (info.maxzoom > 6) {
info.geocoder_shardlevel = 1;
} else {
info.geocoder_shardlevel = 0;
}
}
immediate(function() { this._map.release(map); }.bind(this));
return callback(null, info);
}.bind(this));
};
Bridge.prototype.getIndexableDocs = function(pointer, callback) {
if (!this._map) return callback(new Error('Tilesource not loaded'));
pointer = pointer || {};
pointer.limit = pointer.limit || 10000;
pointer.offset = pointer.offset || 0;
var source = this;
var knownsrs = {
'+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over': '900913',
'+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs': '900913',
'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs': 'WGS84'
};
source.getInfo(function(err, info) {
if (err) return callback(err);
source._map.acquire(function(err, map) {
if (err) return callback(err);
immediate(function() { source._map.release(map); });
var name = (map.parameters.geocoder_layer||'').split('.').shift() || '';
var field = (map.parameters.geocoder_layer||'').split('.').pop() || '_text';
var zoom = info.maxzoom + parseInt(map.parameters.geocoder_resolution||0, 10);
var layer = name ?
map.layers().filter(function(l) { return l.name === name })[0] :
map.layers()[0];
if (!zoom) return callback(new Error('No geocoding zoom defined'));
if (!layer) return callback(new Error('No geocoding layer found'));
if (!knownsrs[layer.srs]) return callback(new Error('Unknown layer SRS'));
var srs = knownsrs[layer.srs];
var featureset = layer.datasource.featureset();
var params = layer.datasource.parameters();
var docs = [];
var cache = {};
var i = 0;
function feature() {
if (i === pointer.offset + pointer.limit) {
pointer.offset = pointer.offset + pointer.limit;
return callback(null, docs, pointer);
}
var f = featureset.next();
if (!f) {
pointer.offset = i;
return callback(null, docs, pointer);
}
// Skip over features if not yet paged to offset.
if (i < pointer.offset) return ++i && immediate(function() {
feature();
});
var doc = f.attributes();
if (!doc[field]) return ++i && immediate(function() {
feature();
});
doc._id = f.id();
doc._zxy = [];
doc._text = doc[field];
if (typeof doc._bbox === 'string') {
doc._bbox = doc._bbox.split(',');
doc._bbox[0] = parseFloat(doc._bbox[0]);
doc._bbox[1] = parseFloat(doc._bbox[1]);
doc._bbox[2] = parseFloat(doc._bbox[2]);
doc._bbox[3] = parseFloat(doc._bbox[3]);
} else {
doc._bbox = doc._bbox || (srs === 'WGS84' ? f.extent() : sm.convert(f.extent(), 'WGS84'));
}
if (typeof doc._center === 'string') {
doc._center = doc._center.split(',');
doc._center[0] = parseFloat(doc._center[0]);
doc._center[1] = parseFloat(doc._center[1]);
} else {
doc._center = [
doc._bbox[0] + (doc._bbox[2]-doc._bbox[0])*0.5,
doc._bbox[1] + (doc._bbox[3]-doc._bbox[1])*0.5
];
}
if (doc._bbox[0] === doc._bbox[2]) delete doc._bbox;
docs.push(doc);
var t = sm.xyz(f.extent(), zoom, false, srs);
var x = t.minX;
var y = t.minY;
var c = (t.maxX - t.minX + 1) * (t.maxY - t.minY + 1);
function tiles() {
if (x > t.maxX && y > t.maxY) {
return ++i && immediate(function() {
feature();
});
}
if (y > t.maxY && ++x) {
y = t.minY;
}
var key = zoom + '/' + x + '/' + y;
// Features must cover > 2 tiles to have false positives.
if (c < 3 || cache[key]) {
if (c < 3 || cache[key][doc._id]) doc._zxy.push(key);
y++;
return tiles();
}
cache[key] = {};
map.extent = sm.bbox(x,y,zoom,false,'900913');
map.render(new mapnik.VectorTile(zoom,x,y), {}, function(err, vtile) {
if (err) return callback(err);
var json = vtile.toJSON();
json.forEach(function(l) {
if (l.name !== layer.name) return;
for (var i = 0; i < l.features.length; i++) {
cache[key][l.features[i].id] = true;
}
});
immediate(function() { tiles(); });
});
}
tiles();
}
feature();
});
});
};