Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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