aboutsummaryrefslogtreecommitdiff
path: root/test/reduceOverlap.js
blob: 68999303ed61ec204ae6d6b5946cb52676b502b7 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const test = require('tape')
const fs = require('fs')
const child_process = require('child_process')
const mktemp = require('mktemp')

function createFeature(options) {
  return {
    type: 'Feature',
    properties: {
      ...(options.flats && {'addr:flats': options.flats}),
      ...(options.unit && {'addr:unit': options.unit}),
      'addr:housenumber': options.housenumber,
      'addr:street': options.street,
      'addr:suburb': options.suburb,
      'addr:state': 'VIC',
      'addr:postcode': '0000'
    },
    geometry: options.coordinates ? {
      type: 'Point',
      coordinates: options.coordinates
    } : null
  }
}

test('reduceOverlap distinct geometries', t => {
  const inputFile = mktemp.createFileSync('/tmp/input_XXXXX.geojson')
  const outputFile = mktemp.createFileSync('/tmp/output_XXXXX.geojson')
  const expectedFile = mktemp.createFileSync('/tmp/expected_XXXXX.geojson')

  const A = createFeature({
    housenumber: '304',
    street: 'Cardigan Street',
    suburb: 'Carlton',
    coordinates: [0, 0]
  })
  const B = createFeature({
    housenumber: '304',
    street: 'Cardigan Street',
    suburb: 'Carlton',
    coordinates: [1, 1]
  })

  // both features to appear in input
  fs.appendFileSync(inputFile, JSON.stringify(A) + '\n')
  fs.appendFileSync(inputFile, JSON.stringify(B) + '\n')

  // output expected to both inputs since they aren't overlapping
  fs.appendFileSync(expectedFile, JSON.stringify(A) + '\n')
  fs.appendFileSync(expectedFile, JSON.stringify(B) + '\n')

  try {
    child_process.execSync(`./bin/reduceOverlap.js --verbose ${inputFile} ${outputFile}`)
  } catch (err) {
    t.fail(err.stdout.toString())
  }

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'same address at different location is not reduced'
  )

  fs.unlinkSync(inputFile)
  fs.unlinkSync(outputFile)
  fs.unlinkSync(expectedFile)

  t.end()
})

test('reduceOverlap matching geometries different attributes', t => {
  const inputFile = mktemp.createFileSync('/tmp/input_XXXXX.geojson')
  const outputFile = mktemp.createFileSync('/tmp/output_XXXXX.geojson')
  const expectedFile = mktemp.createFileSync('/tmp/expected_XXXXX.geojson')

  const A = createFeature({
    housenumber: '100',
    street: 'Foo Street',
    suburb: 'A',
    coordinates: [0, 0]
  })
  const B = createFeature({
    housenumber: '200',
    street: 'Bar Street',
    suburb: 'B',
    coordinates: [0, 0]
  })

  // both features to appear in input
  fs.appendFileSync(inputFile, JSON.stringify(A) + '\n')
  fs.appendFileSync(inputFile, JSON.stringify(B) + '\n')

  // output expected to retain both inputs
  fs.appendFileSync(expectedFile, JSON.stringify(A) + '\n')
  fs.appendFileSync(expectedFile, JSON.stringify(B) + '\n')

  try {
    child_process.execSync(`./bin/reduceOverlap.js --verbose ${inputFile} ${outputFile}`)
  } catch (err) {
    t.fail(err.stdout.toString())
  }

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'different address at same location is retained'
  )

  fs.unlinkSync(inputFile)
  fs.unlinkSync(outputFile)
  fs.unlinkSync(expectedFile)

  t.end()
})

test('reduceOverlap matching geometries adjoining ranges', t => {
  const inputFile = mktemp.createFileSync('/tmp/input_XXXXX.geojson')
  const outputFile = mktemp.createFileSync('/tmp/output_XXXXX.geojson')
  const expectedFile = mktemp.createFileSync('/tmp/expected_XXXXX.geojson')

  const A = createFeature({
    housenumber: '51',
    street: 'Cardigan Street',
    suburb: 'Carlton',
    coordinates: [0, 0]
  })
  const B = createFeature({
    housenumber: '53',
    street: 'Cardigan Street',
    suburb: 'Carlton',
    coordinates: [0, 0]
  })
  const C = createFeature({
    housenumber: '55-57',
    street: 'Cardigan Street',
    suburb: 'Carlton',
    coordinates: [0, 0]
  })

  const result = createFeature({
    housenumber: '51-57',
    street: 'Cardigan Street',
    suburb: 'Carlton',
    coordinates: [0, 0]
  })

  // all features to appear in input
  fs.appendFileSync(inputFile, JSON.stringify(A) + '\n')
  fs.appendFileSync(inputFile, JSON.stringify(B) + '\n')
  fs.appendFileSync(inputFile, JSON.stringify(C) + '\n')

  // output expected to join the ranges
  fs.appendFileSync(expectedFile, JSON.stringify(result) + '\n')

  try {
    child_process.execSync(`./bin/reduceOverlap.js --verbose ${inputFile} ${outputFile}`)
  } catch (err) {
    t.fail(err.stdout.toString())
  }

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'adjoining ranges at same location is merged'
  )

  fs.unlinkSync(inputFile)
  fs.unlinkSync(outputFile)
  fs.unlinkSync(expectedFile)

  t.end()
})