forked from mapbox/mbview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbview.js
86 lines (72 loc) · 2.19 KB
/
mbview.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
/* eslint-disable no-console */
var express = require('express');
var app = express();
var MBTiles = require('mbtiles');
var q = require('d3-queue').queue();
var utils = require('./utils');
var objectAssign = require('object-assign');
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static('public'));
module.exports = {
/**
* Load a tileset and return a reference with metadata
* @param {object} file reference to the tileset
* @param {function} callback that returns the resulting tileset object
*/
loadTiles: function (file, callback) {
new MBTiles(file, function(err, tiles) {
if (err) throw err;
tiles.getInfo(function (err, info) {
if (err) throw err;
var tileset = objectAssign({}, info, {
tiles: tiles
});
callback(null, tileset);
});
});
},
/**
* Defer loading of multiple MBTiles and spin up server.
* Will merge all the configurations found in the sources.
* @param {object} config for the server, e.g. port
* @param {function} callback with the server configuration loaded
*/
serve: function (config, callback) {
var loadTiles = this.loadTiles;
var listen = this.listen;
config.mbtiles.forEach(function (file) {
q.defer(loadTiles, file);
});
q.awaitAll(function (error, tilesets) {
if (error) throw error;
var finalConfig = utils.mergeConfigurations(config, tilesets);
listen(finalConfig, callback);
});
},
listen: function (config, onListen) {
var format = config.tiles._info.format;
app.get('/', function (req, res) {
if (format === 'pbf') {
res.render('vector', config);
} else {
res.render('raster', config);
}
});
app.get('/:source/:z/:x/:y.' + format, function (req, res) {
var p = req.params;
var tiles = config.sources[p.source].tiles;
tiles.getTile(p.z, p.x, p.y, function (err, tile, headers) {
if (err) {
res.end();
} else {
res.writeHead(200, headers);
res.end(tile);
}
});
});
config.server = app.listen(config.port, function () {
onListen(null, config);
});
}
};