-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_extract-coordinates.js
50 lines (42 loc) · 1.54 KB
/
02_extract-coordinates.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
const osmread = require('osm-read');
const fs = require('fs');
const GeoJSON = require('geojson');
const nodes = [];
const osmStreets = [];
const osm = osmread.parse({
format: 'xml',
filePath: './output/highways-magdeburg.osm',
endDocument: function () {
const geoJson = GeoJSON.parse(osmStreets, { 'MultiLineString': 'entries' });
fs.writeFileSync('./output/coordinates-magdeburg.geojson', JSON.stringify(geoJson));
console.log('Done.');
},
node: function (node) {
nodes.push({ id: node.id, coords: [node.lon, node.lat] });
},
way: function (way) {
if (way.tags && way.tags.highway && way.tags.name) {
const osmStreet = osmStreets.find(s => s.name === way.tags.name);
if (osmStreet) {
osmStreet.entries.push(way.nodeRefs.map(nodeRef => {
const node = nodes.find(n => n.id === nodeRef);
if (node) {
return node.coords;
}
return null;
}).filter(node => !!node));
} else {
osmStreets.push({
name: way.tags.name,
entries: [way.nodeRefs.map(nodeRef => {
const node = nodes.find(n => n.id === nodeRef);
if (node) {
return node.coords;
}
return null;
}).filter(node => !!node)]
});
}
}
},
});