-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr2osc.mjs
executable file
·437 lines (391 loc) · 12.4 KB
/
mr2osc.mjs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env node
/**
* Given a MapRoulette Cooperative Challenge, upload all the changes directly without review
*/
import fs from 'fs'
import { pipeline } from 'stream/promises'
import { Transform } from 'stream'
import ndjson from 'ndjson'
import _ from 'lodash'
import fetch from 'node-fetch'
import xml from 'xml-js'
import yargs from 'yargs'
import path from 'path'
const argv = yargs(process.argv.slice(2))
.option('dry-run', {
type: 'boolean',
description: 'Skip uploading to OSM, instead log what we would do'
})
.option('changeset-comment', {
type: 'string',
description: 'OSM Changeset comment',
demandOption: true
})
.argv
if (argv._.length < 2) {
console.error("Usage: ./mr2osc.js mr.json change.osc")
process.exit(1)
}
const mrFile = argv._[0]
const changeFile = argv._[1]
if (!fs.existsSync(mrFile)) {
console.error(`${mrFile} not found`)
process.exit(1)
}
if (argv.dryRun) {
console.log('Dry run enabled')
}
// https://wiki.openstreetmap.org/wiki/API_v0.6#Multi_fetch:_GET_.2Fapi.2F0.6.2F.5Bnodes.7Cways.7Crelations.5D.3F.23parameters
const MAXIMUM_ELEMENTS_PER_GET_REQUEST = 725
const OSM_API_READ = 'https://api.openstreetmap.org'
const OSM_API_WRITE = (process.env.ENVIRONMENT === 'prod') ? 'https://api.openstreetmap.org' : 'https://master.apis.dev.openstreetmap.org'
const USER_AGENT = 'vicmap2osm/1.0 (+https://gitlab.com/alantgeo/vicmap2osm)'
const CREATED_BY = 'https://gitlab.com/alantgeo/vicmap2osm'
const AUTHORIZATION = `Basic ${Buffer.from(process.env.OSM_USERNAME + ':' + process.env.OSM_PASSWORD).toString('base64')}`
let MAXIMUM_ELEMENTS_PER_UPLOAD_REQUEST = 10000
const changesetTags = {
created_by: CREATED_BY,
comment: argv.changesetComment,
source: 'Vicmap Address',
'source:ref': 'https://www.land.vic.gov.au/maps-and-spatial/spatial-data/vicmap-catalogue/vicmap-address'
}
console.log(`Retrieving capabilities from ${OSM_API_WRITE}`)
await fetch(`${OSM_API_WRITE}/api/capabilities`, {
headers: {
'User-Agent': USER_AGENT
}
})
.then(res => res.text())
.then(text => {
const capabilities = JSON.parse(xml.xml2json(text, {
compact: true
}))
const apiMaximumElements = capabilities.osm.api.changesets._attributes.maximum_elements
MAXIMUM_ELEMENTS_PER_UPLOAD_REQUEST = Math.min(MAXIMUM_ELEMENTS_PER_UPLOAD_REQUEST, apiMaximumElements)
})
const operations = {
node: {},
way: {},
relation: {}
}
// full OSM Elements live from OSM API
const elements = {}
const changes = {
node: [],
way: [],
relation: []
}
let taskCount = 0
/**
* Transform which receives MapRoulette tasks and
* stores operations by Element type by id in `operations`.
*/
const listElements = new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform(task, _encoding, callback) {
taskCount++
if (process.stdout.isTTY && taskCount % 1000 === 0) {
process.stdout.write(` ${taskCount.toLocaleString()}\r`)
}
if (task && task.features && task.features.length && task.cooperativeWork && task.cooperativeWork.operations && task.cooperativeWork.operations.length) {
const operation = task.cooperativeWork.operations[0]
if (operation.operationType === 'modifyElement') {
const type = operation.data.id.split('/')[0]
const id = operation.data.id.split('/')[1]
operations[type][id] = operation.data.operations
}
}
callback()
}
})
/**
* Fetch all Elements with operations from `operations`
* so we have the full Element to generate the OsmChange.
* Results stored in `elements`.
*/
async function fetchElements() {
/*
const nodeChunks = _.chunk(Object.keys(operations.nodes), MAXIMUM_ELEMENTS_PER_GET_REQUEST)
const wayChunks = _.chunk(Object.keys(operations.ways), MAXIMUM_ELEMENTS_PER_GET_REQUEST)
const relationChunks = _.chunk(Object.keys(operations.relations), MAXIMUM_ELEMENTS_PER_GET_REQUEST)
*/
for (const type in operations) {
let index = 0
const chunks = _.chunk(Object.keys(operations[type]), MAXIMUM_ELEMENTS_PER_GET_REQUEST)
for (const chunk of chunks) {
index++
process.stdout.write(` Fetch ${type}s (${index}/${chunks.length})\r`)
await fetch(`${OSM_API_READ}/api/0.6/${type}s?${type}s=${chunk.join(',')}`, {
headers: {
'Accept': 'application/json',
'User-Agent': USER_AGENT
}
})
.then(res => res.json())
.then(json => {
json.elements.forEach(element => {
elements[`${element.type}/${element.id}`] = element
})
})
.catch(err => {
console.error(err)
process.exit(1)
})
}
process.stdout.write(`\r`)
process.stdout.write(` Fetched ${chunks.length} ${type}s\n`)
}
return Promise.resolve()
/*
return [
...nodeChunks.map(nodeChunk => {
return fetch(`${OSM_API_READ}/api/0.6/nodes?nodes=${nodeChunk.join(',')}`, {
headers: {
'Accept': 'application/json',
'User-Agent': USER_AGENT
}
})
.then(res => res.json())
.then(json => {
json.elements.forEach(element => {
elements[`${element.type}/${element.id}`] = element
})
})
}),
...wayChunks.map(wayChunk => {
return fetch(`${OSM_API_READ}/api/0.6/ways?ways=${wayChunk.join(',')}`, {
headers: {
'Accept': 'application/json',
'User-Agent': USER_AGENT
}
})
.then(res => res.json())
.then(json => {
json.elements.forEach(element => {
elements[`${element.type}/${element.id}`] = element
})
})
}),
...relationChunks.map(relationChunk => {
return fetch(`${OSM_API_READ}/api/0.6/relations?relations=${relationChunk.join(',')}`, {
headers: {
'Accept': 'application/json',
'User-Agent': USER_AGENT
}
})
.then(res => res.json())
.then(json => {
json.elements.forEach(element => {
elements[`${element.type}/${element.id}`] = element
})
})
})
])
*/
}
function createChanges() {
for (const type in operations) {
for (const [id, ops] of Object.entries(operations[type])) {
const element = elements[`${type}/${id}`]
const tags = []
if (ops && ops.length) {
ops.forEach(operation => {
if (operation.operation === 'setTags') {
// first ensure that our assumptions about the current values of these tags hasn't changed
// TODO
// tags to set
for (const [key, value] of Object.entries(operation.data)) {
// replace with new tag
element.tags[key] = value
}
}
})
}
// set nodeChanges for this node
for (const [key, value] of Object.entries(element.tags)) {
const tag = {
_attributes: {
k: key,
v: value
}
}
tags.push(tag)
}
switch (type) {
case 'node':
changes[type].push({
_attributes: {
id: id,
version: element.version + 1,
lat: element.lat,
lon: element.lon
},
tag: tags
})
break
case 'way':
changes[type].push({
_attributes: {
id: id,
version: element.version + 1,
},
tag: tags,
nodes: element.nodes
})
break
case 'relation':
changes[type].push({
_attributes: {
id: id,
version: element.version + 1,
},
tag: tags,
members: element.members
})
break
}
}
}
}
async function uploadChanges() {
// now we have the latest full elements, apply our changes as an OsmChange
// https://wiki.openstreetmap.org/wiki/API_v0.6#Diff_upload:_POST_.2Fapi.2F0.6.2Fchangeset.2F.23id.2Fupload
const totalElements = Object.values(changes).flat().length
const totalChangesets = Math.ceil(totalElements / MAXIMUM_ELEMENTS_PER_UPLOAD_REQUEST)
if (totalChangesets > 1) {
console.log(`${totalElements} exceeds API maximum elements of ${MAXIMUM_ELEMENTS_PER_UPLOAD_REQUEST} splitting into ${totalChangesets} changesets`)
}
for (let changesetIndex = 0; changesetIndex < totalChangesets; changesetIndex++) {
// create a changeset
let changesetId
const createBody = xml.json2xml({
_declaration: {
_attributes: {
version: "1.0",
encoding: "UTF-8"
}
},
osm: {
changeset: {
tag: Object.keys(changesetTags).map(key => {
return {
_attributes: {
k: key,
v: changesetTags[key]
}
}
})
}
}
}, Object.assign({compact: true}, argv.dryRun ? { spaces: 2 } : {}))
if (argv.dryRun) {
console.log(`${OSM_API_WRITE}/api/0.6/changeset/create`)
console.log(createBody)
} else {
changesetId = await fetch(`${OSM_API_WRITE}/api/0.6/changeset/create`, {
method: 'PUT',
headers: {
'Authorization': AUTHORIZATION,
'User-Agent': USER_AGENT
},
body: createBody
})
.then(res => res.text())
console.log(`Opened changeset ${changesetId}`)
}
/*
const nodeChangesCount = changes.node.length
const wayChangesCount = changes.node.length
const relationChangesCount = changes.node.length
const startIndex = changeIndex * MAXIMUM_ELEMENTS_PER_UPLOAD_REQUEST
const endIndex = ((changeIndex + 1) * MAXIMUM_ELEMENTS_PER_UPLOAD_REQUEST) - 1
if (startIndex < nodeChangesCount) {
changes.node.slice(startIndex, )
}
*/
// upload to the changeset
const uploadBody = xml.json2xml({
_declaration: {
_attributes: {
version: "1.0",
encoding: "UTF-8"
}
},
osmChange: {
_attributes: {
version: '0.6',
generator: CREATED_BY
},
modify: {
node: changes.node.map(change => insertChangesetId(change, changesetId)),
way: changes.way.map(change => insertChangesetId(change, changesetId)),
relation: changes.relation.map(change => insertChangesetId(change, changesetId))
}
}
}, Object.assign({
compact: true,
attributeValueFn: value => {
// these values were tested with test/xmlEntities.js
return value.replace(/"/g, '"') // convert quote back before converting amp
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
}
}, argv.dryRun ? { spaces: 2 } : {}))
const filePath = totalChangesets > 1 ? path.join(path.dirname(changeFile), path.basename(changeFile, '.osc') + '-' + (changesetIndex + 1) + '.osc') : changeFile
fs.writeFileSync(filePath, uploadBody)
console.log(`Saved ${filePath}`)
if (argv.dryRun) {
console.log(`${OSM_API_WRITE}/api/0.6/changeset/${changesetId}/upload`)
} else {
await fetch(`${OSM_API_WRITE}/api/0.6/changeset/${changesetId}/upload`, {
method: 'POST',
headers: {
'Authorization': AUTHORIZATION,
'User-Agent': USER_AGENT
},
body: uploadBody
})
console.log(`Uploaded contents to ${changesetId}`)
}
// close the changeset
if (argv.dryRun) {
console.log(`${OSM_API_WRITE}/api/0.6/changeset/${changesetId}/close`)
} else {
await fetch(`${OSM_API_WRITE}/api/0.6/changeset/${changesetId}/close`, {
method: 'PUT',
headers: {
'Authorization': AUTHORIZATION,
'User-Agent': USER_AGENT
}
})
console.log(`Closed changeset ${changesetId}`)
}
}
return
}
function insertChangesetId(change, changesetId) {
change._attributes.changeset = changesetId
return change
}
console.log('Step 1/4: Find Elements to modify')
async function run() {
await pipeline(
fs.createReadStream(mrFile),
ndjson.parse(),
listElements,
)
for (const [type, value] of Object.entries(operations)) {
console.log(` ${type}s ${Object.keys(value).length}`)
}
console.log('Step 2/4: Fetch latest Elements to modify from OSM API')
await fetchElements()
console.log('Step 3/4: Create changes')
createChanges()
console.log('Step 4/4: Upload changes')
await uploadChanges()
console.log('Success')
}
run().catch(console.error)