-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
77 lines (71 loc) · 1.82 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
var MapnikBackend = require('./tilelive-mapnik/lib/mapnik_backend');
module.exports = function(options) {
options = Object.assign({
interactivity: false,
xml: null,
metatile: 2,
resolution: 4,
bufferSize: 128,
tileSize: 256,
scale: 1
}, options);
var source;
/**
* Initializes the mapnik datasource.
*
* @param {TileServer} server
* @param {function} callback(err, fn)
* @return {void}
*/
function initialize(server, callback) {
var uri = { query: Object.assign({}, options) };
if (uri.query.pathname) {
uri.pathname = uri.query.pathname;
delete uri.query.pathname;
}
if (uri.query.xml) {
uri.xml = uri.query.xml;
delete uri.query.xml;
}
new MapnikBackend(uri, function(err, result) {
source = result;
callback(err);
});
}
/**
* Renders a tile and returns the result as a buffer (PNG),
* plus the headers that should accompany it.
*
* @param {TileServer} server
* @param {TileRequest} req
* @param {function} callback(err, buffer, headers)
* @return {void}
*/
function serveImage(server, req, callback) {
source.getTile(req.z, req.x, req.y, function(err, buffer, headers) {
if (err) return callback(err);
callback(err, buffer, Object.assign({}, headers));
});
}
/**
* Renders a an interactivity tile (JSON).
*
* @param {TileServer} server
* @param {TileRequest} req
* @param {function} callback(err, buffer, headers)
* @return {void}
*/
function serveGrid(server, req, callback) {
source.getGrid(req.z, req.x, req.y, function(err, json, headers) {
if (err) return callback(err);
var buffer = Buffer.from(JSON.stringify(json), 'utf8');
buffer._utfgrid = json;
callback(err, buffer, Object.assign({}, headers));
});
}
return {
name: 'mapnik',
init: initialize,
serve: options.interactivity ? serveGrid : serveImage
};
};