From 75a5661539f1620061f1a379c775caaf1309bd3a Mon Sep 17 00:00:00 2001 From: Niklas Hoesl Date: Sat, 16 Sep 2017 20:56:09 +0200 Subject: [PATCH 1/3] ADD support for encoded path --- docs/endpoints.rst | 4 +++- src/serve_rendered.js | 5 ++++ src/utils.js | 54 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/docs/endpoints.rst b/docs/endpoints.rst index 0672f25..2820a4f 100644 --- a/docs/endpoints.rst +++ b/docs/endpoints.rst @@ -35,7 +35,9 @@ Static images * e.g. ``5.9,45.8|5.9,47.8|10.5,47.8|10.5,45.8|5.9,45.8`` - * ``latlng`` - indicates the ``path`` coordinates are in ``lat,lng`` order rather than the usual ``lng,lat`` + * ``encodedpath`` - an alternative way to provide a path using Google's polyline encoding. Only used if ``path`` is not provided. + * more details: https://developers.google.com/maps/documentation/utilities/polylinealgorithm + * ``latlng`` - indicates the ``path`` coordinates are in ``lat,lng`` order rather than the usual ``lng,lat``. possible values: ``true`` or ``1`` * ``fill`` - color to use as the fill (e.g. ``red``, ``rgba(255,255,255,0.5)``, ``#0000ff``) * ``stroke`` - color of the path stroke * ``width`` - width of the stroke diff --git a/src/serve_rendered.js b/src/serve_rendered.js index 435b0c5..30b39d0 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -504,6 +504,11 @@ module.exports = function(options, repo, params, id, dataResolver) { path.push(pair); } }); + + if(path.length==0){ + path = utils.decodePolyline(query.encodedpath); + } + return path; }; diff --git a/src/utils.js b/src/utils.js index a63731e..0997d4f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -116,3 +116,57 @@ module.exports.getFontsPbf = function(allowedFonts, fontPath, names, range, fall }, reject); }); }; + +// From: https://github.com/mapbox/polyline/blob/master/src/polyline.js +module.exports.decodePolyline = function(str, precision) { + var index = 0, + lat = 0, + lng = 0, + coordinates = [], + shift = 0, + result = 0, + byte = null, + latitude_change, + longitude_change, + factor = Math.pow(10, precision || 5); + + if(str==null){ + return []; + } + + // Coordinates have variable length when encoded, so just keep + // track of whether we've hit the end of the string. In each + // loop iteration, a single coordinate is decoded. + while (index < str.length) { + + // Reset shift, result, and byte + byte = null; + shift = 0; + result = 0; + + do { + byte = str.charCodeAt(index++) - 63; + result |= (byte & 0x1f) << shift; + shift += 5; + } while (byte >= 0x20); + + latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1)); + + shift = result = 0; + + do { + byte = str.charCodeAt(index++) - 63; + result |= (byte & 0x1f) << shift; + shift += 5; + } while (byte >= 0x20); + + longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1)); + + lat += latitude_change; + lng += longitude_change; + + coordinates.push([lng / factor, lat / factor]); + } + + return coordinates; +}; \ No newline at end of file From 61eef1518e80615f9310c50aa9d8ba7f8fb4a33a Mon Sep 17 00:00:00 2001 From: Niklas Hoesl Date: Sat, 16 Sep 2017 21:03:35 +0200 Subject: [PATCH 2/3] ADD linebreak --- docs/endpoints.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/endpoints.rst b/docs/endpoints.rst index 2820a4f..0d0e85c 100644 --- a/docs/endpoints.rst +++ b/docs/endpoints.rst @@ -36,6 +36,7 @@ Static images * e.g. ``5.9,45.8|5.9,47.8|10.5,47.8|10.5,45.8|5.9,45.8`` * ``encodedpath`` - an alternative way to provide a path using Google's polyline encoding. Only used if ``path`` is not provided. + * more details: https://developers.google.com/maps/documentation/utilities/polylinealgorithm * ``latlng`` - indicates the ``path`` coordinates are in ``lat,lng`` order rather than the usual ``lng,lat``. possible values: ``true`` or ``1`` * ``fill`` - color to use as the fill (e.g. ``red``, ``rgba(255,255,255,0.5)``, ``#0000ff``) From 11322bf916ca9c6aeb7537d982afff36135a373d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20H=C3=B6sl?= Date: Thu, 12 Oct 2017 09:17:34 +0200 Subject: [PATCH 3/3] ADD dynamic maps generation support --- src/serve_style.js | 24 +++++++++++++++++++++++- src/server.js | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/serve_style.js b/src/serve_style.js index cd9dfe9..600c7dc 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -16,7 +16,10 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) { Object.keys(styleJSON.sources).forEach(function(name) { var source = styleJSON.sources[name]; var url = source.url; - if (url && url.lastIndexOf('mbtiles:', 0) === 0) { + + var validateUrl = source.validate_url !=undefined ? source.validate_url : true + + if (url && url.lastIndexOf('mbtiles:', 0) === 0 && validateUrl) { var mbtilesFile = url.substring('mbtiles://'.length); var fromData = mbtilesFile[0] == '{' && mbtilesFile[mbtilesFile.length - 1] == '}'; @@ -63,7 +66,22 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) { repo[id] = styleJSON; app.get('/' + id + '/style.json', function(req, res, next) { + + var pattern = req.query.pattern + var value = req.query.value + + console.log("## Pattern: "+pattern) + console.log("## Value: "+value) + var fixUrl = function(url, opt_nokey, opt_nostyle) { + console.log("## Before if "+pattern) + + if(pattern != null){ + console.log("## Enter replace for "+url) + url = url.replace(pattern, value) + console.log("## Url: "+url) + } + if (!url || (typeof url !== 'string') || url.indexOf('local://') !== 0) { return url; } @@ -78,6 +96,7 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) { if (queryParams.length) { query = '?' + queryParams.join('&'); } + return url.replace( 'local://', req.protocol + '://' + req.headers.host + '/') + query; }; @@ -86,6 +105,7 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) { Object.keys(styleJSON_.sources).forEach(function(name) { var source = styleJSON_.sources[name]; source.url = fixUrl(source.url); + console.log("final url: "+source.url) }); // mapbox-gl-js viewer cannot handle sprite urls with query if (styleJSON_.sprite) { @@ -94,6 +114,8 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) { if (styleJSON_.glyphs) { styleJSON_.glyphs = fixUrl(styleJSON_.glyphs, false, true); } + + return res.send(styleJSON_); }); diff --git a/src/server.js b/src/server.js index 06164ee..3aa4991 100644 --- a/src/server.js +++ b/src/server.js @@ -185,6 +185,20 @@ function start(opts) { ); }); + app.get('/new-user', function(req, res){ + var userId = req.query.id; + var id = "heatmap-"+userId; + var item = { + mbtiles: "heatmap-"+userId+".mbtiles" + } + + serve_data(options, serving.data, item, id, serving.styles).then(function(sub) { + console.log("# serve data") + app.use('/data/', sub); + res.send("ok") + }); + }); + app.get('/styles.json', function(req, res, next) { var result = []; var query = req.query.key ? ('?key=' + req.query.key) : '';