Configurable optional alias for .pbf tiles (#109)

This commit is contained in:
Petr Sloup 2017-01-25 09:43:34 +01:00
parent 1079ece860
commit 6231f9f7a7
4 changed files with 24 additions and 9 deletions

View file

@ -26,6 +26,7 @@ Example::
"png": 90
},
"maxSize": 2048,
"pbfAlias": "pbf",
"serveAllFonts": false
},
"styles": {

View file

@ -50,14 +50,18 @@ module.exports = function(options, repo, params, id, styles) {
});
});
var tilePattern = '/' + id + '/:z(\\d+)/:x(\\d+)/:y(\\d+).:format([\\w]+)';
var tilePattern = '/' + 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 &&
!(req.params.format == 'geojson' && tileJSON.format == 'pbf')) {
var format = req.params.format;
if (format == options.pbfAlias) {
format = 'pbf';
}
if (format != tileJSON.format &&
!(format == 'geojson' && tileJSON.format == 'pbf')) {
return res.status(404).send('Invalid format');
}
if (z < tileJSON.minzoom || 0 || x < 0 || y < 0 ||
@ -106,9 +110,9 @@ module.exports = function(options, repo, params, id, styles) {
}
}
}
if (req.params.format == 'pbf') {
if (format == 'pbf') {
headers['Content-Type'] = 'application/x-protobuf';
} else if (req.params.format == 'geojson') {
} else if (format == 'geojson') {
headers['Content-Type'] = 'application/json';
if (isGzipped) {
@ -150,7 +154,9 @@ module.exports = function(options, repo, params, id, styles) {
app.get('/' + id + '.json', function(req, res, next) {
var info = clone(tileJSON);
info.tiles = utils.getTileUrls(req, info.tiles,
'data/' + id, info.format);
'data/' + id, info.format, {
'pbf': options.pbfAlias
});
return res.send(info);
});

View file

@ -186,7 +186,9 @@ module.exports = function(opts, callback) {
} else {
path = type + '/' + id;
}
info.tiles = utils.getTileUrls(req, info.tiles, path, info.format);
info.tiles = utils.getTileUrls(req, info.tiles, path, info.format, {
'pbf': options.pbfAlias
});
arr.push(info);
});
return arr;
@ -287,7 +289,9 @@ module.exports = function(opts, callback) {
'/data/' + id + '.json' + query) + '/wmts';
var tiles = utils.getTileUrls(
req, data_.tiles, 'data/' + id, data_.format);
req, data_.tiles, 'data/' + id, data_.format, {
'pbf': options.pbfAlias
});
data_.xyz_link = tiles[0];
}
if (data_.filesize) {

View file

@ -7,7 +7,7 @@ var async = require('async'),
var clone = require('clone'),
glyphCompose = require('glyph-pbf-composite');
module.exports.getTileUrls = function(req, domains, path, format) {
module.exports.getTileUrls = function(req, domains, path, format, aliases) {
if (domains) {
if (domains.constructor === String && domains.length > 0) {
@ -28,6 +28,10 @@ module.exports.getTileUrls = function(req, domains, path, format) {
}
var query = queryParams.length > 0 ? ('?' + queryParams.join('&')) : '';
if (aliases && aliases[format]) {
format = aliases[format];
}
var uris = [];
domains.forEach(function(domain) {
uris.push(req.protocol + '://' + domain + '/' + path +