forked from nodetiles/nodetiles-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.js
120 lines (104 loc) · 3.09 KB
/
routes.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
var fs = require('fs'),
Map = require('./projector'),
Projector = require('./projector');
/**
* Map tile routing
* :zoom/:col/:row.png routing
*/
module.exports.tilePng = function tilePng(options){
var options = options || {},
map = options.map;
if (!options.map) {
throw new Error("You must set options.map equal to your map");
}
return function tilePng(req, res, next){
var tileCoordinate, bounds;
// verify arguments
tileCoordinate = req.path.match(/(\d+)\/(\d+)\/(\d+)\.png$/);
if (!tileCoordinate) {
return next();
}
// slice the regexp down to usable size
tileCoordinate = tileCoordinate.slice(1,4).map(Number);
// set the bounds and render
bounds = Projector.util.tileToMeters(tileCoordinate[1], tileCoordinate[2], tileCoordinate[0]);
map.render({
bounds: {minX: bounds[0], minY: bounds[1], maxX: bounds[2], maxY: bounds[3]},
width: 256,
height: 256,
zoom: tileCoordinate[0],
callback: function(err, canvas) {
// TODO: catche the error
var stream = canvas.createPNGStream();
stream.pipe(res);
}
});
};
};
/**
* UTFGrid routing
* :zoom/:col/:row.jsonp routing
*/
module.exports.utfGrid = function utfGrid(options){
var options = options || {},
map = options.map,
format;
if (!options.map) {
throw new Error("You must set options.map equal to your map");
}
return function tilePng(req, res, next){
var tileCoordinate, format, bounds;
// verify arguments (don't forget jsonp!)
tileCoordinate = req.path.match(/(\d+)\/(\d+)\/(\d+)\.(png|json|jsonp)$/);
if (!tileCoordinate) {
return next();
}
// slice the regexp down to usable size
console.log(tileCoordinate[4]);
format = tileCoordinate[4];
tileCoordinate = tileCoordinate.slice(1,4).map(Number);
// Show the rasterized utfgrid for debugging
respondWithImage = format === 'png';
if (respondWithImage) {
renderHandler = function(err, canvas) {
var stream = canvas.createPNGStream();
stream.pipe(res);
};
}
else {
renderHandler = function(err, grid) {
res.jsonp(grid);
};
}
bounds = Projector.util.tileToMeters(tileCoordinate[1], tileCoordinate[2], tileCoordinate[0], 64);
map.renderGrid({
bounds: {minX: bounds[0], minY: bounds[1], maxX: bounds[2], maxY: bounds[3]},
width: 64,
height: 64,
zoom: tileCoordinate[0],
drawImage: respondWithImage,
callback: renderHandler
});
};
};
module.exports.tileJson = function tileJson(options) {
var options = options || {},
path = options.path;
if (!options.path) {
throw new Error("You must set options.path to point to your tile.json file");
}
return function tileJson(req, res, next){
fs.readFile(path, 'utf8', function(err, file){
if (err) return next(err);
var tileJson;
// don't let JSON.parse barf all over everything
try {
tileJson = JSON.parse(file);
}
catch(err) {
return next(err);
}
return res.jsonp(tileJson);
});
}
};