Last active
May 12, 2017 08:08
-
-
Save hanbyul-here/38e964581e7a84efd0da0bf1b7a12f2f to your computer and use it in GitHub Desktop.
get centroid data with turf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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