-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathosm2geojson.js
45 lines (37 loc) · 939 Bytes
/
osm2geojson.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
var split = require('split2'),
through = require('through2');
var geojson = {
'type': 'FeatureCollection',
'features': []
};
function parser( line, _, next ){
var json = JSON.parse( line );
if( json.type === 'node' ){
geojson.features.push({
'type': 'Feature',
'properties': json,
'geometry': {
'type': 'Point',
'coordinates': [ parseFloat( json.lon ), parseFloat( json.lat ) ]
}
});
}
else if( json.type === 'way' ){
geojson.features.push({
'type': 'Feature',
'properties': json,
'geometry': {
'type': 'LineString',
'coordinates': json.nodes.map( function( node ){
return [ parseFloat( node.lon ), parseFloat( node.lat ) ];
})
}
});
}
next();
}
function flush(){
console.log( JSON.stringify( geojson, null, 2 ) );
}
process.stdin.pipe( split() )
.pipe( through( parser, flush ) );