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

/**
 * Report features which overlap
 */

const fs = require('fs')
const { Readable, 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: ./reportOverlap.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)
}

let sourceCount = 0
const features = {}

/**
 * Index features by geometry. Used as a first pass, so a second pass can easily compare
 * features with the same geometry.
 */
const index = new Transform({
  readableObjectMode: true,
  writableObjectMode: true,
  transform(feature, encoding, callback) {
    sourceCount++

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

    const geometryKey = feature.geometry.coordinates.join(',')

    if (!(geometryKey in features)) {
      features[geometryKey] = []
    }
    features[geometryKey].push(feature)

    callback()
  }
})

let totalFeaturesWhichOverlap = 0
let countGroupsOfOverlaps = 0

/**
 * Report features with the same geometry.
 */
let featureIndex = 0
const reportOverlap = new Transform({
  readableObjectMode: true,
  writableObjectMode: true,
  transform(key, encoding, callback) {
    featureIndex++
    if (!argv.quiet) {
      if (process.stdout.isTTY && featureIndex % 10000 === 0) {
        process.stdout.write(` ${featureIndex.toLocaleString()} / ${sourceCount.toLocaleString()} (${Math.round(featureIndex / sourceCount * 100)}%)\r`)
      }
    }

    const sharedGeometry = features[key]

    if (sharedGeometry.length === 1) {
      // only one feature with this geometry
    } else {
      totalFeaturesWhichOverlap += sharedGeometry.length
      countGroupsOfOverlaps++
      this.push({
        type: 'Feature',
        properties: {
          count: sharedGeometry.length
        },
        geometry: sharedGeometry[0].geometry
      })
    }

    callback()
  }
})

// first pass to index by geometry
console.log('Pass 1/2: index by geometry')
pipeline(
  fs.createReadStream(inputFile),
  ndjson.parse(),
  index,
  err => {
    if (err) {
      console.log(err)
      process.exit(1)
    } else {
      console.log(`  of ${sourceCount.toLocaleString()} features found ${Object.keys(features).length.toLocaleString()} unique geometries`)
      // second pass to report overlapping features
      console.log('Pass 2/2: report overlapping features')
      pipeline(
        Readable.from(Object.keys(features)),
        reportOverlap,
        ndjson.stringify(),
        fs.createWriteStream(outputFile),
        err => {
          if (err) {
            console.log(err)
            process.exit(1)
          } else {
            console.log(`Total overlapping features: ${totalFeaturesWhichOverlap}`)
            console.log(`Locations with overlapping features: ${countGroupsOfOverlaps}`)
            process.exit(0)
          }
        }
      )
    }
  }
)