Support for raster mbtiles (issue #13)

This commit is contained in:
Petr Sloup 2016-04-21 18:23:13 +02:00
parent d486a8595b
commit c0fb4fd400
7 changed files with 55 additions and 44 deletions

View file

@ -43,8 +43,13 @@
<div class="item">
<div class="sample-img"></div>
<h3>{{name}}</h3>
<p>Id: {{@key}} | <a href="/vector/{{@key}}.json">TileJSON</a></p>
<a class="btn" href="/vector/{{@key}}/{{viewer_hash}}">X-Ray viewer</a>
<p>Id: {{@key}} | <a href="/data/{{@key}}.json">TileJSON</a></p>
{{#is_vector}}
<a class="btn" href="/data/{{@key}}/{{viewer_hash}}">X-Ray viewer</a>
{{/is_vector}}
{{^is_vector}}
Raster tiles
{{/is_vector}}
</div>
{{/each}}
</div>

View file

@ -84,7 +84,7 @@
}
};
xhttp.responseType = 'json';
xhttp.open('GET', '/vector/{{id}}.json', true);
xhttp.open('GET', '/data/{{id}}.json', true);
xhttp.send();
var propertyList = document.getElementById('propertyList');

View file

@ -7,7 +7,7 @@ var clone = require('clone'),
express = require('express');
module.exports = function(options, repo, params, id, reportVector, reportFont) {
module.exports = function(options, repo, params, id, reportTiles, reportFont) {
var app = express().disable('x-powered-by');
var styleFile = path.join(options.paths.styles, params.style);
@ -18,8 +18,8 @@ module.exports = function(options, repo, params, id, reportVector, reportFont) {
var url = source.url;
if (url.lastIndexOf('mbtiles:', 0) === 0) {
var mbtiles = url.substring('mbtiles://'.length);
var identifier = reportVector(mbtiles);
source.url = 'local://vector/' + identifier + '.json';
var identifier = reportTiles(mbtiles);
source.url = 'local://data/' + identifier + '.json';
}
});

View file

@ -23,24 +23,27 @@ module.exports = function(options, repo, params, id) {
function(err) {
source.getInfo(function(err, info) {
tileJSON['name'] = id;
tileJSON['format'] = 'pbf';
Object.assign(tileJSON, info);
tileJSON['tilejson'] = '2.0.0';
tileJSON['basename'] = id;
tileJSON['format'] = 'pbf';
Object.assign(tileJSON, params.tilejson || {});
utils.fixTileJSONCenter(tileJSON);
});
});
var tilePattern = '/vector/' + id + '/:z(\\d+)/:x(\\d+)/:y(\\d+).pbf';
var tilePattern = '/data/' + id + '/:z(\\d+)/:x(\\d+)/:y(\\d+).:format([\\w]+)';
app.get(tilePattern, function(req, res, next) {
var z = req.params.z | 0,
x = req.params.x | 0,
y = req.params.y | 0;
if (req.params.format != tileJSON.format) {
return res.status(404).send('Invalid format');
}
if (z < tileJSON.minzoom || 0 || x < 0 || y < 0 ||
z > tileJSON.maxzoom ||
x >= Math.pow(2, z) || y >= Math.pow(2, z)) {
@ -56,8 +59,10 @@ module.exports = function(options, repo, params, id) {
} else {
var md5 = crypto.createHash('md5').update(data).digest('base64');
headers['content-md5'] = md5;
if (tileJSON['format'] == 'pbf') {
headers['content-type'] = 'application/x-protobuf';
headers['content-encoding'] = 'gzip';
}
res.set(headers);
if (data == null) {
@ -69,10 +74,10 @@ module.exports = function(options, repo, params, id) {
});
});
app.get('/vector/' + id + '.json', function(req, res, next) {
app.get('/data/' + id + '.json', function(req, res, next) {
var info = clone(tileJSON);
info.tiles = utils.getTileUrls(req, info.tiles,
'vector/' + id, info.format);
'data/' + id, info.format);
return res.send(info);
});

View file

@ -17,7 +17,7 @@ var clone = require('clone'),
var serve_font = require('./serve_font'),
serve_raster = require('./serve_raster'),
serve_style = require('./serve_style'),
serve_vector = require('./serve_vector'),
serve_tiles = require('./serve_tiles'),
utils = require('./utils');
module.exports = function(opts, callback) {
@ -25,7 +25,7 @@ module.exports = function(opts, callback) {
serving = {
styles: {},
raster: {},
vector: {},
tiles: {},
fonts: { // default fonts, always expose these (if they exist)
'Open Sans Regular': true,
'Arial Unicode MS Regular': true
@ -61,7 +61,7 @@ module.exports = function(opts, callback) {
paths.sprites = path.resolve(paths.root, paths.sprites || '');
paths.mbtiles = path.resolve(paths.root, paths.mbtiles || '');
var vector = clone(config.vector || {});
var tiles = clone(config.data || {});
Object.keys(config.styles || {}).forEach(function(id) {
var item = config.styles[id];
@ -70,21 +70,21 @@ module.exports = function(opts, callback) {
return;
}
if (item.vector !== false) {
if (item.serve_data !== false) {
app.use('/', serve_style(options, serving.styles, item, id,
function(mbtiles) {
var vectorItemId;
Object.keys(vector).forEach(function(id) {
if (vector[id].mbtiles == mbtiles) {
vectorItemId = id;
var tilesItemId;
Object.keys(tiles).forEach(function(id) {
if (tiles[id].mbtiles == mbtiles) {
tilesItemId = id;
}
});
if (vectorItemId) { // mbtiles exist in the vector config
return vectorItemId;
if (tilesItemId) { // mbtiles exist in the tiles config
return tilesItemId;
} else {
var id = mbtiles.substr(0, mbtiles.lastIndexOf('.')) || mbtiles;
while (vector[id]) id += '_';
vector[id] = {
while (tiles[id]) id += '_';
tiles[id] = {
'mbtiles': mbtiles
};
return id;
@ -105,14 +105,14 @@ module.exports = function(opts, callback) {
app.use(cors());
Object.keys(vector).forEach(function(id) {
var item = vector[id];
Object.keys(tiles).forEach(function(id) {
var item = tiles[id];
if (!item.mbtiles || item.mbtiles.length == 0) {
console.log('Missing "mbtiles" property for ' + id);
return;
}
app.use('/', serve_vector(options, serving.vector, item, id));
app.use('/', serve_tiles(options, serving.tiles, item, id));
});
app.get('/styles.json', function(req, res, next) {
@ -142,11 +142,11 @@ module.exports = function(opts, callback) {
app.get('/raster.json', function(req, res, next) {
res.send(addTileJSONs([], req, 'raster'));
});
app.get('/vector.json', function(req, res, next) {
res.send(addTileJSONs([], req, 'vector'));
app.get('/data.json', function(req, res, next) {
res.send(addTileJSONs([], req, 'tiles'));
});
app.get('/index.json', function(req, res, next) {
res.send(addTileJSONs(addTileJSONs([], req, 'raster'), req, 'vector'));
res.send(addTileJSONs(addTileJSONs([], req, 'raster'), req, 'tiles'));
});
//------------------------------------
@ -195,15 +195,16 @@ module.exports = function(opts, callback) {
}
}
});
var data = clone(serving.vector || {});
var data = clone(serving.tiles || {});
Object.keys(data).forEach(function(id) {
var vector = data[id];
var center = vector.center;
var tiles = data[id];
var center = tiles.center;
if (center) {
vector.viewer_hash = '#' + center[2] + '/' +
tiles.viewer_hash = '#' + center[2] + '/' +
center[1].toFixed(5) + '/' +
center[0].toFixed(5);
}
tiles.is_vector = tiles.format == 'pbf';
});
return {
styles: styles,
@ -228,14 +229,14 @@ module.exports = function(opts, callback) {
return res.redirect(301, '/styles/' + req.params.id + '/');
});
serveTemplate('/vector/:id/$', 'xray', function(params) {
serveTemplate('/data/:id/$', 'xray', function(params) {
var id = params.id;
var vector = serving.vector[id];
if (!vector) {
var tiles = serving.tiles[id];
if (!tiles) {
return null;
}
vector.id = id;
return vector;
tiles.id = id;
return tiles;
});
var server = app.listen(process.env.PORT || opts.port, function() {

View file

@ -41,7 +41,7 @@ var testTileJSON = function(url, basename) {
describe('Metadata', function() {
testTileJSONArray('/index.json');
testTileJSONArray('/raster.json');
testTileJSONArray('/vector.json');
testTileJSONArray('/data.json');
describe('/styles.json is valid array', function() {
it('is json', function(done) {
@ -65,5 +65,5 @@ describe('Metadata', function() {
});
testTileJSON('/raster/test.json', 'test');
testTileJSON('/vector/zurich-vector.json', 'zurich-vector');
testTileJSON('/data/zurich-vector.json', 'zurich-vector');
});

View file

@ -1,5 +1,5 @@
var testTile = function(prefix, z, x, y, status) {
var path = '/vector/' + prefix + '/' + z + '/' + x + '/' + y + '.pbf';
var path = '/data/' + prefix + '/' + z + '/' + x + '/' + y + '.pbf';
it(path + ' returns ' + status, function(done) {
var test = supertest(app).get(path);
if (status) test.expect(status);