Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.