From ed7426f7d338f5dec461b2ae109de358c1db6efd Mon Sep 17 00:00:00 2001 From: Julian Perelli Date: Tue, 13 Mar 2018 01:40:22 -0300 Subject: [PATCH] Adding tileserverUrl option and some minor refactors --- README.md | 25 +++++++++++++------------ lib/lib.js | 17 ++++++++--------- lib/template.html | 5 ++--- package.json | 6 +++--- sample-server.js | 7 ++++--- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 4a21694..484a1ad 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,14 @@ And a [dynamic version](http://osm-static-maps.herokuapp.com/dynamic?geojson=[{" As a first approach, the service can render a geoJSON in a map, returning a PNG and you can determine also an optional height and width in pixels. -Parameters can be passed to the app as GET (POST should be working also...) +Parameters that can be used (some can be passed to the app server as GET query params) | Parameter | Description | | ---- | ---- | | geojson | geojson object to be rendered in the map | | height | height in pixels of the returned img | | width | height in pixels of the returned img | +| tileserverUrl | url of a tileserver (default is official osm: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png') | | more things | to be added soon! | How to use @@ -25,16 +26,16 @@ How to use 1. This library is published in npm you can use it as an npm module -``` --shell- -npm install osm-static-maps - --index.js- -osmsm = require('osm-static-maps'); -osmsm({geojson: geojson}) - .then(function(imageStream) { ... }) - .catch(function(error) { ... }) -``` + ``` + -shell- + npm install osm-static-maps + + -index.js- + osmsm = require('osm-static-maps'); + osmsm({geojson: geojson}) + .then(function(imageStream) { ... }) + .catch(function(error) { ... }) + ``` 2. alternatively you can download the code, run the sample server and use it standalone (see How to run) @@ -55,7 +56,7 @@ Note that you can use yarn if you like it more than npm To develop use -```` +``` npm run dev # or just 'yarn dev' ``` diff --git a/lib/lib.js b/lib/lib.js index 2203b39..41ecd13 100644 --- a/lib/lib.js +++ b/lib/lib.js @@ -15,14 +15,13 @@ var template = Handlebars.compile(fs.readFileSync(path.join(__dirname, 'template module.exports = function(options) { return new Promise(function(resolve, reject) { - var context = { - lat : -34.921779, - lng : -57.9524339, - zoom : 12, - geojson: "", - } + options.lat = options.lat || -34.921779; + options.lng = options.lng || -57.9524339; + options.zoom = options.zoom || 12; + options.geojson = options.geojson || ""; options.height = options.height || 600; options.width = options.width || 800; + options.tileserverUrl = options.tileserverUrl || 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; var html = ''; try { @@ -31,17 +30,17 @@ module.exports = function(options) { catch(e) { reject(e) } - console.log(html) webshot( html, { siteType: 'html', takeShotOnCallback: true, windowSize: { width: options.width, height: options.height }, - timeout:15000 + timeout:15000, + errorIfJSException: true }, function(err, stream) { - if (err) return reject(err); + if (err) return reject(err, html); resolve(stream); } ); diff --git a/lib/template.html b/lib/template.html index 27d5b47..916c58c 100644 --- a/lib/template.html +++ b/lib/template.html @@ -19,14 +19,13 @@ {{else}} map.setView(L.latLng({{ lat }}, {{ lng }}), {{ zoom }}); {{/if}} - var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; var osmAttrib='© OpenStreetMap contributors'; - var osm = new L.TileLayer(osmUrl, {maxZoom: 17, fadeAnimation: false}); + var osm = new L.TileLayer('{{{tileserverUrl}}}', {maxZoom: 17, fadeAnimation: false}); map.attributionControl.setPrefix('Leaflet & osm-static-maps').addAttribution( osmAttrib ); map.addLayer(osm); // make sure that is loaded before taking the screenshot - osm.on('load', function(){ setTimeout( function(){if (window.callPhantom) window.callPhantom('takeShot') }, 1000) } ) + osm.on('load', function(){ setTimeout( function(){if (window.callPhantom) window.callPhantom('takeShot') }, 1) } ) diff --git a/package.json b/package.json index 75b887b..9c428db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "osm-static-maps", - "version": "2.0.0", + "version": "2.1.0", "description": "Create a static image of a map with the features you want", "author": "Julian Perelli", "contributors": [ @@ -26,8 +26,8 @@ "npm": "1.2.x" }, "scripts": { - "start": "node server.js", - "dev": "nodemon server.js" + "start": "node sample-server.js", + "dev": "nodemon sample-server.js" }, "main": "./lib/lib.js", "devDependencies": { diff --git a/sample-server.js b/sample-server.js index 1922e20..8c08c55 100644 --- a/sample-server.js +++ b/sample-server.js @@ -50,7 +50,9 @@ app.get('/', function(req, res) { lat: context.lat, lng: context.lng, zoom: context.zoom, - geojson: context.geojson + geojson: context.geojson, + // 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' + tileserverUrl: 'http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}' }).then(function(stream) { stream.on('data', function(data) { res.write(data.toString('binary'), 'binary'); @@ -59,8 +61,7 @@ app.get('/', function(req, res) { res.end(data); }); }).catch(function(err) { - res.send(err + ". Perhaps there is an error in your parameters?"); - res.end(); + res.end(". Perhaps there is an error in your parameters?"); }) });