generated from the-pudding/lambda-node-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonToGeojson.js
75 lines (61 loc) · 1.92 KB
/
jsonToGeojson.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
import fs from "fs";
let obj;
function getFile(geo){
return new Promise((resolve, reject) => {
fs.readFile(`open_maps_json/${geo}.json`, 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
resolve();
});
})
}
export default async function convert(geo){
await getFile(geo);
let geojson = {
"type": "FeatureCollection",
"generator": "overpass-turbo",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.",
"timestamp": "2024-05-10T20:00:57Z",
"features": []
}
let nodeLookup = {};
let nodes = obj["elements"].filter(d => d.type == "node");
let ways = obj["elements"].filter(d => d.type == "way");
for (let node of nodes){
nodeLookup[node.id] = node;
}
console.log(geo,ways.length)
for (let way of ways){
let feature = {
"type": "Feature",
"properties": {
// "@id": "way/181379127",
// "leisure": "pitch",
// "source": "Bing",
// "sport": "basketball"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[]
]
},
"id": ""
}
let newNodeArray = [];
for(let node of way.nodes){
let cross = nodeLookup[node];
let coor = [cross.lon,cross.lat];
newNodeArray.push(coor);
}
feature.geometry.coordinates[0] = newNodeArray;
feature.id = way.id;
feature.properties = way.tags;
feature.properties.id = way.id;
geojson.features.push(feature);
}
fs.writeFileSync(`open_maps_geojson/${geo}.json`, JSON.stringify(geojson), 'utf8', function(err){
if (err) throw err
console.log('File saved.')
})
}