-
Notifications
You must be signed in to change notification settings - Fork 0
/
building.js
executable file
·199 lines (179 loc) · 6.08 KB
/
building.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env node
/**
* Take Vicmap address points which have a building name,
* then conflate with existing OSM names
*/
const fs = require('fs')
const { Transform, pipeline } = require('readable-stream')
const ndjson = require('ndjson')
const point = require('@turf/helpers').point
const { capitalCase } = require('capital-case')
const Flatbush = require('flatbush')
const bbox = require('@turf/bbox').default
const { around } = require('geoflatbush')
const { lcs } = require('string-comparison')
const argv = require('yargs/yargs')(process.argv.slice(2))
.argv
if (argv._.length < 3) {
console.error("Usage: ./building.js vicmap-building.geojson victoria-named-features.osm.geojson vicmap-building-conflation")
process.exit(1)
}
const inputFile = argv._[0]
const osmFile = argv._[1]
const outputPath = argv._[2]
if (!fs.existsSync(inputFile)) {
console.error(`${inputFile} not found`)
process.exit(1)
}
console.log('Reading OSM data')
const osmFeatures = fs.readFileSync(osmFile, 'utf-8').toString().split('\n')
.filter(line => line !== '')
.map((line, index, array) => {
if (process.stdout.isTTY && index % 1000 === 0) {
process.stdout.write(` ${index.toLocaleString()}/${array.length.toLocaleString()} (${Math.round(index / array.length * 100)}%)\r`)
}
try {
const feature = JSON.parse(line)
feature.properties.id = index
return feature
} catch {
console.log(`Error parsing line ${index} of ${osmFile}: ${line}`)
}
})
console.log('Creating index for nearby OSM search')
const osmIndex = new Flatbush(osmFeatures.length)
for (const osmFeature of osmFeatures) {
osmIndex.add(...bbox(osmFeature))
}
osmIndex.finish()
// ndjson streams to output features
const outputKeys = [
// MapRoulette challenges
'mr_singleNearbySimilarFeature',
'mr_multipleNearbySimilarFeatures',
'mr_noNearbySimilarFeature'
]
const outputStreams = {}
const outputStreamOutputs = {}
outputKeys.forEach(key => {
outputStreams[key] = ndjson.stringify()
outputStreamOutputs[key] = outputStreams[key].pipe(fs.createWriteStream(`${outputPath}/${key}.geojson`))
})
let sourceCount = 0
let exactMatchCount = 0
const conflate = new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform(feature, encoding, callback) {
sourceCount++
if (!argv.quiet) {
if (process.stdout.isTTY && sourceCount % 100 === 0) {
process.stdout.write(` ${sourceCount.toLocaleString()}\r`)
}
}
const name = feature.properties.name
const properties = {
name: capitalCase(name)
}
// find nearby matching OSM feature
const maxDistanceInKm = 1
const nearby = around(osmIndex, ...feature.geometry.coordinates, Infinity, maxDistanceInKm)
const nearbyMatches = nearby.filter(i => {
const similarity = lcs.similarity(osmFeatures[i].properties.name.toLowerCase(), name.toLowerCase())
return similarity > 0.8
})
const nearbyMatchedFeatures = nearbyMatches.map(i => osmFeatures[i])
/* TODO log to file
if (nearbyMatches.length) {
console.log(name)
console.log(' > ', nearbyMatches.map(i => osmFeatures[i].properties.name))
}
*/
if (nearbyMatches.length === 1) {
// a single nearby OSM features found with similar name
if (nearbyMatchedFeatures[0].properties.name.toLowerCase() === name.toLowerCase()) {
// name exactly matched
exactMatchCount++
} else {
// name was similar but not an exact match
// create a MapRoulette task to investigate further
const task = {
type: 'FeatureCollection',
features: [
...nearbyMatchedFeatures.map(f => {
// MapRoulette uses the first feature id as the task ID
f.id = feature.id
return f
}),
// last feature is used as the first one shown in MapRoulette
point(feature.geometry.coordinates, Object.assign({}, feature.properties, {
'marker-color': 'orange',
'marker-size': 'large',
'OSM Name': nearbyMatchedFeatures[0].properties.name
}, properties))
]
}
outputStreams.mr_singleNearbySimilarFeature.write(task)
}
} else if (nearbyMatches.length > 1) {
// multiple nearby OSM features found with similar name, create a MapRoulette task to investigate further
const task = {
type: 'FeatureCollection',
features: [
...nearbyMatchedFeatures.map(f => {
// MapRoulette uses the first feature id as the task ID
f.id = feature.id
return f
}),
// last feature is used as the first one shown in MapRoulette
point(feature.geometry.coordinates, Object.assign({}, feature.properties, {
'marker-color': 'orange',
'marker-size': 'large'
}, properties))
]
}
outputStreams.mr_multipleNearbySimilarFeatures.write(task)
} else {
// no nearby OSM feature found with similar name, so create a MapRoulette task
const task = {
type: 'FeatureCollection',
features: [
point(feature.geometry.coordinates, Object.assign({}, feature.properties, properties), {
// MapRoulette uses the first feature id as the task ID
id: feature.id
})
]
}
outputStreams.mr_noNearbySimilarFeature.write(task)
}
callback()
}
})
console.log('Stage 1/1 reading Vicmap building points')
pipeline(
fs.createReadStream(inputFile),
ndjson.parse(),
conflate,
(err) => {
if (err) {
console.log(err)
process.exit(1)
} else {
console.log(`${exactMatchCount} Vicmap building names exactly matched OSM features`)
outputKeys.forEach(key => {
outputStreams[key].end()
})
Promise.all(outputKeys.map(key => {
return new Promise(resolve => {
outputStreamOutputs[key].on('finish', () => {
console.log(`saved ${outputPath}/${key}.geojson`)
resolve()
})
})
}))
.then(() => {
process.exit(0)
})
}
}
)