aboutsummaryrefslogtreecommitdiff
path: root/test/reduceRangeDuplicates.js
blob: 21c80c46e12fbd5054271594a8538b3f7226fd26 (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
172
173
174
175
176
177
178
179
180
const test = require('tape')
const fs = require('fs')
const child_process = require('child_process')
const mktemp = require('mktemp')

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

test('reduceRangeDuplicates 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 AB = createFeature([0, 0], null, '304-306', 'Cardigan Street', 'Carlton')
  const A = createFeature([-1, 0], null, '304', 'Cardigan Street', 'Carlton')
  const B = createFeature([1, 0], null, '306', 'Cardigan Street', 'Carlton')

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

  // output expected to just be endpoints, dropping the range
  fs.appendFileSync(expectedFile, JSON.stringify(A) + '\n')
  fs.appendFileSync(expectedFile, JSON.stringify(B) + '\n')

  child_process.execSync(`./bin/reduceRangeDuplicates.js --verbose ${inputFile} ${outputFile}`)

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'range with endpoints appearing separately, drops range'
  )

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

  t.end()
})

test('reduceRangeDuplicates overlapping 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 AB = createFeature([0, 0], null, '304-306', 'Cardigan Street', 'Carlton')
  const A = createFeature([0, 0], null, '304', 'Cardigan Street', 'Carlton')
  const B = createFeature([0, 0], null, '306', 'Cardigan Street', 'Carlton')

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

  // output expected to drop the endpoints and retain the range since endpoints are overlapping
  fs.appendFileSync(expectedFile, JSON.stringify(AB) + '\n')

  child_process.execSync(`./bin/reduceRangeDuplicates.js --verbose ${inputFile} ${outputFile}`)

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'range with endpoints appearing separately but overlapping, drops the endpoints'
  )

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

  t.end()
})

test('reduceRangeDuplicates', 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 AC = createFeature(null, null, '249-263', 'Faraday Street', 'Carlton')
  const B = createFeature(null, null, '251', 'Faraday Street', 'Carlton')

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

  // output expected to just be range, dropping the midpoint
  fs.appendFileSync(expectedFile, JSON.stringify(AC) + '\n')

  child_process.execSync(`./bin/reduceRangeDuplicates.js --verbose ${inputFile} ${outputFile}`)

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'range with lone midpoint, drops midpoint'
  )

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

  t.end()
})

test('reduceRangeDuplicates', 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 AC = createFeature(null, null, '249-263', 'Faraday Street', 'Carlton')
  const B = createFeature(null, '1', '251', 'Faraday Street', 'Carlton')

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

  // output expected to both features because dropping the midpoint would loose the unit
  fs.appendFileSync(expectedFile, JSON.stringify(AC) + '\n')
  fs.appendFileSync(expectedFile, JSON.stringify(B) + '\n')

  child_process.execSync(`./bin/reduceRangeDuplicates.js --verbose ${inputFile} ${outputFile}`)

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'midpoint with unit not dropped'
  )

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

  t.end()
})

test('reduceRangeDuplicates', 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 AC = createFeature(null, null, '249-263', 'Faraday Street', 'Carlton')
  const B = createFeature(null, null, '251', 'Faraday Street', 'Carlton', '1;2;3')

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

  // output expected to both features because dropping the midpoint would loose the flats
  fs.appendFileSync(expectedFile, JSON.stringify(AC) + '\n')
  fs.appendFileSync(expectedFile, JSON.stringify(B) + '\n')

  child_process.execSync(`./bin/reduceRangeDuplicates.js --verbose ${inputFile} ${outputFile}`)

  t.same(
    fs.readFileSync(outputFile, 'utf-8').trim().split('\n').map(JSON.parse),
    fs.readFileSync(expectedFile, 'utf-8').trim().split('\n').map(JSON.parse),
    'midpoint with flats not dropped'
  )

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

  t.end()
})