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

/**
 * Create a MapRoulette Cooperative Challenge from the exact matches,
 * which suggests updating the OSM objects tags with those from Vicmap.
 * 
 * https://github.com/osmlab/maproulette3/wiki/Cooperative-Challenges
 */

// TODO need a higher level json structre
// TODO maybe these need to be grouped by block?

const fs = require('fs')
const { Transform, pipeline } = require('stream')
const ndjson = require('ndjson')
const omit = require('object.omit')
const path = require('path')

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

if (argv._.length < 2) {
  console.error("Usage: ./mr.js dist/conflate dist/mr.geojson")
  process.exit(1)
}

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

if (!fs.existsSync(path.join(sourceDirectory, 'exactMatch.geojson'))) {
  console.error(`${path.join(sourceDirectory, 'exactMatch.geojson')} not found`)
  process.exit(1)
}

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

    if (process.stdout.isTTY && sourceCount % 10000 === 0) {
      process.stdout.write(` ${sourceCount / 1000}k\r`)
    }

    const matches = feature.properties._matches.split(',')

    for (const match of matches) {
      const operation = {
        operationType: 'modifyElement',
        data: {
          id: match,
          operations: [{
            operation: 'setTags',
            data: omit(feature.properties, '_matches')
          }]
        }
      }

      this.push(operation)
    }

    callback()
  }
})

pipeline(
  fs.createReadStream(path.join(sourceDirectory, 'exactMatch.geojson')),
  ndjson.parse(),
  mr,
  ndjson.stringify(),
  fs.createWriteStream(outputFile),
  err => {
    if (err) {
      console.log(err)
      process.exit(1)
    } else {
      process.exit(0)
    }
  }
)