forked from stadtnavi/generate-gtfs-flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch-stop-times-txt.js
executable file
·214 lines (182 loc) · 6.96 KB
/
patch-stop-times-txt.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: [
'help', 'h',
'version', 'v',
]
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
patch-stop-times-txt <path-to-flex-rules> <gtfs-routes> <gtfs-trips> <gtfs-stops> <gtfs-stop-times>
Examples:
patch-stop-times-txt flex-rules.js \\
gtfs/{routes,trips,stops,stop_times}.txt >gtfs/stop_times.patched.txt
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`${pkg.name} v${pkg.version}\n`)
process.exit(0)
}
const showError = (err) => {
console.error(err)
process.exit(1)
}
const {resolve} = require('path')
const assert = require('assert')
const pickupTypes = require('gtfs-utils/pickup-types')
const dropOffTypes = require('gtfs-utils/drop-off-types')
const {Stringifier} = require('csv-stringify')
const createReadGtfsFile = require('./lib/read-gtfs-files')
const addSeconds = require('./lib/add-seconds')
const {computeFlexSpecsByTripId} = require('./lib/flex-specs-by-trip-id')
const {
generateFlexLocationId: flexLocId,
generateFlexTripId: genFlexTripId,
} = require('./lib/ids')
const pathToFlexRules = argv._[0]
if (!pathToFlexRules) showError('Missing path-to-flex-rules.')
const flexRules = require(resolve(process.cwd(), pathToFlexRules))
const requiredGtfsFiles = [
'routes',
'trips',
'stops',
'stop_times',
]
const readGtfsFile = createReadGtfsFile(requiredGtfsFiles, argv._.slice(1))
const timepointTypes = { // todo: move to gtfs-utils
APPROXIMATE: 0,
EXACT: 1,
}
const patchStopTimeWithBookingRules = (st, rufbusSpec) => {
const {bookingRule} = rufbusSpec
// GTFS-BookingRules
// https://github.com/MobilityData/gtfs-flex/blob/e1832cfea5ddb9df29bd2fc50e80b0a4987695c1/spec/reference.md#stop_timestxt-file-extended-1
st.pickup_booking_rule_id = bookingRule.booking_rule_id
st.drop_off_booking_rule_id = bookingRule.booking_rule_id
}
const patchStopTimeWithFlexibleTrips = (st, rufbusSpec) => {
const {
id: specId,
pickup_type,
drop_off_type,
} = rufbusSpec
const {
arrival_time,
departure_time,
} = st
st.timepoint = timepointTypes.APPROXIMATE
// GTFS-FlexibleTrips
// https://github.com/MobilityData/gtfs-flex/blob/e1832cfea5ddb9df29bd2fc50e80b0a4987695c1/spec/reference.md#stop_timestxt-file-extended
st.stop_id = flexLocId(specId, st.stop_id)
// `arrival_time`:
// - Forbidden when `stop_times.stop_id` references a `location_groups.locationg_group_id` or an `id` from `locations.geojson`.
// `departure_time`:
// - Forbidden when `stop_times.stop_id` references a `location_groups.locationg_group_id` or an `id` from `locations.geojson`.
st.arrival_time = null
st.departure_time = null
// `start_pickup_dropoff_window`:
// Time that on-demand service becomes available in a GeoJSON location or location group.
// - Required if `stop_times.stop_id` refers to `location_groups.location_group_id` or `id` from `locations.geojson`.
// - Forbidden if `stop_times.stop_id` refers to `stops.stop_id`.
// todo: smarter window calculation, e.g. depending on prev stop_time
st.start_pickup_dropoff_window = addSeconds(arrival_time, -30)
// `end_pickup_dropoff_window`
// Time that on-demand service ends in a GeoJSON location or location group.
// - Required if `stop_times.stop_id` refers to `location_groups.location_group_id` or `id` from `locations.geojson`.
// - Forbidden if `stop_times.stop_id` refers to `stops.stop_id`.
// todo: smarter window calculation, e.g. depending on prev stop_time
st.end_pickup_dropoff_window = addSeconds(arrival_time, 30)
// `pickup_type`:
// `0` or empty - Regularly scheduled pickup.
// `1` - No pickup available.
// `2` - Must phone agency to arrange pickup.
// `3` - Must coordinate with driver to arrange pickup.
// - `pickup_type=0` forbidden for `stop_times.stop_id` referring to `location_groups.location_group_id` or `id` from `locations.geojson`.
// - `pickup_type=3` forbidden for `location_groups.location_group_id` or `locations.geojson` that are not a single `LineString`.
// - Optional otherwise.
assert.ok((
pickup_type !== pickupTypes.REGULAR &&
pickup_type !== pickupTypes.MUST_COORDINATE_WITH_DRIVER
), `\
rufbusSpec ${specId} has a pickup_type of ${pickup_type}, but it is forbidden for locations.geojson-based stop_times.`)
st.pickup_type = pickup_type
// `drop_off_type`:
// `0` or empty - Regularly scheduled drop off.
// `1` - No drop off available.
// `2` - Must phone agency to arrange drop off.
// `3` - Must coordinate with driver to arrange drop off.
// - `drop_off_type=0` forbidden for `stop_times.stop_id` referring to `location_groups.location_group_id` or `id` from `locations.geojson`.
// - Optional otherwise.
assert.ok((
drop_off_type !== dropOffTypes.REGULAR
), `\
rufbusSpec ${specId} has a drop_off_type of ${drop_off_type}, but it is forbidden for locations.geojson-based stop_times.`)
st.drop_off_type = drop_off_type
// todo: add mean_duration_factor & mean_duration_offset?
// todo: add safe_duration_factor & safe_duration_offset?
}
;(async () => {
const byTripId = await computeFlexSpecsByTripId(flexRules, readGtfsFile)
const csv = new Stringifier({
quoted: true,
header: true,
})
csv.pipe(process.stdout)
// (original) trip ID -> stop_times rows
const flexTrips = new Map()
// pass through all stop_times rows, but add booking rules & empty columns
for await (const st of readGtfsFile('stop_times')) {
// Assume non-on-demand case first.
st.pickup_booking_rule_id = null
st.drop_off_booking_rule_id = null
st.start_pickup_dropoff_window = null
st.end_pickup_dropoff_window = null
st.timepoint = timepointTypes.EXACT
if (byTripId.has(st.trip_id)) {
const flexSpec = byTripId.get(st.trip_id)
patchStopTimeWithBookingRules(st, flexSpec)
if (!flexTrips.has(st.trip_id)) {
flexTrips.set(st.trip_id, [st])
} else {
flexTrips.get(st.trip_id).push(st)
}
}
csv.write(st)
}
for (let [originalTripId, stopTimes] of flexTrips.entries()) {
const flexSpec = byTripId.get(originalTripId)
const flexTripId = genFlexTripId(originalTripId)
stopTimes = stopTimes
.sort((a, b) => parseInt(a.stop_sequence) - parseInt(b.stop_sequence))
.forEach((st, i) => {
// We put the drop-off stop_time row before the pickup row so that
// you cannot board and then immediately alight at the same stop.
const dropOffAtFlexAreaSt = {
...st,
trip_id: flexTripId,
stop_sequence: i * 2,
stop_headsign: null,
}
patchStopTimeWithBookingRules(dropOffAtFlexAreaSt, flexSpec)
patchStopTimeWithFlexibleTrips(dropOffAtFlexAreaSt, flexSpec)
dropOffAtFlexAreaSt.pickup_type = pickupTypes.NOT_AVAILBLE
csv.write(dropOffAtFlexAreaSt)
const pickupAtStopSt = {
...st,
trip_id: flexTripId,
stop_sequence: i * 2 + 1,
timepoint: timepointTypes.EXACT,
}
patchStopTimeWithBookingRules(pickupAtStopSt, flexSpec)
pickupAtStopSt.drop_off_type = dropOffTypes.NOT_AVAILBLE
csv.write(pickupAtStopSt)
})
}
csv.end()
})()
.catch(showError)