Skip to content

Commit

Permalink
Adding tileserverUrl option and some minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
jperelli committed Mar 13, 2018
1 parent de0910b commit ed7426f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@ 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
==========

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)

Expand All @@ -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'
```

Expand Down
17 changes: 8 additions & 9 deletions lib/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
);
Expand Down
5 changes: 2 additions & 3 deletions lib/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -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) } )
</script>
</body>
</html>
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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": {
Expand Down
7 changes: 4 additions & 3 deletions sample-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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?");
})

});
Expand Down

0 comments on commit ed7426f

Please sign in to comment.