diff options
author | Andrew Harvey <andrew@alantgeo.com.au> | 2021-05-21 16:05:04 +1000 |
---|---|---|
committer | Andrew Harvey <andrew@alantgeo.com.au> | 2021-05-21 16:05:04 +1000 |
commit | a4a86859e7b373c9a68a5503e47511c2e8537d72 (patch) | |
tree | ec5ed2a68d530ce5ec514a8bb4fa9f77e8f0ff15 /bin/compareSuburb.js | |
parent | 9cc8b035da93c7e6bdf92735c3f671081a40fc3e (diff) |
report postcodes per osm suburb
Diffstat (limited to 'bin/compareSuburb.js')
-rwxr-xr-x | bin/compareSuburb.js | 67 |
1 files changed, 61 insertions, 6 deletions
diff --git a/bin/compareSuburb.js b/bin/compareSuburb.js index c849735..e04b993 100755 --- a/bin/compareSuburb.js +++ b/bin/compareSuburb.js @@ -1,7 +1,10 @@ #!/usr/bin/env node /** - * Compare the addr:suburb reported from Vicmap which the corresponding suburb/locality boundary existing in OSM + * Compare the addr:suburb reported from Vicmap with the corresponding suburb/locality boundary existing in OSM + * to report any Vicmap addresses where addr:suburb conflicts with OSM's boundaries + * + * For each OSM suburb/locality, report the distribution of Vicmap addr:postcode's falling with the boundary. */ const fs = require('fs') @@ -16,14 +19,15 @@ const argv = require('yargs/yargs')(process.argv.slice(2)) }) .argv -if (argv._.length < 3) { - console.error("Usage: ./compareSuburb.js vicmap-osm.geojson osm_admin_level_10.geojson output.geojson") +if (argv._.length < 4) { + console.error("Usage: ./compareSuburb.js vicmap-osm.geojson osm_admin_level_10.geojson dist/vicmapSuburbDiffersWithOSM.geojson dist/suburbsWithPostcodeCounts.geojson") process.exit(1) } const vicmapFile = argv._[0] const osmFile = argv._[1] const outputFile = argv._[2] +const postcodeOutputFile = argv._[3] if (!fs.existsSync(vicmapFile)) { console.error(`${vicmapFile} not found`) @@ -38,7 +42,9 @@ const osmFeatures = fs.readFileSync(osmFile, 'utf-8').toString().split('\n') .filter(line => line !== '') .map((line, index) => { try { - return JSON.parse(line) + const feature = JSON.parse(line) + feature.properties.id = index + return feature } catch { console.log(`Error parsing line ${index} of ${osmFile}: ${line}`) } @@ -50,7 +56,18 @@ const lookup = new PolygonLookup({ features: osmFeatures }) -// conflate vicmap addresses with OSM addresses +// postcode counts by OSM admin boundary id +/* +{ + 0: { + '3000': address count + } +} +*/ +const postcodes = {} + +// compare vicmap addr:suburb with OSM boundary +// report vicmap addr:postcode by OSM boundary let sourceCount = 0 const compare = new Transform({ readableObjectMode: true, @@ -73,6 +90,17 @@ const compare = new Transform({ feature.properties._osmSuburb = osmFeature.properties['name'] this.push(feature) } + + // postcodes by OSM suburb + const postcode = feature.properties['addr:postcode'] + if (!(osmFeature.properties.id in postcodes)) { + postcodes[osmFeature.properties.id] = {} + } + if (!(postcode in postcodes[osmFeature.properties.id])) { + postcodes[osmFeature.properties.id][postcode] = 0 + } + + postcodes[osmFeature.properties.id][postcode] += 1 } else { // address not found within any OSM suburb // console.log('Not found within any OSM suburb', feature) @@ -95,7 +123,34 @@ pipeline( console.log(err) process.exit(1) } else { - process.exit(0) + const suburbsWithPostcodeCountsStream = ndjson.stringify() + const suburbsWithPostcodeCountsOutput = suburbsWithPostcodeCountsStream.pipe(fs.createWriteStream(postcodeOutputFile)) + osmFeatures.forEach((feature, index) => { + const postcodesFoundInThisSuburb = index in postcodes ? Object.keys(postcodes[index]) : {} + const postcodeCounts = [] + for (const [postcode, count] of Object.entries(index in postcodes ? postcodes[index] : {})) { + postcodeCounts.push({ + postcode, + count + }) + } + const sortedPostcodeCounts = postcodeCounts.sort((a, b) => b.count - a.count) + + feature.properties._distinctPostcodes = postcodesFoundInThisSuburb.length + + sortedPostcodeCounts.forEach((postcodeCount, index) => { + feature.properties[`_postcode_${index + 1}`] = postcodeCount.postcode + feature.properties[`_postcode_count_${index + 1}`] = postcodeCount.count + }) + + suburbsWithPostcodeCountsStream.write(feature) + }) + suburbsWithPostcodeCountsStream.end() + + suburbsWithPostcodeCountsOutput.on('finish', () => { + console.log(`saved ${postcodeOutputFile}`) + process.exit(0) + }) } } ) |