Created
September 20, 2016 10:01
Star
You must be signed in to star a gist
WOF post code script
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 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