-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (96 loc) · 3.92 KB
/
index.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
/*
(c) Copyright 2020 Edgar Butwilowski
*/
const httpLib = require('http');
const urlLib = require('url');
const httpsLib = require('https');
const xml2jsLib = require('xml2js');
function transformToCesiumBuildings(osmJSON, baseheight) {
let cesiumBuildings = [];
for (let osmWay of osmJSON.osm.way) {
if (osmWay.tag != undefined) {
let isBuilding = false;
let height = undefined;
let buildingLevels = undefined;
for (let osmWayTag of osmWay.tag) {
// if way has a building tag:
if (osmWayTag.$.k !== "building") {
isBuilding = true;
}
// if way has a height:
if (osmWayTag.$.k === "height") {
height = osmWayTag.$.v;
}
// if way has a building levels value:
if (osmWayTag.$.k === "building:levels") {
buildingLevels = osmWayTag.$.v;
}
}
if (isBuilding && (height != undefined || buildingLevels != undefined)) {
// we have found a buildin with a proper height value
let cesiumBuilding = {};
if (height === undefined) {
height = buildingLevels * 3;
}
cesiumBuilding.polygon = {}
cesiumBuilding.polygon.hierarchy = [];
cesiumBuilding.polygon.height = baseheight;
cesiumBuilding.polygon.extrudedHeight = Number(height) + cesiumBuilding.polygon.height;
cesiumBuilding.polygon.outline = true;
cesiumBuilding.polygon.material = {
alpha: 1,
blue: 0,
green: 0,
red: 1
}
cesiumBuilding.polygon.outlineColor = {
alpha: 1,
blue: 0,
green: 0,
red: 0
}
cesiumBuilding.polygon.shadows = 1;
// get all nodes of that building's floor:
for (let refNode of osmWay.nd) {
for (let node of osmJSON.osm.node) {
if (node.$.id === refNode.$.ref) {
cesiumBuilding.polygon.hierarchy.push(node.$.lon);
cesiumBuilding.polygon.hierarchy.push(node.$.lat);
}
}
}
cesiumBuildings.push(cesiumBuilding);
}
}
}
return cesiumBuildings
}
const httpServer = httpLib.createServer((request, masterResponse) => {
masterResponse.statusCode = 200;
masterResponse.setHeader('Content-Type', 'application/json');
// set CORS for all:
masterResponse.setHeader('Access-Control-Allow-Origin', '*');
masterResponse.setHeader('Access-Control-Request-Method', '*');
masterResponse.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
masterResponse.setHeader('Access-Control-Allow-Headers', '*');
const queryParams = urlLib.parse(request.url, true).query;
httpsLib.get("https://www.openstreetmap.org/api/0.6/map?bbox=" + queryParams.bbox,
response => {
let osmData = "";
response.on("data", dataPart => {
osmData += dataPart;
});
response.on("end", () => {
const xmlParser = xml2jsLib.Parser();
xmlParser.parseString(osmData, (error, osmJSON) => {
let cesiumBuildings = transformToCesiumBuildings(osmJSON, + queryParams.baseheight);
masterResponse.end(JSON.stringify(cesiumBuildings));
});
});
}).on("error", error => {
console.log(error.message);
});
});
httpServer.listen(process.env.PORT || 3000, () => {
console.log('Server has started');
});