Skip to content

Instantly share code, notes, and snippets.

@rowanwins
Created September 20, 2016 10:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rowanwins/058a783c86e5b68972f7c5d6a17d7734 to your computer and use it in GitHub Desktop.
WOF post code script
var shp = require('shpjs');
var walk = require('walk')
var fs = require('fs')
var turfArea = require('turf-area')
var turfBbox = require('turf-bbox')
var turfCentroid = require('turf-centroid')
var newSrcData = fs.readFileSync(__dirname +"/src/ABS_Postcodes.geojson", 'utf8');
newSrcData = JSON.parse(newSrcData);
var walker = walk.walk(__dirname + "/wof-postcodes-au");
walker.on("file", function (root, fileStats, next) {
if (fileStats.name.includes('geojson')){
var currentWOF = fs.readFileSync(root + '\\' + fileStats.name, 'utf8')
var originalGeojson = JSON.parse(currentWOF);
var match = findMatchingPoly(originalGeojson.properties['wof:name'])
if (match === null){
next();
}
else {
originalGeojson.geometry = match.geometry
originalGeojson.geometry = match.geometry
originalGeojson.properties.area_square_m = turfArea(match)
var newBBox = turfBbox(match)
originalGeojson.properties.bbox = newBBox
originalGeojson.bbox = newBBox
var newCentroid = turfCentroid(match)
originalGeojson.properties['geom:latitude'] = newCentroid.geometry.coordinates[0][1]
originalGeojson.properties['geom:longitude'] = newCentroid.geometry.coordinates[0][0]
console.log(fileStats.name + " edited")
fs.writeFileSync(root + '\\' + fileStats.name, JSON.stringify(originalGeojson, null, 2))
}
}
next();
});
function findMatchingPoly(postcode) {
var matchingFeatures = newSrcData.features.filter(function(item){
return item.properties['POA_CODE'] == postcode;
})
if(matchingFeatures.length > 0){
return matchingFeatures[0]
}
else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment