-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
executable file
·349 lines (309 loc) · 10.5 KB
/
app.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env node
const http = require('http')
const geolib = require('geolib')
const fetch = require('node-fetch')
const polyline = require('@mapbox/polyline')
const osrmTextInstructions = require('osrm-text-instructions')('v5')
const baseUrl = 'http://localhost:5000'
const intersectionDist = 100
const alternatives = 5;
const stripAlternative = false
http.createServer(onRequest).listen(3000)
/**
* Catch all incoming request in order to translate them.
* @param {Object} clientReq
* @param {Object} clientRes
*/
async function onRequest (clientReq, clientRes) {
let osrmPath = translatePath(clientReq.url)
let result = await fetch(`${baseUrl}${osrmPath}`).then(res => res.json())
console.log(`Path ${clientReq.url} translated to ${osrmPath}`)
let translatedResult = translateResult(result)
let destination = getDestination(clientReq.url)
let origin = getOrigin(clientReq.url);
let intersections = fetchIntersections(result.routes[0], alternatives)
translatedResult.routes[0] = fetchInstructions(result.routes[0])
let alternativeRoutes = await Promise.all(intersections.map(intersection => {
return getAlternativeRoutes(intersection, destination)
}))
alternativeRoutes.forEach(alternativeRoute => {
if (alternativeRoute.length > 0 && !hasCycle(alternativeRoute[0].routes[0],alternativeRoute[0].waypoints,origin)) {
let route = stripAlternative ? stripAlternativeRoute(alternativeRoute[0].routes[0]) : alternativeRoute[0].routes[0]
route.legs[1].annotation = {
congestion: new Array(polyline.decode(route.geometry).length - 1).fill(getColor(route, translatedResult.routes[0]))
}
translatedResult.routes.push(route)
}
})
clientRes.write(JSON.stringify(translatedResult))
clientRes.end('\n')
}
/**
* Make sure that the directions endpoint is mapped to the routing endpoint.
* Strip all GET params and append some needed params.
* @param {String} originalPath
* @return {String} translatedPath
*/
function translatePath (originalPath) {
return originalPath.replace('directions/v5/mapbox', 'route/v1').split('?')[0] + '?steps=true&geometries=polyline6&annotations=true&overview=full&continue_straight=true'
}
/**
* Return an array of every intersection along the route
* @param {Object} route
* @return {Array} intersections
*/
function fetchIntersections (route, limit) {
let intersections = []
let count = 0
let duration = 0
for (let leg of route.legs) {
for (let step of leg.steps) {
duration += step.duration
for (let intersection of step.intersections) {
intersection.duration = duration
intersections.push(intersection)
count++
if (count >= limit) {
return intersections
}
}
}
}
return intersections
}
/**
* Add text instructions to OSRM steps
* @param {Object} route
* @return {Object} route
*/
function fetchInstructions (route) {
route.legs.map(leg => {
for (let i = 0; i < leg.steps.length; i++) {
leg.steps[i].maneuver.instruction = ''
if (typeof leg.steps[i + 1] === 'undefined') {
return
}
leg.steps[i].voiceInstructions = [{
distanceAlongGeometry: leg.steps[i].distance,
announcement: osrmTextInstructions.compile('en', leg.steps[i + 1]),
ssmlAnnouncement: '<speak><amazon:effect name="drc"><prosody rate="1.08">' + osrmTextInstructions.compile('en', leg.steps[i + 1]) + '</prosody></amazon:effect></speak>'
}]
leg.steps[i].bannerInstructions = [{
distanceAlongGeometry: leg.steps[i].distance,
primary: {
text: osrmTextInstructions.getWayName('en', leg.steps[i + 1]),
components: [{
text: osrmTextInstructions.compile('en', leg.steps[i + 1]),
type: 'text'
}],
type: leg.steps[i + 1].maneuver.type,
modifier: leg.steps[i + 1].maneuver.modifier,
degrees: leg.steps[i + 1].maneuver.bearing_after,
driving_side: 'right'
},
secondary: null
}]
}
return leg
})
return route
}
/**
* Return true if the route contains a cycle
* @param {Object} route
* @param {Array} waypoints
* @param {Geopoint} origin
*/
function hasCycle (route,waypoints,origin) {
// Checks if there are U-turns with waypoints distance ; Could do it with the intersection distance but seems complicated
if(waypoints.length !==0) {
let distanceCounter = 0;
for (let waypoint of waypoints) {
if (waypoint.distance) {
if (waypoint.distance > distanceCounter)
distanceCounter = waypoint.distance;
if (waypoint.distance < distanceCounter)
return true;
}
}
}
let intersections = {};
for (let [legIndex,leg] of route.legs.entries()) {
for (let [stepIndex,step] of leg.steps.entries()) {
for (let [intersectionIndex,intersection] of step.intersections.entries()) {
// If intersection not far from origin and it isn't the first one of the first step of the first....
if((intersectionIndex!==0 &&
stepIndex !==0 &&
legIndex !==0 ) &&
!checkDistance(toGeopoint(""+intersection.location[0]+","+intersection.location[1]+""),origin,25/1000)) // Distance in km
return true;
intersections[toGeostring(intersection.location)] = intersections[toGeostring(intersection.location)] + 1 || 0
if (intersections[toGeostring(intersection.location)] > 1) {
return true
}
}
}
}
return false
}
/**
* Get the destination by parsing the url in url format.
* @param {String} url
* @return {Geopoint} destination
*/
function getDestination (url) {
return toGeopoint(url.split('/')[5].split(';')[1].split('?')[0])
}
/**
* Check the distance between two points
* @param {Geopoint} firstGeo
* @param {Geopoint} secondGeo
* @param {number} distance
*/
// Check https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula
// Haversine formula
function checkDistance(firstGeo,secondGeo,distance){
let p = 0.017453292519943295; // Math.PI / 180
let c = Math.cos;
let a = 0.5 - c((secondGeo.lat - firstGeo.lat) * p)/2 +
c(firstGeo.lat * p) * c(secondGeo.lat * p) *
(1 - c((secondGeo.lon - firstGeo.lon) * p))/2;
let calculatedDistance = 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
return calculatedDistance>distance
}
/**
* Get the origin by parsing the url in url format.
* @param {String} url
* @return {Geopoint} origin
*/
function getOrigin (url) {
return toGeopoint(url.split('/')[5].split(';')[0])
}
/**
* The mapbox sdk needs a uuid, crashes otherwise. So append one here.
* @param {Object} originalResult
* @return {Object} translatedResult
*/
function translateResult (originalResult) {
let translatedResult = Object.assign({}, originalResult)
translatedResult.uuid = 1
return translatedResult
}
/**
* Calculate the points around the intersection.
* These points are intersectionDist meters away from the intersection
* on every unused road.
* @param {Object} intersection
* @return {Array} viaPoints
*/
function getViaPoints (intersection) {
let initialPoint = toGeopoint(intersection.location)
let otherBearings = intersection.bearings
let bearingIn = otherBearings[intersection.in]
let bearingOut = otherBearings[intersection.out]
// Remove bearings of current primary route and the ones ones in wrong direction
otherBearings = otherBearings.filter(bearing =>
bearing !== bearingOut && bearing !== bearingIn
)
var viaPoints = otherBearings.map(bearing => {
return geolib.computeDestinationPoint(initialPoint, intersectionDist, bearing)
})
return viaPoints
}
/**
* Get alternative routes via every viapoint from the intersection.
* @param {Object} intersection
* @param {Geopoint} destination
* @return {Promise[]|routes} alternative routes
*/
function getAlternativeRoutes (intersection, destination) {
return new Promise((resolve, reject) => {
let start = toGeopoint(intersection.location)
let viaPoints = getViaPoints(intersection)
let alternativeRoutes = viaPoints.map(viaPoint => {
return getRoute([
start,
viaPoint,
destination
])
})
Promise.all(alternativeRoutes).then(routes => {
let sortedRoutes = routes.sort((a, b) => {
return a.routes[0].duration > b.routes[0].duration ? 1 : -1
})
sortedRoutes.map(route => {
route.routes[0].duration = route.routes[0].duration + intersection.duration
return route
})
resolve(sortedRoutes)
})
})
}
/**
* Fetch an alternative route from OSRM service.
* @param {Array} points
* @return {Promse|Route}
*/
function getRoute (waypoints) {
let coordinates = toCoordinateString(waypoints)
return fetch(`${baseUrl}/route/v1/driving/${coordinates}?steps=true&geometries=polyline6`)
.then(res => res.json())
}
/**
* Determine color of route based on extra time
* @param {Object} alternativeRoute
* @param {Object} originalRoute
*/
function getColor (alternativeRoute, originalRoute) {
let extraTime = alternativeRoute.duration - originalRoute.duration
let extraPercentage = alternativeRoute.duration / originalRoute.duration
if (extraTime < 100 || extraPercentage < 1.05) {
return 'moderate'
} else {
return 'heavy'
}
}
/**
* Only keep the first two steps of the route.
* @param {Object} alternativeRoute
* @return {Object} alternativeRoute
*/
function stripAlternativeRoute (alternativeRoute) {
alternativeRoute.geometry = polyline.encode(polyline.decode(alternativeRoute.legs[0].steps[0].geometry).concat(polyline.decode(alternativeRoute.legs[0].steps[1].geometry)))
return alternativeRoute
}
/**
* Convert geopoints to the format OSRM understands
* @param {Array} waypoints
* @return {String} coordinate string
*/
function toCoordinateString (waypoints) {
return waypoints
.map(waypoint => {
if (waypoint.longitude) {
waypoint.lon = waypoint.longitude
waypoint.lat = waypoint.latitude
}
return `${waypoint.lon},${waypoint.lat}`
}).join(';')
}
/**
* Simple "hash" of waypoint
* @param {Array} waypoint
* @return {String} waypoint
*/
function toGeostring (waypoint) {
return `${waypoint[0]};${waypoint[1]}`
}
/**
* Convert coordinate string or array to the generic geopoint format.
* @param {String} coordinateString
* @return {Geopoint} geopoint
*/
function toGeopoint (waypoint) {
let coordinates = (typeof waypoint === 'string') ? waypoint.split(',') : waypoint
return {
lon: coordinates[0],
lat: coordinates[1]
}
}