From 0673c8990a3eb55bada175b5b7674405aa6fbef9 Mon Sep 17 00:00:00 2001 From: Petr Sloup Date: Tue, 14 Mar 2017 16:12:19 +0100 Subject: [PATCH] Add option to disable static maps (close #129) --- docs/config.rst | 3 +- src/serve_rendered.js | 210 +++++++++++++++++++++--------------------- 2 files changed, 108 insertions(+), 105 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index 814bb97..aedbd70 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -27,7 +27,8 @@ Example:: }, "maxSize": 2048, "pbfAlias": "pbf", - "serveAllFonts": false + "serveAllFonts": false, + "serveStaticMaps": true }, "styles": { "basic": { diff --git a/src/serve_rendered.js b/src/serve_rendered.js index b2a6cbc..b12f7f4 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -474,121 +474,123 @@ module.exports = function(options, repo, params, id, dataResolver) { return z; }; - var staticPattern = - '/static/:raw(raw)?/%s/:width(\\d+)x:height(\\d+)' + - ':scale(' + SCALE_PATTERN + ')?\.:format([\\w]+)'; + if (options.serveStaticMaps !== false) { + var staticPattern = + '/static/:raw(raw)?/%s/:width(\\d+)x:height(\\d+)' + + ':scale(' + SCALE_PATTERN + ')?\.:format([\\w]+)'; - var centerPattern = - util.format(':x(%s),:y(%s),:z(%s)(@:bearing(%s)(,:pitch(%s))?)?', - FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, - FLOAT_PATTERN, FLOAT_PATTERN); + var centerPattern = + util.format(':x(%s),:y(%s),:z(%s)(@:bearing(%s)(,:pitch(%s))?)?', + FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, + FLOAT_PATTERN, FLOAT_PATTERN); - app.get(util.format(staticPattern, centerPattern), function(req, res, next) { - var raw = req.params.raw; - var z = +req.params.z, - x = +req.params.x, - y = +req.params.y, - bearing = +(req.params.bearing || '0'), - pitch = +(req.params.pitch || '0'), - w = req.params.width | 0, - h = req.params.height | 0, - scale = getScale(req.params.scale), - format = req.params.format; + app.get(util.format(staticPattern, centerPattern), function(req, res, next) { + var raw = req.params.raw; + var z = +req.params.z, + x = +req.params.x, + y = +req.params.y, + bearing = +(req.params.bearing || '0'), + pitch = +(req.params.pitch || '0'), + w = req.params.width | 0, + h = req.params.height | 0, + scale = getScale(req.params.scale), + format = req.params.format; - if (z < 0) { - return res.status(404).send('Invalid zoom'); - } + if (z < 0) { + return res.status(404).send('Invalid zoom'); + } - if (raw) { - var ll = mercator.inverse([x, y]); - x = ll[0]; - y = ll[1]; - } + if (raw) { + var ll = mercator.inverse([x, y]); + x = ll[0]; + y = ll[1]; + } - var path = extractPathFromQuery(req.query); - var overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, - path, req.query); + var path = extractPathFromQuery(req.query); + var overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, + path, req.query); - return respondImage(z, x, y, bearing, pitch, w, h, scale, format, - res, next, overlay); - }); - - var boundsPattern = - util.format(':minx(%s),:miny(%s),:maxx(%s),:maxy(%s)', - FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN); - - app.get(util.format(staticPattern, boundsPattern), function(req, res, next) { - var raw = req.params.raw; - var bbox = [+req.params.minx, +req.params.miny, - +req.params.maxx, +req.params.maxy]; - - if (raw) { - var minCorner = mercator.inverse(bbox.slice(0, 2)); - var maxCorner = mercator.inverse(bbox.slice(2)); - bbox[0] = minCorner[0]; - bbox[1] = minCorner[1]; - bbox[2] = maxCorner[0]; - bbox[3] = maxCorner[1]; - } - - var w = req.params.width | 0, - h = req.params.height | 0, - scale = getScale(req.params.scale), - format = req.params.format; - - var z = calcZForBBox(bbox, w, h, req.query), - x = (bbox[0] + bbox[2]) / 2, - y = (bbox[1] + bbox[3]) / 2, - bearing = 0, - pitch = 0; - - var path = extractPathFromQuery(req.query); - var overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, - path, req.query); - return respondImage(z, x, y, bearing, pitch, w, h, scale, format, - res, next, overlay); - }); - - var autoPattern = 'auto'; - - app.get(util.format(staticPattern, autoPattern), function(req, res, next) { - var path = extractPathFromQuery(req.query); - if (path.length < 2) { - return res.status(400).send('Invalid path'); - } - - var raw = req.params.raw; - var w = req.params.width | 0, - h = req.params.height | 0, - bearing = 0, - pitch = 0, - scale = getScale(req.params.scale), - format = req.params.format; - - var bbox = [Infinity, Infinity, -Infinity, -Infinity]; - path.forEach(function(pair) { - bbox[0] = Math.min(bbox[0], pair[0]); - bbox[1] = Math.min(bbox[1], pair[1]); - bbox[2] = Math.max(bbox[2], pair[0]); - bbox[3] = Math.max(bbox[3], pair[1]); + return respondImage(z, x, y, bearing, pitch, w, h, scale, format, + res, next, overlay); }); - var z = calcZForBBox(bbox, w, h, req.query), - x = (bbox[0] + bbox[2]) / 2, - y = (bbox[1] + bbox[3]) / 2; + var boundsPattern = + util.format(':minx(%s),:miny(%s),:maxx(%s),:maxy(%s)', + FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN); - if (raw) { - var ll = mercator.inverse([x, y]); - x = ll[0]; - y = ll[1]; - } + app.get(util.format(staticPattern, boundsPattern), function(req, res, next) { + var raw = req.params.raw; + var bbox = [+req.params.minx, +req.params.miny, + +req.params.maxx, +req.params.maxy]; - var overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, - path, req.query); + if (raw) { + var minCorner = mercator.inverse(bbox.slice(0, 2)); + var maxCorner = mercator.inverse(bbox.slice(2)); + bbox[0] = minCorner[0]; + bbox[1] = minCorner[1]; + bbox[2] = maxCorner[0]; + bbox[3] = maxCorner[1]; + } - return respondImage(z, x, y, bearing, pitch, w, h, scale, format, - res, next, overlay); - }); + var w = req.params.width | 0, + h = req.params.height | 0, + scale = getScale(req.params.scale), + format = req.params.format; + + var z = calcZForBBox(bbox, w, h, req.query), + x = (bbox[0] + bbox[2]) / 2, + y = (bbox[1] + bbox[3]) / 2, + bearing = 0, + pitch = 0; + + var path = extractPathFromQuery(req.query); + var overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, + path, req.query); + return respondImage(z, x, y, bearing, pitch, w, h, scale, format, + res, next, overlay); + }); + + var autoPattern = 'auto'; + + app.get(util.format(staticPattern, autoPattern), function(req, res, next) { + var path = extractPathFromQuery(req.query); + if (path.length < 2) { + return res.status(400).send('Invalid path'); + } + + var raw = req.params.raw; + var w = req.params.width | 0, + h = req.params.height | 0, + bearing = 0, + pitch = 0, + scale = getScale(req.params.scale), + format = req.params.format; + + var bbox = [Infinity, Infinity, -Infinity, -Infinity]; + path.forEach(function(pair) { + bbox[0] = Math.min(bbox[0], pair[0]); + bbox[1] = Math.min(bbox[1], pair[1]); + bbox[2] = Math.max(bbox[2], pair[0]); + bbox[3] = Math.max(bbox[3], pair[1]); + }); + + var z = calcZForBBox(bbox, w, h, req.query), + x = (bbox[0] + bbox[2]) / 2, + y = (bbox[1] + bbox[3]) / 2; + + if (raw) { + var ll = mercator.inverse([x, y]); + x = ll[0]; + y = ll[1]; + } + + var overlay = renderOverlay(z, x, y, bearing, pitch, w, h, scale, + path, req.query); + + return respondImage(z, x, y, bearing, pitch, w, h, scale, format, + res, next, overlay); + }); + } app.get('/rendered.json', function(req, res, next) { var info = clone(tileJSON);