-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompute-connections.js
73 lines (64 loc) · 1.78 KB
/
compute-connections.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
'use strict'
const readStopTimes = require('./lib/read-stop-times')
// todo: respect stopover.stop_timezone & agency.agency_timezone
const computeConnections = async function* (readFile, filters = {}, opt = {}) {
if ('function' !== typeof readFile) {
throw new Error('readFile must be a function.')
}
filters = {
trip: () => true,
stopTime: () => true,
frequenciesRow: () => true,
...filters,
}
if ('function' !== typeof filters.trip) {
throw new Error('filters.trip must be a function.')
}
if ('function' !== typeof filters.stopTime) {
throw new Error('filters.stopTime must be a function.')
}
if ('function' !== typeof filters.frequenciesRow) {
throw new Error('filters.frequenciesRow must be a function.')
}
for await (const _ of readStopTimes(readFile, filters)) {
const {
tripId,
stops, arrivals, departures,
headwayBasedStarts: hwStarts,
headwayBasedEnds: hwEnds,
headwayBasedHeadways: hwHeadways,
} = _
const connections = []
// scheduled connections
for (let i = 1; i < stops.length; i++) {
connections.push({
tripId,
fromStop: stops[i - 1],
departure: departures[i - 1],
toStop: stops[i],
arrival: arrivals[i],
headwayBased: false,
})
}
// headway-based connections
// todo: DRY with compute-stopover-times
const t0 = arrivals[0]
const hwStartsL = hwStarts ? hwStarts.length : 0
for (let h = 0; h < hwStartsL; h++) {
for (let t = hwStarts[h]; t < hwEnds[h]; t += hwHeadways[h]) {
for (let i = 1; i < stops.length; i++) {
connections.push({
tripId,
fromStop: stops[i - 1],
departure: t + departures[i - 1] - t0,
toStop: stops[i],
arrival: t + arrivals[i] - t0,
headwayBased: true,
})
}
}
}
yield connections
}
}
module.exports = computeConnections