aboutsummaryrefslogtreecommitdiff
path: root/bin/findAbbrStreets.js
blob: 095fa8023751e3dd90fca0524ea7079e5b641150 (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
#!/usr/bin/env node

/**
 * Find OSM streets with abbreviated street types
 */

const fs = require('fs')
const { Transform, pipeline } = require('stream')
const ndjson = require('ndjson')

const argv = require('yargs/yargs')(process.argv.slice(2))
  .argv

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

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

const abbreviatedTypes = ['rd', 'st', 'ave', 'av', 'ln', 'cl', 'hwy', 'pl']
const streetTypes = {
  'rd': 'Road',
  'st': 'Street',
  'ave': 'Avenue',
  'av': 'Avenue',
  'ln': 'Lane',
  'hwy': 'Highway',
  'pl': 'Place',
  'fwy': 'Freeway',
  'pde': 'Parade',
  'dr': 'Drive',
  'cr': 'Crescent',
  'cl': 'Close',
  'ct': 'Court'
}

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

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

    if (process.stdout.isTTY && index % 10000 === 0) {
      process.stdout.write(` ${index.toLocaleString()}\r`)
    }

    if ('addr:street' in feature.properties) {
      const street = feature.properties['addr:street'].toLowerCase()
      const streetEndsWithAbbr = abbreviatedTypes.map(ab => street.endsWith(` ${ab}`)).reduce((acc, cur) => acc || cur)
      if (streetEndsWithAbbr) {
        const matches = street.match(/ (rd|st|ave|av|ln|cl|hwy|pl|fwy|pde|dr|cr|cl|ct)$/)
        if (matches.length) {
          const ab = matches[1]
          const expandedStreetType = streetTypes[ab]
          const fullStreetName = feature.properties['addr:street'].replace(/ (rd|st|ave|av|ln|cl|hwy|pl|fwy|pde|dr|cr|cl|ct)$/i, ` ${expandedStreetType}`)

          // MapRoulette task
          const task = {
            type: 'FeatureCollection',
            features: [ feature ],
            cooperativeWork: {
              meta: {
                version: 2,
                type: 1 // tag fix type
              },
              operations: [{
                operationType: 'modifyElement',
                data: {
                  id: `${feature.properties['@type']}/${feature.properties['@id']}`,
                  operations: [{
                    operation: 'setTags',
                    data: {
                      'addr:street': fullStreetName
                    }
                  }]
                }
              }]
            }
          }
          tasks++
          this.push(task)
        }
      }
    }

    callback()
  }
})

pipeline(
  fs.createReadStream(inputFile),
  ndjson.parse(),
  checkStreet,
  ndjson.stringify(),
  fs.createWriteStream(outputFile),
  err => {
    if (err) {
      console.log(err)
      process.exit(1)
    } else {
      console.log(`${tasks} tasks`)
      process.exit(0)
    }
  }
)