-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
117 lines (102 loc) · 3.59 KB
/
api.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
113
114
115
116
117
'use strict';
const fetch = require('node-fetch');
module.exports = (router) => {
router.get('/', (request, response) => {
response.status(200).send("Root route for this application's api");
});
router.get('/points', (request, response) => {
Promise.resolve({centerSquarePoints: [
{
end: {lat: 46.773781379807524,lng: 23.58806787222754},
start: {lat: 46.77488095105854, lng: 23.589667275956423},
},
{
end: {lat: 46.77488095105854, lng: 23.589667275956423},
start: {lat: 46.775623000948556,lng: 23.59058520784507},
},
{
end: {lat: 46.775623000948556,lng: 23.59058520784507},
start: {lat: 46.77471540405222, lng: 23.59181175856475},
},
{
end: {lat: 46.77471540405222,lng: 23.59181175856475},
start: {lat: 46.77429367376812,lng: 23.592295011235933},
},
{
end: {lat: 46.77471540405222,lng: 23.59181175856475},
start: {lat: 46.77419829442854,lng: 23.592122984174388},
},
{
end: {lat: 46.77429367376812,lng: 23.592295011235933},
start: {lat: 46.77419829442854,lng: 23.592122984174388},
},
{
end: {lat: 46.77488095105854,lng: 23.589667275956423},
start: {lat: 46.77411916855766,lng: 23.590376558733723},
},
{
end: {lat: 46.77419829442854,lng: 23.592122984174388},
start: {lat: 46.77411916855766,lng: 23.590376558733723},
},
{
end: {lat: 46.77411916855766,lng: 23.590376558733723},
start: {lat: 46.77370555612028,lng: 23.58821318920320}
}
]})
.then(async function(data) {
const {centerSquarePoints} = data;
const bounds = {
minLat: 999,
maxLat: -999,
minLng: 999,
maxLng: -999
};
for (const pair of centerSquarePoints) {
for (const prop in pair) {
if (pair[prop].lat > bounds.maxLat)
bounds.maxLat = pair[prop].lat;
else if (pair[prop].lat < bounds.minLat)
bounds.minLat = pair[prop].lat;
if (pair[prop].lng > bounds.maxLng)
bounds.maxLng = pair[prop].lng;
else if (pair[prop].lng < bounds.minLng)
bounds.minLng = pair[prop].lng;
}
}
return Promise.resolve({bounds, centerSquarePoints});
})
.then(async function(response) {
const {centerSquarePoints, bounds} = response;
let arrayKeys = {};
let pointsArray = [];
let index = 0;
centerSquarePoints.forEach(pair => {
for (const p in pair) {
const prop = `${pair[p].lat},${pair[p].lng}`;
if (!arrayKeys.hasOwnProperty(prop)) {
arrayKeys[prop] = {
index: index++,
point: pair[p]
};
}
}
});
for (let i = 0; i < centerSquarePoints.length; i++) {
for (const p of ['start', 'end']) {
const prop = `${centerSquarePoints[i][p].lat},${centerSquarePoints[i][p].lng}`;
centerSquarePoints[i][p] = arrayKeys[prop];
}
}
for (const prop in arrayKeys) {
pointsArray[arrayKeys[prop].index] = arrayKeys[prop].point;
}
return Promise.resolve({bounds, centerSquarePoints, pointsArray});
})
.then((data) => {
response.setHeader('Cache-Control', 'no-cache');
response.status(200).json({pairs: data.centerSquarePoints, bounds: data.bounds, points: data.pointsArray});
})
.catch(error => console.log(error));
});
return router;
};