Skip to content

Commit

Permalink
serve: format proper StopTimeEvents 🐛
Browse files Browse the repository at this point in the history
The previous format (empty arrival and/or departure object) causes errors in OpenTripPlanner.

The GTFS-Realtime spec is very clear about this:

> Either delay or time must be provided within a StopTimeEvent - both fields cannot be empty.

related: opentripplanner/OpenTripPlanner#5050
  • Loading branch information
derhuerst committed Apr 14, 2023
1 parent 670de28 commit 191a550
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/gtfs-rt-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,30 @@ const {STOPPED_AT, IN_TRANSIT_TO} = VehiclePosition.VehicleStopStatus

const formatStopTimeUpdate = (st) => {
const s = st.stop

const arr = st.arrival || st.prognosedArrival || st.plannedArrival
const arrTime = arr ? formatWhen(arr) : null
const arrDelay = 'number' === typeof st.arrivalDelay ? st.arrivalDelay : null
const arrival = arrTime !== null || arrDelay !== null ? {
time: arrTime,
delay: arrDelay,
// todo: uncertainty
} : null

const dep = st.departure || st.prognosedDeparture || st.plannedDeparture
const depTime = dep ? formatWhen(dep) : null
const depDelay = 'number' === typeof st.departureDelay ? st.departureDelay : null
const departure = depTime !== null || depDelay !== null ? {
time: depTime,
delay: depDelay,
// todo: uncertainty
} : null

return {
stop_id: s && (s.ids && s.ids.gtfs || s.id) || null,
stop_sequence: 'number' === typeof st.stopoverIndex ? st.stopoverIndex : null,
arrival: {
time: arr ? formatWhen(arr) : null,
delay: 'number' === typeof st.arrivalDelay ? st.arrivalDelay : null
// todo: uncertainty
},
departure: {
time: dep ? formatWhen(dep) : null,
delay: 'number' === typeof st.departureDelay ? st.departureDelay : null
// todo: uncertainty
},
arrival,
departure,
schedule_relationship: st.cancelled ? SKIPPED : SCHEDULED
}
}
Expand Down

0 comments on commit 191a550

Please sign in to comment.