forked from naturalatlas/tilestrata-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (70 loc) · 1.81 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 _ = require('lodash');
var MapnikBackend = require('tilelive-mapnik');
module.exports = function(options) {
options = _.defaults(options, {
interactivity: false,
xml: null,
metatile: 2,
resolution: 4,
bufferSize: 128,
tileSize: 256,
scale: 1
});
var source;
/**
* Initializes the mapnik datasource.
*
* @param {TileServer} server
* @param {function} callback(err, fn)
* @return {void}
*/
function initialize(server, callback) {
var url = 'mapnik://' + options.xml + '?' + [
'metatile=' + options.metatile,
'resolution=' + options.resolution,
'bufferSize=' + options.bufferSize,
'tileSize=' + options.tileSize,
'scale=' + options.scale
].join('&');
new MapnikBackend(url, 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, _.clone(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 = new Buffer(JSON.stringify(json), 'utf8');
buffer._utfgrid = json;
callback(err, buffer, _.clone(headers));
});
}
return {
name: 'mapnik',
init: initialize,
serve: options.interactivity ? serveGrid : serveImage
};
};