├── .gitignore ├── clustering.js ├── leaflet.js ├── package.json ├── readme.md └── sample-data.geojson /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /clustering.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'), 2 | L = require('./leaflet'); 3 | 4 | function cluster(zoom, callback) { 5 | callback = callback || function () {}; 6 | 7 | fs.readFile('sample-data.geojson', function (err, content) { 8 | var data = JSON.parse(content); 9 | 10 | var map = L.map('map', { 11 | center: [38.543869175876154, -92.5433349609375], 12 | zoom: zoom, 13 | maxZoom: 12 14 | }), 15 | 16 | dataLayer = L.geoJson(data), 17 | clusterLayer = new L.MarkerClusterGroup(); 18 | 19 | map.addLayer(clusterLayer); 20 | clusterLayer.addLayer(dataLayer); 21 | 22 | var features = clusterLayer._featureGroup.getLayers().map(function (cluster) { 23 | var f = cluster.toGeoJSON(); 24 | f.properties.featureCount = cluster.__parent.getChildCount(); 25 | return f; 26 | }); 27 | 28 | callback({ 29 | type: "FeatureCollection", 30 | features: features 31 | }); 32 | }); 33 | } 34 | 35 | if (require.main === module) { 36 | cluster(9, function (geojson) { 37 | process.stdout.write(JSON.stringify(geojson)); 38 | }); 39 | } else { 40 | module.exports = cluster; 41 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "server-side-leaflet", 3 | "version": "0.0.1", 4 | "description": "Leaflet coerced to run server-side with jsdom", 5 | "main": "leaflet.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/rclark/server-side-leaflet.git" 12 | }, 13 | "keywords": [ 14 | "leaflet", 15 | "maps", 16 | "geospatial" 17 | ], 18 | "author": "Vladamir Agafonkin, mostly", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/rclark/server-side-leaflet/issues" 22 | }, 23 | "dependencies": { 24 | "jsdom": "~0.8.4" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # server-side-leaflet 2 | 3 | Use [Leaflet](http://leafletjs.com/) on the server with help from [jsdom](https://github.com/tmpvar/jsdom). 4 | 5 | All this does is 6 | 7 | - spoof the `window` and DOM with jsdom, 8 | - fix the size of the map to 1024x1024, 9 | - set `L.Icon.Default.imagePath` to some nonsense, 10 | - and then let Leaflet do the rest. 11 | 12 | I concatenated [Leaflet.markercluster]() here, in order to use it to do clustering server-side. See [the example](clustering.js). --------------------------------------------------------------------------------