diff --git a/docs/endpoints.rst b/docs/endpoints.rst index 0672f25..0d0e85c 100644 --- a/docs/endpoints.rst +++ b/docs/endpoints.rst @@ -35,7 +35,10 @@ 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 aca8295..18f0388 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -513,6 +513,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/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 f889999..31d4c97 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) : ''; diff --git a/src/utils.js b/src/utils.js index 6335a40..15e0469 100644 --- a/src/utils.js +++ b/src/utils.js @@ -114,3 +114,57 @@ module.exports.getFontsPbf = function(allowedFonts, fontPath, names, range, fall return glyphCompose.combine(values); }); }; + +// 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