Skip to content

Instantly share code, notes, and snippets.

@veltman
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/08f0e03212230a4b5192 to your computer and use it in GitHub Desktop.
Save veltman/08f0e03212230a4b5192 to your computer and use it in GitHub Desktop.
Joining data against PLUTO
// New York City PLUTO shapefiles are pretty big.
// If you have data by BBL and want to join it
// against PLUTO shapes but you don't like QGIS
// or PostGIS, you could convert it to GeoJSON
// `ogr2ogr -f "GeoJSON -t_srs "EPSG:4326" pluto.geojson pluto.shp`
// And then stream it like this.
var fs = require("fs"),
_ = require("underscore"),
through = require("through"),
JSONStream = require("JSONStream");
// Our list to join
var dataByBBL = {
"1234567890": {
some: "data",
more: "data"
},
"1231231231": {
some: "data",
more: "data"
},
"4564564564": {
some: "data",
more: "data"
}
};
// Stream PLUTO, one feature at a time
fs.createReadStream("pluto.geojson",{encoding:"utf8"})
.pipe(JSONStream.parse("features.*"))
.pipe(through(function write(feature){
// Ignore this lot, we don't care about it
if (!dataByBBL[feature.properties.BBL]) {
return true;
}
// Stream the feature with our data added to the properties
this.queue(_.extend(feature,dataByBBL[feature.properties.BBL]));
}))
.pipe(JSONStream.stringify(
'{"type": "FeatureCollection", "features": [', //opening string
",", //separator between features
"]}" //closing string
)) // Write back out to GeoJSON
.pipe(fs.createWriteStream("joined-with-pluto.geojson"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment