aboutsummaryrefslogtreecommitdiff
path: root/bin/vicmap2osm.js
blob: f637fd49c29e8d2f09e6decd001a9fe2f37c7053 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node

/**
 * Convert from Vicmap Address schema into OSM Address schema, and omit some addresses
 */

const fs = require('fs')
const { Transform, pipeline } = require('readable-stream')
const ndjson = require('ndjson')
const toOSM = require('../lib/toOSM.js')
const filterOSM = require('../lib/filterOSM.js')
const filterSource = require('../lib/filterSource.js')

const argv = require('yargs/yargs')(process.argv.slice(2))
  .option('debug', {
    type: 'boolean',
    description: 'Dumps full debug logs'
  })
  .option('tracing', {
    type: 'boolean',
    description: 'Includes _pfi tags to aid debugging'
  })
  .option('preserve-derivable-properties', {
    type: 'boolean',
    default: false,
    description: 'If set, preserves addr:suburb, addr:postcode, addr:state, otherwise omits them'
  })
  .demandCommand(2)
  .argv

if (argv._.length < 2) {
  console.error("Usage: ./vicmap2osm.js input.geojson output.geojson")
  process.exit(1)
}

const inputFile = argv._[0]
const outputFile = argv._[1]

if (!fs.existsSync(inputFile)) {
  console.error(`${inputFile} not found`)
  process.exit(1)
}

// output Vicmap complex name data
const complexStream = ndjson.stringify()
const complexStreamOutput = complexStream.pipe(fs.createWriteStream(`dist/vicmap-complex.geojson`))

// output Vicmap building name data
const buildingStream = ndjson.stringify()
const buildingStreamOutput = buildingStream.pipe(fs.createWriteStream(`dist/vicmap-building.geojson`))

let sourceCount = 0
const transform = new Transform({
  readableObjectMode: true,
  writableObjectMode: true,
  transform(feature, encoding, callback) {
    sourceCount++

    if (!argv.quiet) {
      if (process.stdout.isTTY && sourceCount % 1000 === 0) {
        process.stdout.write(` ${sourceCount.toLocaleString()}\r`)
      }
    }

    if (feature.properties.COMPLEX) {
      const complexFeature = {
        type: 'Feature',
        id: `${feature.properties.PFI}`,
        properties: {
          name: feature.properties.COMPLEX
        },
        geometry: feature.geometry
      }
      complexStream.write(complexFeature)
    }

    // convert source Feature into a Feature per the OSM schema
    const osm = toOSM(feature, {
      tracing: argv.tracing,
      /* omit addr:suburb, addr:postcode, addr:state */
      includeDerivableProperties: argv.preserveDerivableProperties
    })

    if (feature.properties.BUILDING) {
      const buildingFeature = {
        type: 'Feature',
        id: `${feature.properties.PFI}`,
        properties: Object.assign({}, osm.properties, {
          name: feature.properties.BUILDING
        }),
        geometry: osm.geometry
      }
      buildingStream.write(buildingFeature)
    }

    // some addresses we skip importing into OSM, see README.md#omitted-addresses
    if (filterOSM(osm) && filterSource(feature)) {
      this.push(osm)
    }

    callback()
  }
})

// stream in source ndjson, transfom and stream out
pipeline(
  fs.createReadStream(inputFile),
  ndjson.parse(),
  transform,
  ndjson.stringify(),
  fs.createWriteStream(outputFile),
  (err) => {
    if (err) {
      console.log(err)
      process.exit(1)
    } else {
      complexStream.end()
      buildingStream.end()
      complexStreamOutput.on('finish', () => {
        console.log(`saved dist/vicmap-complex.geojson`)
        buildingStreamOutput.on('finish', () => {
          console.log(`saved dist/vicmap-building.geojson`)
          process.exit(0)
        })
      })
    }
  }
)