-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (200 loc) · 5.31 KB
/
index.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
'use strict'
const {ok} = require('assert')
const findStations = require('hafas-find-stations')
const debug = require('debug')('hafas-generate-gtfs')
const Queue = require('queue')
const createCollectDeps = require('hafas-collect-departures-at')
const findDepsDurationLimit = require('hafas-find-departures-duration-limit')
const DAY = 24 * 60 * 60 * 1000
const isValidDep = (dep) => {
const when = +new Date(dep.plannedWhen)
if (Number.isNaN(when)) {
debug('invalid departure', dep)
return false
}
return true
}
// todo: progress estimation
const generateGtfs = async (hafas, createWritable, serviceArea, opt = {}) => {
if ('function' !== typeof hafas.trip) {
throw new Error('invalid HAFAS client: .trip is not a function')
}
let {
begin, duration, concurrency,
// todo: products filter?
} = {
begin: Date.now(),
duration: 30 * DAY,
concurrency: 4,
...opt
}
begin = +new Date(begin)
ok(!Number.isNaN(begin), 'begin is invalid')
ok(Number.isInteger(duration), 'duration is invalid')
ok(duration > 0, 'duration is invalid')
// todo: optionally use hafas-discover-stations?
const stations = await findStations(hafas, serviceArea)
const durStep = await findDepsDurationLimit(hafas, stations[0])
debug('duration step', durStep)
const queue = new Queue({
concurrency,
// todo: timeout
})
// todo: fare_rules, fare_attributes
// todo: shapes
// todo: frequencies
// todo: pathways
// todo: levels
// todo: feed_info
// todo: translations
// todo: attributions
const agencies = new Map()
const stops = new Map()
const routes = new Map()
const trips = []
const stop_times = []
const addAgency = (dep) => {
const a = {
agency_id: String(agencies.size+1),
agency_name: dep.line.operator.name
}
agencies.set(String(agencies.size+1), a)
}
const formatStop = (stop) => {
const f = {
stop_id: stop.id,
stop_name: stop.name, // todo: normalize
stop_lat: stop.location.latitude,
stop_lon: stop.location.longitude,
location_type: 0, parent_station: null,
}
if (stop.station) {
f.location_type = 1
f.parent_station = stop.station.id
}
return f
}
const addStop = (stop) => {
if (!stops.has(stop.id)) {
stops.set(stop.id, formatStop(stop))
}
if (stop.station && !stops.has(stop.station.id)) {
stops.set(stop.station.id, formatStop(stop.station))
}
}
const routeTypeByProduct = Object.assign(Object.create(null), {
suburban: 0,
subway: 1,
tram: 0,
bus: 3,
ferry: 4,
express: 2,
regional: 2,
})
const addLine = (line) => {
if (routes.has(line.id)) return;
routes.set(line.id, {
route_id: line.id,
// leave this temporary blank, see e.g.
// https://www.data.wien.gv.at/txt/wrlinien-gtfs-routes.txt
// todo: agency_id
route_short_name: line.name,
route_type: routeTypeByProduct[line.product],
// todo: route_color
})
}
const addTrip = (trip) => {
trips.push({
route_id: null, // todo
service_id: null, // todo
trip_id: trip.id,
trip_headsign: trip.direction,
direction_id: null, // todo
// todo: shape_id
// todo: wheelchair_accessible, bikes_allowed
})
}
const addStopover = (stopover, trip, i) => {
stop_times.push({
trip_id: trip.id,
arrival_time: stopover.plannedArrival,
departure_time: stopover.plannedDeparture,
stop_id: stopover.stop.id,
stop_sequence: i,
stop_headsign: trip.direction,
// todo: pickup_type, drop_off_type
// todo: shape_dist_traveled
timepoint: 1, // "Times are considered exact."
})
}
const fetchTrip = (dep, station) => async () => {
const lineName = dep.line && dep.line.name || 'foo'
const trip = await hafas.trip(dep.tripId, lineName)
if (!trip.line) {
debug('invalid trip', trip)
return;
}
// if (trip.line.operator) addOperator(trip.line.operator)
addLine(trip.line)
addTrip(trip)
for (let i = 0; i < trip.stopovers.length; i++) {
const stopover = trip.stopovers[i]
addStop(stopover.stop)
addStopover(stopover, trip, i)
}
}
const collectOps = {
remarks: false,
}
if (hafas.profile.departuresStbFltrEquiv !== false) {
collectOps.includeRelatedStations = false
}
const collectDeps = createCollectDeps(hafas.departures, collectOps, durStep)
const end = begin + duration
const collectDepsAt = (station) => async () => {
debug('fetching departures', station.id, station.name)
for await (let deps of collectDeps(station.id, begin)) {
deps = deps
.filter(isValidDep)
.sort((a, b) => new Date(a.plannedWhen) - new Date(b.plannedWhen))
for (const dep of deps) {
if (!trips.some(t => t.trip_id === dep.tripId)) {
queue.push(fetchTrip(dep, station))
}
if (!agencies.has(dep.line.operator.id))
queue.push(addAgency(dep))
}
if (deps.length === 0) {
// todo: try for a little longer
debug('0 departures', deps) // todo: log `when`
break
}
const lastWhen = +new Date(deps[deps.length - 1].plannedWhen)
if (lastWhen >= end) break
}
}
for (const station of stations) {
queue.push(collectDepsAt(station))
}
let done = 0
queue.on('success', () => {
done++
debug('progress', done, '/', queue.length + done)
})
queue.start()
await new Promise((resolve, reject) => {
queue.once('error', (err) => {
reject(err)
queue.end(err)
})
queue.once('end', () => resolve())
})
return {
agencies,
stops,
routes,
trips,
stop_times,
}
}
module.exports = generateGtfs