-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-from-xml.js
114 lines (87 loc) · 2.82 KB
/
extract-from-xml.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
'use strict';
const fs = require('fs');
const xmlstream = require('xml-object-stream');
const readStream = fs.createReadStream('germany2.osm');
const parser = xmlstream.parse(readStream);
const nodesOut = fs.createWriteStream('nodes.json');
const waysOut = fs.createWriteStream('ways.json');
const relationsOut = fs.createWriteStream('relations.json');
// <node
parser.each('node', async (node) => {
const data = {
_key: node.$.id,
lat: Number(node.$.lat),
lng: Number(node.$.lon)
};
if (node.$children) {
data.tags = node.$children.map(n => ({ k: n.$.k, v: n.$.v }));
const nonTags = node.$children.filter(n => 'tag' != n.$name);
if (nonTags.length) {
console.log(nonTags);
console.log(JSON.stringify(node, false, 4));
}
}
nodesOut.write(JSON.stringify(data));
nodesOut.write('\n');
});
// <way
parser.each('way', async (way) => {
const data = {
_key: way.$.id,
tags: way.$children.filter(n => n.$name === 'tag').map(n => n.$),
nodes: way.$children.filter(n => n.$name === 'nd').map(n => n.$.ref)
};
const other = way.$children.filter(n => (n.$name !== 'tag') && (n.$name !== 'nd'));
if (other.length) {
console.log(way);
console.log(other);
}
waysOut.write(JSON.stringify(data));
waysOut.write('\n');
});
// <relation
parser.each('relation', async (relation) => {
const data = {
_key: relation.$.id,
ways: relation.$children.filter(n => n.$.type === 'way').map(n => ({ ref: n.$.ref, role: n.$.role })),
nodes: relation.$children.filter(n => n.$.type === 'node').map(n => n.$),
tags: relation.$children.filter(n => n.$name === 'tag').map(n => n.$)
};
try {
data.admin_level = Number(data.tags.find(n => n.k === 'admin_level').v);
} catch (e) {
console.log(`No admin_level for ${data._key}`);
console.log(data.tags);
}
const subareas = relation.$children.filter(n => n.$.type === 'subarea').map(n => n.$.ref);
if (subareas.length) {
data.subareas = subareas;
}
const others = relation.$children.filter(n => {
if (n.$name === 'member') {
if ((n.$.type !== 'way') && (n.$.type !== 'node') && (n.$.type !== 'relation')) {
return true;
}
return false;
}
if (n.$name !== 'tag') {
return true;
}
return false;
});
if (others.length) {
console.log(JSON.stringify(relation, false, 4));
console.log(others);
process.exit();
}
relationsOut.write(JSON.stringify(data));
relationsOut.write('\n');
});
parser.on('error', (err) => {
console.log(err);
});
parser.on('end', () => {
nodesOut.end();
waysOut.end();
relationsOut.end();
});