From 6231f9f7a721deb618af8272cc52d691854590cf Mon Sep 17 00:00:00 2001 From: Petr Sloup Date: Wed, 25 Jan 2017 09:43:34 +0100 Subject: [PATCH] Configurable optional alias for .pbf tiles (#109) --- docs/config.rst | 1 + src/serve_data.js | 18 ++++++++++++------ src/server.js | 8 ++++++-- src/utils.js | 6 +++++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index 699ee3e..814bb97 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -26,6 +26,7 @@ Example:: "png": 90 }, "maxSize": 2048, + "pbfAlias": "pbf", "serveAllFonts": false }, "styles": { diff --git a/src/serve_data.js b/src/serve_data.js index 6081f3a..b1306ac 100644 --- a/src/serve_data.js +++ b/src/serve_data.js @@ -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); }); diff --git a/src/server.js b/src/server.js index 15aa48c..752bd15 100644 --- a/src/server.js +++ b/src/server.js @@ -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) { diff --git a/src/utils.js b/src/utils.js index 261695f..4b6d66f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 +