Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created December 4, 2013 21:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save springmeyer/7796138 to your computer and use it in GitHub Desktop.
Save springmeyer/7796138 to your computer and use it in GitHub Desktop.
shapefile to geojson in node.js with mapnik
var geojsonStream = require('geojson-stream');
var mapnik = require('mapnik');
var fs = require('fs');
var input = 'test/data/world_merc.shp';
var output = './out.geojson';
// set up geojson output stream
var fileOut = fs.createWriteStream(output);
var geojsonOut = geojsonStream.stringify();
geojsonOut.pipe(fileOut);
// set up mapnik to read shapefile
var ds = new mapnik.Datasource({type:'shape',file:input});
var featureset = ds.featureset()
var feat = featureset.next();
// read all features
while (feat) {
geojsonOut.write(JSON.parse(feat.toJSON()));
feat = featureset.next();
}
geojsonOut.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment