-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
111 lines (96 loc) · 3.62 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
/**
*
*
*
* EXAMPLE SERVER
* (this server is unnecessary if you are using the `nodetiles` command line tool)
*
* You can run this server using `node server`
* (be sure you run `npm install` first to install dependencies)
*
*
*
*/
// Basic configuration
var PORT = process.env.PORT || process.argv[2] || 3000;
var DEBUG = true;
var path = require('path'),
express = require('express'),
app = module.exports = express(),
fs = require('fs');
//
// Setup the map
//
var nodetiles = require('nodetiles-core'),
GeoJsonSource = nodetiles.datasources.GeoJson,
Projector = nodetiles.projector;
var map = new nodetiles.Map();
map.addData(new GeoJsonSource({
name: "world",
path: __dirname + '/map/data/countries.geojson',
projection: "EPSG:900913"
}));
map.addData(new GeoJsonSource({
name: "example",
path: __dirname + '/map/data/example.geojson',
projection: "EPSG:4326"
}));
map.addStyle(fs.readFileSync('./map/theme/style.mss','utf8'));
//
// Configure Express routes
//
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
// Backbone routing
app.use('/assets', express.static(__dirname + '/assets'));
});
app.configure('production', function(){
app.use(express.errorHandler());
io.set('log level', 1); // reduce logging
// Backbone routing: compilation step is included in `npm install` script
app.use('/app', express.static(__dirname + '/dist/release'));
app.use(express.static(__dirname + '/public'));
});
// web mercator bounds in lat/lon
//EXTENT="-180 -85.05112878 180 85.05112878"
//
// 3. Serve the map image, size `size` center at `lon,lat`
app.get('/static/map.png', function img(req, res) {
var lonMin = parseFloat(req.query["lonMin"]) || -170;
var latMin = parseFloat(req.query["latMin"]) || -57.0511;
var lonMax = parseFloat(req.query["lonMax"]) || 170;
var latMax = parseFloat(req.query["latMax"]) || 75.0511;
var width = parseInt(req.query["width"], 10) || 800;
var height = parseInt(req.query["height"], 10) || width || 500;
var proj = req.query["proj"] ? "EPSG:"+req.query["proj"] : "EPSG:900913";
getImg([lonMin, latMin], [lonMax, latMax], width, height, proj, res);
// getImg([-170, -57.0511], [170, 75.0511], 800, 500, "EPSG:900913", res);
});
app.get('/static/:lonMin/:latMin/:lonMax/:latMax/:width/:height/:proj/map.png', function img(req, res) {
var proj = "EPSG:"+req.params.proj;
getImg([req.params.lonMin, req.params.latMin].map(parseFloat),[req.params.lonMax, req.params.latMax].map(parseFloat), parseInt(req.params.width,10), parseInt(req.params.height,10), proj, res);
});
app.get('/static/:lonMin/:latMin/:lonMax/:latMax/:width/:height/map.png', function img(req, res) {
var proj = "EPSG:900913";
getImg([req.params.lonMin, req.params.latMin].map(parseFloat),[req.params.lonMax, req.params.latMax].map(parseFloat), parseInt(req.params.width,10), parseInt(req.params.height,10), proj, res);
});
function getImg(min, max, width, height, proj, res) {
map.projection = proj;
var pMin = Projector.project.Point("EPSG:4326", proj, min);
var pMax = Projector.project.Point("EPSG:4326", proj, max);
// verify arguments
//if (! || tileCoordinate.length != 3) {
// res.send(404, req.url + 'not a coordinate, match =' + tileCoordinate);
// return;
// }
// set the bounds and render
map.render(pMin[0],pMin[1], pMax[0], pMax[1], width, height, function(error, canvas) {
if (error){
console.error(error);
}
var stream = canvas.createPNGStream();
stream.pipe(res);
});
}
app.listen(PORT);
console.log("Express server listening on port %d in %s mode", PORT, app.settings.env);