-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRouteUtilsV3.js
More file actions
346 lines (315 loc) · 9.3 KB
/
Copy pathRouteUtilsV3.js
File metadata and controls
346 lines (315 loc) · 9.3 KB
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
// @flow
import AllStopUtils from './AllStopUtils';
import GraphhopperUtils from './GraphhopperUtils';
import LogUtils from './LogUtils';
import ParseRouteUtils from './ParseRouteUtilsV3';
/**
* Returns the flattened version of arr.
*
* @param arr
* @returns {Array<Object>}
*/
function flatten(arr: Array<Array<Object>>): Array<Object> {
return [].concat(...arr);
}
/**
* Returns whether or not location is a bus stop.
*
* @param location
* @returns {Promise<boolean>}
*/
async function isBusStop(location: string): Promise<boolean> {
const stops = await AllStopUtils.fetchAllStops();
return stops.filter(s => s.name === location).length > 0;
}
/**
* Returns whether [route] contains a bus transfer
*
* @param route
* @returns {boolean}
*/
function routeContainsTransfer(route: Object): boolean {
const { directions } = route;
const routeIds = [];
directions.forEach((direction) => {
const { routeId } = direction;
if (routeId && !routeIds.includes(routeId)) routeIds.push(routeId);
});
return routeIds.length > 1;
}
/**
* Returns whether [routeA] and [routeB] share the the same start and end bus stops.
* @param routeA
* @param routeB
* @returns {boolean}
*/
function routesHaveSameStartEndStops(routeA: Object, routeB: Object): boolean {
const routeADirections = routeA.directions;
const routeBDirections = routeB.directions;
if (routeADirections.length < 2 || routeBDirections.length < 2) {
return false;
}
const routeAStartStop = routeADirections[0].name;
const routeAEndStop = routeADirections[routeADirections.length - 1].name;
const routeBStartStop = routeBDirections[0].name;
const routeBEndStop = routeBDirections[routeBDirections.length - 1].name;
return routeAStartStop === routeBStartStop && routeAEndStop === routeBEndStop;
}
/**
* Filter and validate the array of bus routes to send to the client.
*
* @param parsedBusRoutes
* @param parsedWalkingRoute
* @param start
* @param end
* @param departureTimeQuery
* @param isArriveBy
* @param originBusStopName
* @returns {Promise<Object>}
*/
async function createFinalBusRoutes(
parsedBusRoutes: Array<Object>,
parsedWalkingRoute: Object,
start: string,
end: string,
departureTimeQuery: number,
isArriveBy: boolean,
originBusStopName: ?string,
): Promise<Array<Object>> {
const departureTimeNowMs = parseFloat(departureTimeQuery) * 1000;
const departureDelayBuffer = !isArriveBy;
const startPointList = start.split(',');
const endPointList = end.split(',');
const startPoint = { lat: startPointList[0], long: startPointList[1] };
const endPoint = { lat: endPointList[0], long: endPointList[1] };
const finalRoutes = (await Promise.all(
parsedBusRoutes.map(currPath => ParseRouteUtils.condenseRoute(
currPath,
startPoint,
endPoint,
parsedWalkingRoute.directions[0].distance,
departureDelayBuffer,
departureTimeNowMs,
)),
)).filter(route => route !== null).sort((routeA, routeB) => {
// For routes that have the same start and end bus stops, the route with
// the earlier departure time should be shown first.
if (routesHaveSameStartEndStops(routeA, routeB)) {
const routeADepartureTime = new Date(routeA.departureTime);
const routeBDepartureTime = new Date(routeB.departureTime);
return routeADepartureTime < routeBDepartureTime ? -1 : 1;
}
// Otherwise, just use the current order.
return 0;
});
return finalRoutes;
}
/**
* Queries Graphhopper and returns exactly one walking route and any bus routes.
*
* The routes are processed prior to being returned. Note that GraphHopper always returns a walking route.
*
* @param originName
* @param destinationName
* @param end
* @param start
* @param departureTimeQuery
* @param isArriveBy
* @returns {Promise<Object>}
*/
async function getParsedWalkingAndBusRoutes(
originName: string,
destinationName: string,
end: string,
start: string,
departureTimeQuery: number,
isArriveBy: boolean,
): Promise<{ parsedBusRoutes: ?Array<Object>, parsedWalkingRoute: Object }> {
const routes = await GraphhopperUtils.fetchRoutes(end, start, departureTimeQuery, isArriveBy);
if (!routes) {
return {
parsedWalkingRoute: await getParsedWalkingRoute(
originName,
destinationName,
end,
start,
departureTimeQuery,
isArriveBy,
),
parsedBusRoutes: null,
};
}
const departureTimeMs = GraphhopperUtils.getDepartureTime(departureTimeQuery, isArriveBy, 0);
const parsedRoutes = await ParseRouteUtils.parseRoutes(
routes, originName, destinationName, departureTimeMs, isArriveBy,
);
let parsedWalkingRoute = parsedRoutes.find(route => route.numberOfTransfers === -1);
// Make request to Ghopper walking service if the bus service doesn't provide walking directions
if (!parsedWalkingRoute) {
parsedWalkingRoute = await getParsedWalkingRoute(
originName,
destinationName,
end,
start,
departureTimeQuery,
isArriveBy,
);
}
return {
parsedWalkingRoute,
parsedBusRoutes: parsedRoutes.filter(route => route.numberOfTransfers !== -1),
};
}
/**
* Queries Graphhopper for walking directions and returns exactly one walking route.
*
* We only query the Graphhopper walking service if the Graphhopper bus service doesn't provide
* walking directions.
*
* @param originName
* @param destinationName
* @param end
* @param start
* @param departureTimeQuery
* @param isArriveBy
* @returns {Promise<Object>}
*/
async function getParsedWalkingRoute(
originName: string,
destinationName: string,
end: string,
start: string,
departureTimeQuery: number,
isArriveBy: boolean,
): Promise<Object> {
const walkingRoute = await GraphhopperUtils.fetchWalkingRoute(end, start);
return ParseRouteUtils.parseWalkingRoute(
walkingRoute,
GraphhopperUtils.getDepartureTime(departureTimeQuery, isArriveBy, 0),
originName,
destinationName,
isArriveBy,
);
}
/**
* Returns the routes for a search categorizing them as being fromStop,
* boardingSoon, or walking.
*
* @param destinationName
* @param end
* @param start
* @param departureTimeQuery
* @param isArriveBy
* @param originBusStopName
* @returns {Promise<Object>}
*/
async function getSectionedRoutes(
originName: string,
destinationName: string,
end: string,
start: string,
departureTimeQuery: number,
isArriveBy: boolean,
originBusStopName: ?string,
): Promise<Object> {
const {
parsedBusRoutes,
parsedWalkingRoute,
} = await getParsedWalkingAndBusRoutes(originName, destinationName, end, start, departureTimeQuery, isArriveBy);
const sectionedRoutes = {
boardingSoon: [],
fromStop: [],
walking: [parsedWalkingRoute],
};
if (!parsedBusRoutes) {
LogUtils.log({ message: 'RouteUtils.js: Graphhopper route error : could not fetch bus routes' });
return sectionedRoutes;
}
const finalBusRoutes = await createFinalBusRoutes(
parsedBusRoutes,
parsedWalkingRoute,
start,
end,
departureTimeQuery,
isArriveBy,
);
// Iterates over each route in finalBusRoutes, calculates total delay from bus segments,
// and applies this delay to the 'walk' segment if it follows a delayed bus segment.
finalBusRoutes.forEach((route) => {
let totalDelay = 0;
let firstDelay = null;
let foundFirstDelay = false;
let firstDelayIndex = 0;
const { directions } = route;
for (let i = 0; i < directions.length; i++) {
const segment = directions[i];
const { delay } = segment;
if (!foundFirstDelay && delay !== null) {
firstDelay = delay;
foundFirstDelay = true;
firstDelayIndex = i;
}
if (segment.type === 'walk' && totalDelay > 0) {
segment.delay = totalDelay;
} else if (delay !== null) {
totalDelay += delay;
}
}
// assign delay to walking route before first bus delay
let i = 0;
if (foundFirstDelay) {
while (directions[i].type === 'walk' && firstDelay != null && i < firstDelayIndex) {
directions[i].delay = firstDelay;
i += 1;
}
}
});
finalBusRoutes.forEach((route) => {
if (originBusStopName !== null
&& route.directions
&& route.directions.length > 0
&& route.directions[0].stops.length > 0
&& route.directions[0].stops[0].name === originBusStopName
) {
sectionedRoutes.fromStop.push(route);
} else {
sectionedRoutes.boardingSoon.push(route);
}
});
return sectionedRoutes;
}
async function getRoutes(
originName: string,
destinationName: string,
end: string,
start: string,
departureTimeQuery: number,
isArriveBy: boolean,
): Promise<Array<Object>> {
const {
parsedBusRoutes,
parsedWalkingRoute,
} = await getParsedWalkingAndBusRoutes(originName, destinationName, end, start, departureTimeQuery, isArriveBy);
if (!parsedBusRoutes) {
LogUtils.log({ message: 'RouteUtils.js: Graphhopper route error : could not fetch bus routes' });
return [parsedWalkingRoute];
}
// combine and filter to create the final route
const finalRoutes = await createFinalBusRoutes(
parsedBusRoutes,
parsedWalkingRoute,
start,
end,
departureTimeQuery,
isArriveBy,
);
finalRoutes.push(parsedWalkingRoute);
return finalRoutes;
}
export default {
flatten,
getRoutes,
getSectionedRoutes,
isBusStop,
routeContainsTransfer,
};