Skip to content

Instantly share code, notes, and snippets.

@waleedshkt
Forked from springmeyer/shp2geojson.js
Created May 13, 2020 08:14
Show Gist options
  • Save waleedshkt/3eba501d4207a23c9db2e820318b727b to your computer and use it in GitHub Desktop.
Save waleedshkt/3eba501d4207a23c9db2e820318b727b 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