-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-coordinates.js
55 lines (37 loc) · 1.5 KB
/
fetch-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
51
52
53
54
55
const fs = require('fs');
const fetch = require('node-fetch');
const converter = require('./converter');
if (fs.existsSync('./rooms-coordinates.json')) {
fs.unlinkSync('./rooms-coordinates.json');
}
fs.appendFileSync('./rooms-coordinates.json', '[');
let first = true;
fetch('https://occupancy-backend-e150a8daef31.herokuapp.com/api/rooms')
.then((res) => res.json())
.then(async (json) => {
for (const room of json) {
console.log('Fetching coordinates for ' + room.name);
await fetch('https://plan.epfl.ch/search?partitionlimit=5&interface=main&routing=validated&query=' + room.name)
.then((res) => res.json())
.then((data) => {
const roomCoordinates = [];
if (data.features.length === 0) return;
data.features
.find((f) => f.geometry.type === 'MultiPolygon')
.geometry.coordinates[0][0].forEach((coordinates) => {
const { latitude, longitude } = converter(coordinates[0], coordinates[1]);
roomCoordinates.push([latitude, longitude]);
});
fs.appendFileSync('./rooms-coordinates.json', (first ? '' : ',') + JSON.stringify({
name: room.name,
type: room.type,
geometry: {
type: 'MultiPolygon',
coordinates: [roomCoordinates]
}
}, null, 4));
first = false;
});
}
fs.appendFileSync('./rooms-coordinates.json', ']');
});