@@ -93,7 +93,7 @@ const getSpecialDates = async timetable => {
93
93
} ;
94
94
} ;
95
95
96
- const processStops = async ( agencyKey , timetable , config ) => {
96
+ const processStops = async ( timetable , config ) => {
97
97
timetable . showDayList = false ;
98
98
timetable . noServiceSymbolUsed = false ;
99
99
timetable . requestStopSymbolUsed = false ;
@@ -119,13 +119,14 @@ const processStops = async (agencyKey, timetable, config) => {
119
119
}
120
120
return false ;
121
121
} ) ;
122
- if ( ! stop ) {
123
- return ;
124
- }
125
- stop . trips . push ( formatters . formatStopTime ( stoptime , timetable , config ) ) ;
126
122
127
- if ( stop . type === 'arrival' ) {
128
- timetable . stops [ stopIndex + 1 ] . trips . push ( formatters . formatStopTime ( stoptime , timetable , config ) ) ;
123
+ if ( stop ) {
124
+ stop . trips . push ( formatters . formatStopTime ( stoptime , timetable , config ) ) ;
125
+
126
+ // If showing arrival and departure times, add trip to the departure stop
127
+ if ( stop . type === 'arrival' ) {
128
+ timetable . stops [ stopIndex + 1 ] . trips . push ( formatters . formatStopTime ( stoptime , timetable , config ) ) ;
129
+ }
129
130
}
130
131
} ) ;
131
132
@@ -141,7 +142,7 @@ const processStops = async (agencyKey, timetable, config) => {
141
142
const stopIds = _ . map ( timetable . stops , 'stop_id' ) ;
142
143
143
144
const stopDatas = await gtfs . getStops ( {
144
- agency_key : agencyKey ,
145
+ agency_key : timetable . agency_key ,
145
146
stop_id : {
146
147
$in : stopIds
147
148
}
@@ -155,7 +156,7 @@ const processStops = async (agencyKey, timetable, config) => {
155
156
// If `showStopCity` is true, look up stop attributes.
156
157
if ( config . showStopCity ) {
157
158
const stopAttributes = await gtfs . getStopAttributes ( {
158
- agency_key : agencyKey ,
159
+ agency_key : timetable . agency_key ,
159
160
stop_id : {
160
161
$in : stopIds
161
162
}
@@ -213,8 +214,20 @@ function getDaysFromCalendars(calendars) {
213
214
return days ;
214
215
}
215
216
216
- const convertRouteToTimetablePage = async ( agencyKey , route , direction ) => {
217
+ const convertTimetableToTimetablePage = async timetable => {
218
+ const filename = await generateFileName ( timetable ) ;
219
+ return {
220
+ agency_key : timetable . agency_key ,
221
+ timetable_page_id : timetable . timetable_id ,
222
+ timetable_page_label : timetable . timetable_label ,
223
+ timetables : [ timetable ] ,
224
+ filename
225
+ } ;
226
+ } ;
227
+
228
+ const convertRouteToTimetablePage = async ( route , direction ) => {
217
229
const timetable = {
230
+ agency_key : route . agency_key ,
218
231
timetable_id : `${ route . route_id } _${ direction . direction_id } ` ,
219
232
timetable_label : `${ formatters . formatRouteName ( route ) } to ${ direction . trip_headsign } ` ,
220
233
route_id : route . route_id ,
@@ -223,20 +236,14 @@ const convertRouteToTimetablePage = async (agencyKey, route, direction) => {
223
236
end_date : parseInt ( formatters . toGTFSDate ( moment ( ) . add ( 1 , 'week' ) ) , 10 )
224
237
} ;
225
238
226
- const trips = gtfs . getTrips ( {
227
- agency_key : agencyKey ,
239
+ const serviceIds = await gtfs . getTrips ( {
240
+ agency_key : route . agency_key ,
228
241
route_id : route . route_id ,
229
242
direction_id : direction . direction_id
230
- } ) ;
231
-
232
- if ( trips . length === 0 ) {
233
- return ;
234
- }
235
-
236
- const serviceIds = _ . uniq ( _ . map ( trips , 'service_id' ) ) ;
243
+ } ) . distinct ( 'service_id' ) ;
237
244
238
245
const calendars = await gtfs . getCalendars ( {
239
- agency_key : agencyKey ,
246
+ agency_key : route . agency_key ,
240
247
service_id : {
241
248
$in : serviceIds
242
249
}
@@ -248,17 +255,6 @@ const convertRouteToTimetablePage = async (agencyKey, route, direction) => {
248
255
return convertTimetableToTimetablePage ( timetable ) ;
249
256
} ;
250
257
251
- const convertTimetableToTimetablePage = async timetable => {
252
- const filename = await generateFileName ( timetable ) ;
253
- return {
254
- agency_key : timetable . agency_key ,
255
- timetable_page_id : timetable . timetable_id ,
256
- timetable_page_label : timetable . timetable_label ,
257
- timetables : [ timetable ] ,
258
- filename
259
- } ;
260
- } ;
261
-
262
258
const convertRoutesToTimetablePages = async agencyKey => {
263
259
const routes = await gtfs . getRoutes ( { agency_key : agencyKey } ) ;
264
260
const timetablePages = await Promise . all ( routes . map ( async route => {
@@ -269,7 +265,7 @@ const convertRoutesToTimetablePages = async agencyKey => {
269
265
const directionGroups = _ . groupBy ( results , direction => direction . direction_id ) ;
270
266
return Promise . all ( _ . map ( directionGroups , directionGroup => {
271
267
const direction = directionGroup [ 0 ] ;
272
- return convertRouteToTimetablePage ( agencyKey , route , direction ) ;
268
+ return convertRouteToTimetablePage ( route , direction ) ;
273
269
} ) ) ;
274
270
} ) ) ;
275
271
@@ -327,7 +323,24 @@ const formatTimetablePage = async timetablePage => {
327
323
}
328
324
329
325
return timetablePage ;
330
- }
326
+ } ;
327
+
328
+ const getTimetableGeoJSON = async ( timetable , config ) => {
329
+ const shapesGeojson = await gtfs . getShapesAsGeoJSON ( {
330
+ agency_key : timetable . agency_key ,
331
+ route_id : timetable . route_id ,
332
+ direction_id : timetable . direction_id
333
+ } ) ;
334
+
335
+ const stopsGeojson = await gtfs . getStopsAsGeoJSON ( {
336
+ agency_key : timetable . agency_key ,
337
+ route_id : timetable . route_id ,
338
+ direction_id : timetable . direction_id
339
+ } ) ;
340
+
341
+ const geojson = await geojsonMerge . merge ( [ shapesGeojson , stopsGeojson ] ) ;
342
+ return simplifyGeoJSON ( geojson , config . coordinatePrecision ) ;
343
+ } ;
331
344
332
345
function simplifyGeoJSON ( geojson , coordinatePrecision ) {
333
346
if ( coordinatePrecision === undefined ) {
@@ -362,7 +375,7 @@ exports.setDefaultConfig = config => {
362
375
return Object . assign ( defaults , config ) ;
363
376
} ;
364
377
365
- exports . generateHTML = async ( agencyKey , timetablePage , config ) => {
378
+ exports . generateHTML = async ( timetablePage , config ) => {
366
379
const stats = {
367
380
stops : 0 ,
368
381
trips : 0 ,
@@ -378,7 +391,7 @@ exports.generateHTML = async (agencyKey, timetablePage, config) => {
378
391
// Format Timetables
379
392
await Promise . all ( timetablePage . timetables . map ( async timetable => {
380
393
const query = {
381
- agency_key : agencyKey ,
394
+ agency_key : timetablePage . agency_key ,
382
395
start_date : { $lt : timetable . end_date } ,
383
396
end_date : { $gte : timetable . start_date }
384
397
} ;
@@ -401,20 +414,19 @@ exports.generateHTML = async (agencyKey, timetablePage, config) => {
401
414
}
402
415
} ) ;
403
416
404
- const calendars = await gtfs . getCalendars ( query ) ;
405
- if ( calendars . length === 0 ) {
417
+ timetable . calendars = await gtfs . getCalendars ( query ) ;
418
+ if ( timetable . calendars . length === 0 ) {
406
419
throw new Error ( `No calendars found for start_date=${ timetable . start_date } , end_date=${ timetable . end_date } , timetable_id=${ timetable . timetable_id } ` ) ;
407
420
}
408
- // Get Calendars
409
- timetable . calendars = formatters . formatCalendars ( calendars ) ;
421
+
410
422
timetable . serviceIds = _ . map ( timetable . calendars , 'service_id' ) ;
411
423
timetable . serviceIds . forEach ( serviceId => {
412
424
stats . serviceIds [ serviceId ] = true ;
413
425
} ) ;
414
426
stats . routeIds [ timetable . route_id ] = true ;
415
427
timetable . dayList = formatters . formatDays ( timetable ) ;
416
428
const routes = await gtfs . getRoutes ( {
417
- agency_key : agencyKey ,
429
+ agency_key : timetablePage . agency_key ,
418
430
route_id : timetable . route_id
419
431
} ) ;
420
432
@@ -424,7 +436,7 @@ exports.generateHTML = async (agencyKey, timetablePage, config) => {
424
436
timetable . route = routes [ 0 ] ;
425
437
426
438
timetable . trips = await gtfs . getTrips ( {
427
- agency_key : agencyKey ,
439
+ agency_key : timetablePage . agency_key ,
428
440
route_id : timetable . route_id ,
429
441
direction_id : timetable . direction_id ,
430
442
service_id : {
@@ -440,7 +452,7 @@ exports.generateHTML = async (agencyKey, timetablePage, config) => {
440
452
stats . trips += timetable . trips . length ;
441
453
442
454
const timetableStopOrders = await gtfs . getTimetableStopOrders ( {
443
- agency_key : agencyKey ,
455
+ agency_key : timetablePage . agency_key ,
444
456
timetable_id : timetable . timetable_id
445
457
} ) ;
446
458
@@ -452,7 +464,7 @@ exports.generateHTML = async (agencyKey, timetablePage, config) => {
452
464
trip . dayList = formatters . formatDays ( trip . calendar ) ;
453
465
454
466
const stoptimes = await gtfs . getStoptimes ( {
455
- agency_key : agencyKey ,
467
+ agency_key : timetablePage . agency_key ,
456
468
trip_id : trip . trip_id
457
469
} ) ;
458
470
@@ -465,38 +477,17 @@ exports.generateHTML = async (agencyKey, timetablePage, config) => {
465
477
466
478
return trip ;
467
479
} ) ) ;
480
+ timetable . orderedTrips = sortTrips ( trips ) ;
468
481
469
482
timetable . stops = getStops ( timetableStopOrders , longestTrip . stoptimes ) ;
470
483
stats . stops += timetable . stops . length ;
471
484
472
- if ( ! trips || trips . length === 0 ) {
473
- throw new Error ( `No trips found for timetable_id=${ timetable . timetable_id } ` ) ;
474
- }
475
-
476
- timetable . orderedTrips = sortTrips ( trips ) ;
477
-
478
- await processStops ( agencyKey , timetable , config ) ;
485
+ await processStops ( timetable , config ) ;
479
486
480
487
timetable . specialDates = await getSpecialDates ( timetable ) ;
481
488
482
489
if ( config . showMap ) {
483
- const shapesGeojson = await gtfs . getShapesAsGeoJSON ( {
484
- agency_key : agencyKey ,
485
- route_id : timetable . route_id ,
486
- direction_id : timetable . direction_id
487
- } ) ;
488
-
489
- const stopsGeojson = await gtfs . getStopsAsGeoJSON ( {
490
- agency_key : agencyKey ,
491
- route_id : timetable . route_id ,
492
- direction_id : timetable . direction_id
493
- } ) ;
494
-
495
- const geojson = await geojsonMerge . merge ( [ shapesGeojson , stopsGeojson ] ) ;
496
- timetable . geojson = await simplifyGeoJSON ( geojson , config . coordinatePrecision ) ;
497
- timetable . hasShapes = _ . some ( geojson . features , feature => {
498
- return feature . geometry . type === 'LineString' ;
499
- } ) ;
490
+ timetable . geojson = await getTimetableGeoJSON ( timetable , config ) ;
500
491
}
501
492
} ) ) ;
502
493
@@ -567,20 +558,22 @@ exports.getTimetablePage = async (agencyKey, timetablePageId) => {
567
558
const directionId = parseInt ( parts . pop ( ) , 10 ) ;
568
559
const routeId = parts . join ( '_' ) ;
569
560
const routes = await gtfs . getRoutes ( {
570
- agenct_key : agencyKey ,
561
+ agency_key : agencyKey ,
571
562
route_id : routeId
572
563
} ) ;
573
564
574
565
if ( routes . length === 0 ) {
575
566
throw new Error ( `No route found for route_id=${ routeId } ` ) ;
576
567
}
568
+
569
+ const route = routes [ 0 ] ;
577
570
const directions = await gtfs . getDirectionsByRoute ( {
578
571
agency_key : agencyKey ,
579
- route_id : routeId ,
572
+ route_id : routeId
580
573
} ) ;
581
574
582
575
const direction = _ . find ( directions , direction => direction . direction_id === directionId ) ;
583
- timetablePages = await convertRouteToTimetablePage ( agencyKey , result , direction ) ;
576
+ timetablePages = [ await convertRouteToTimetablePage ( route , direction ) ] ;
584
577
} else if ( timetablePages . length === 0 ) {
585
578
// If no timetablepage, use timetable
586
579
const timetable = _ . find ( timetables , { timetable_id : timetablePageId } ) ;
0 commit comments