Skip to content

Instantly share code, notes, and snippets.

@hanbyul-here
Last active May 12, 2017 08:08
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 hanbyul-here/38e964581e7a84efd0da0bf1b7a12f2f to your computer and use it in GitHub Desktop.
Save hanbyul-here/38e964581e7a84efd0da0bf1b7a12f2f to your computer and use it in GitHub Desktop.
get centroid data with turf.js
var turf = require('turf');
var fs = require('fs');
var jsonfile = require('jsonfile');
var seoulBuildings = JSON.parse(fs.readFileSync('./processed-data/buildings2345.geojson'));
var seoulBuildingsCentroids = [];
for (var i = 0, len = seoulBuildings.features.length; i < len; i++) {
var centroid = turf.centroid(seoulBuildings.features[i]);
if(centroid.geometry.coordinates[0]) {
centroid.geometry.coordinates[0] = parseFloat(centroid.geometry.coordinates[0].toFixed(5));
centroid.geometry.coordinates[1] = parseFloat(centroid.geometry.coordinates[1].toFixed(5));
centroid.properties = {};
/* We are throwing all other properties away than the date of approval to make the data lighter */
centroid.properties.year = seoulBuildings.features[i].properties.year;
seoulBuildingsCentroids.push(centroid);
}
}
var objectToWrite = {
"type":"FeatureCollection",
"features": seoulBuildingsCentroids
}
jsonfile.writeFileSync('./seoulBuildingsCentroids2345.geojson', objectToWrite);
console.log('saved seoulBuildingsCentroids2345.geojson');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment