minotor 11.2.3 → 11.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/CHANGELOG.md +3 -3
  2. package/README.md +27 -0
  3. package/dist/cli.mjs +300 -98
  4. package/dist/cli.mjs.map +1 -1
  5. package/dist/gtfs/parser.d.ts +3 -1
  6. package/dist/gtfs/routes.d.ts +1 -2
  7. package/dist/gtfs/trips.d.ts +2 -1
  8. package/dist/parser.cjs.js +225 -81
  9. package/dist/parser.cjs.js.map +1 -1
  10. package/dist/parser.d.ts +6 -2
  11. package/dist/parser.esm.js +224 -82
  12. package/dist/parser.esm.js.map +1 -1
  13. package/dist/router.cjs.js +1 -1
  14. package/dist/router.cjs.js.map +1 -1
  15. package/dist/router.d.ts +4 -3
  16. package/dist/router.esm.js +1 -1
  17. package/dist/router.esm.js.map +1 -1
  18. package/dist/router.umd.js +1 -1
  19. package/dist/router.umd.js.map +1 -1
  20. package/dist/routing/result.d.ts +1 -4
  21. package/dist/routing/route.d.ts +9 -5
  22. package/dist/timetable/route.d.ts +9 -5
  23. package/dist/timetable/timetable.d.ts +24 -3
  24. package/dist/transfers/__tests__/straightLineTransferGenerator.test.d.ts +1 -0
  25. package/dist/transfers/generator.d.ts +40 -0
  26. package/dist/transfers/straightLineTransferGenerator.d.ts +58 -0
  27. package/dist/utils/map.d.ts +10 -0
  28. package/package.json +1 -1
  29. package/src/__e2e__/router.test.ts +6 -2
  30. package/src/cli/minotor.ts +21 -1
  31. package/src/cli/repl.ts +6 -12
  32. package/src/gtfs/__tests__/parser.test.ts +29 -1
  33. package/src/gtfs/__tests__/routes.test.ts +4 -3
  34. package/src/gtfs/__tests__/transfers.test.ts +8 -8
  35. package/src/gtfs/__tests__/trips.test.ts +60 -24
  36. package/src/gtfs/parser.ts +50 -1
  37. package/src/gtfs/profiles/__tests__/extended.test.ts +2 -1
  38. package/src/gtfs/profiles/extended.ts +14 -14
  39. package/src/gtfs/profiles/standard.ts +11 -10
  40. package/src/gtfs/routes.ts +5 -2
  41. package/src/gtfs/transfers.ts +6 -5
  42. package/src/gtfs/trips.ts +7 -14
  43. package/src/parser.ts +20 -2
  44. package/src/router.ts +12 -6
  45. package/src/routing/__tests__/access.test.ts +14 -6
  46. package/src/routing/__tests__/plainRouter.test.ts +25 -23
  47. package/src/routing/__tests__/plotter.test.ts +8 -3
  48. package/src/routing/__tests__/rangeResult.test.ts +6 -2
  49. package/src/routing/__tests__/rangeRouter.test.ts +10 -6
  50. package/src/routing/__tests__/raptor.test.ts +15 -9
  51. package/src/routing/__tests__/result.test.ts +9 -4
  52. package/src/routing/__tests__/route.test.ts +3 -4
  53. package/src/routing/__tests__/router.test.ts +2 -1
  54. package/src/routing/access.ts +7 -4
  55. package/src/routing/plotter.ts +8 -3
  56. package/src/routing/raptor.ts +5 -5
  57. package/src/routing/result.ts +32 -14
  58. package/src/routing/route.ts +13 -10
  59. package/src/routing/state.ts +1 -1
  60. package/src/timetable/__tests__/io.test.ts +22 -10
  61. package/src/timetable/__tests__/route.test.ts +18 -24
  62. package/src/timetable/__tests__/timetable.test.ts +17 -11
  63. package/src/timetable/io.ts +30 -28
  64. package/src/timetable/route.ts +14 -7
  65. package/src/timetable/timetable.ts +77 -28
  66. package/src/transfers/README.md +97 -0
  67. package/src/transfers/__tests__/straightLineTransferGenerator.test.ts +204 -0
  68. package/src/transfers/generator.ts +45 -0
  69. package/src/transfers/straightLineTransferGenerator.ts +143 -0
  70. package/src/utils/map.ts +22 -0
@@ -0,0 +1,97 @@
1
+ # Virtual transfers
2
+
3
+ Many GTFS feeds only partially populate `transfers.txt`, so nearby stops that belong to different stations (e.g. a subway platform and an adjacent tram or bus terminal) end up with no walking connection and cannot be used as an interchange. A `TransferGenerator` can be passed to the parser to synthesize the missing walking transfers as a pre-processing step, before the timetable is serialized.
4
+
5
+ ## Built-in implementation
6
+
7
+ `StraightLineTransferGenerator` connects nearby stops that aren't already linked in the feed — for every stop it adds walking transfers to the other stops within a radius, estimating the walking time from the straight-line distance:
8
+
9
+ ```ts
10
+ import {
11
+ GtfsParser,
12
+ StraightLineTransferGenerator,
13
+ extendedGtfsProfile,
14
+ } from 'minotor/parser';
15
+
16
+ const parser = new GtfsParser(
17
+ 'gtfs-feed.zip',
18
+ extendedGtfsProfile,
19
+ new StraightLineTransferGenerator({ maxDistanceMeters: 500 }),
20
+ );
21
+
22
+ const timetable = await parser.parseTimetable(new Date());
23
+ ```
24
+
25
+ On top of the walking time, some adjustments make the estimate more realistic:
26
+
27
+ - a **detour factor** as no path in the real world is a straight line
28
+ - a flat **change penalty** added to every transfer, for the general overhead of changing
29
+
30
+ The change penalty is symmetric, so the two directions of a transfer share the same minimum time. Existing transfers from the feed are always preserved:
31
+
32
+ | Option | Default | Description |
33
+ | ---------------------- | ------- | ----------------------------------------------------------------------- |
34
+ | `maxDistanceMeters` | `500` | Radius within which two stops are connected |
35
+ | `walkingSpeedKmh` | `4` | Assumed walking speed |
36
+ | `detourFactor` | `1.3` | Multiplier from straight-line to real walking distance (`1` to disable) |
37
+ | `changePenaltyMinutes` | `3` | Flat penalty added to every generated transfer (`0` to disable) |
38
+
39
+ ## Custom implementation
40
+
41
+ The straight-line estimate is relatively basic but works without external dependencies. For realistic walking times, you can implement the `TransferGenerator` interface yourself by e.g. delegating to an external pedestrian routing engine.
42
+
43
+ The parser passes the candidate origin stops (route-served stops with coordinates) and deduplicates the result against the transfers already in the feed, keeping only links between stops a route actually calls at — so you can emit a transfer for every nearby pair without tracking what already exists. Return the directed transfers to add, using `TransferTypes.REQUIRES_MINIMAL_TIME` so the router treats them as walking connections (for both transfers and access/egress). `minTransferTime` is expressed in minutes.
44
+
45
+ ```ts
46
+ import { GtfsParser, TransferTypes, extendedGtfsProfile } from 'minotor/parser';
47
+ import type { GeneratedTransfers, TransferGenerator } from 'minotor/parser';
48
+ import type { Stop, StopsIndex } from 'minotor';
49
+
50
+ // Delegates to a hypothetical external walking-router HTTP API.
51
+ class WalkingApiTransferGenerator implements TransferGenerator {
52
+ async generate(
53
+ originStops: Stop[],
54
+ stops: StopsIndex,
55
+ ): Promise<GeneratedTransfers> {
56
+ const generated: GeneratedTransfers = new Map();
57
+
58
+ for (const origin of originStops) {
59
+ // Reuse minotor's geo index to find nearby candidate stops.
60
+ const neighbors = stops.findStopsByLocation(
61
+ origin.lat,
62
+ origin.lon,
63
+ Infinity,
64
+ this.maxDistanceKm,
65
+ );
66
+
67
+ for (const neighbor of neighbors) {
68
+ // Ask the external engine for a real door-to-door walking time.
69
+ const walkTimeInMinutes = await callExternalRoutingEngine(
70
+ origin.lat,
71
+ origin.lon,
72
+ neighbor.lat,
73
+ neighbor.lon,
74
+ );
75
+
76
+ const transfers = generated.get(origin.id) ?? [];
77
+ transfers.push({
78
+ destination: neighbor.id,
79
+ type: TransferTypes.REQUIRES_MINIMAL_TIME,
80
+ minTransferTime: walkTimeInMinutes,
81
+ });
82
+ generated.set(origin.id, transfers);
83
+ }
84
+ }
85
+
86
+ return generated;
87
+ }
88
+ }
89
+
90
+ const parser = new GtfsParser(
91
+ 'gtfs-feed.zip',
92
+ extendedGtfsProfile,
93
+ new WalkingApiTransferGenerator('https://walk.example.com'),
94
+ );
95
+ ```
96
+
97
+ Transfers are directed. The example above emits one edge per `origin -> neighbor`; the reverse edge is produced when the neighbor is itself visited as an origin (every candidate stop is passed as an origin), so a symmetric walking time yields a usable connection both ways.
@@ -0,0 +1,204 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+
4
+ import { distance } from 'geokdbush';
5
+
6
+ import { Stop } from '../../stops/stops.js';
7
+ import { StopsIndex } from '../../stops/stopsIndex.js';
8
+ import { TransferTypes } from '../../timetable/timetable.js';
9
+ import { StraightLineTransferGenerator } from '../straightLineTransferGenerator.js';
10
+
11
+ // A tiny synthetic interchange: a subway and a tram stop ~100m apart, a bus
12
+ // stop ~2km away, and a stop with no coordinates.
13
+ const subwayLat = 59.357;
14
+ const subwayLon = 18.102;
15
+ const tramLat = 59.3576;
16
+ const tramLon = 18.1035;
17
+ const farLat = 59.37;
18
+ const farLon = 18.13;
19
+
20
+ const subway: Stop = {
21
+ id: 0,
22
+ name: 'Subway',
23
+ lat: subwayLat,
24
+ lon: subwayLon,
25
+ children: [],
26
+ locationType: 'SIMPLE_STOP_OR_PLATFORM',
27
+ };
28
+ const tram: Stop = {
29
+ id: 1,
30
+ name: 'Tram',
31
+ lat: tramLat,
32
+ lon: tramLon,
33
+ children: [],
34
+ locationType: 'SIMPLE_STOP_OR_PLATFORM',
35
+ };
36
+ const far: Stop = {
37
+ id: 2,
38
+ name: 'Far Away',
39
+ lat: farLat,
40
+ lon: farLon,
41
+ children: [],
42
+ locationType: 'SIMPLE_STOP_OR_PLATFORM',
43
+ };
44
+ const noCoords: Stop = {
45
+ id: 3,
46
+ name: 'No Coordinates',
47
+ children: [],
48
+ locationType: 'SIMPLE_STOP_OR_PLATFORM',
49
+ };
50
+
51
+ const stops = new StopsIndex([subway, tram, far, noCoords]);
52
+
53
+ describe('StraightLineTransferGenerator', () => {
54
+ it('connects nearby stops with typed walking transfers', () => {
55
+ const generator = new StraightLineTransferGenerator({
56
+ maxDistanceMeters: 500,
57
+ });
58
+ const transfers = generator.generate([subway, tram], stops);
59
+
60
+ const subwayToTram = (transfers.get(subway.id) ?? []).find(
61
+ (t) => t.destination === tram.id,
62
+ );
63
+ assert.ok(subwayToTram, 'expected a transfer from subway to tram');
64
+ assert.strictEqual(subwayToTram.type, TransferTypes.REQUIRES_MINIMAL_TIME);
65
+ assert.ok(
66
+ typeof subwayToTram.minTransferTime === 'number' &&
67
+ Number.isInteger(subwayToTram.minTransferTime),
68
+ );
69
+ });
70
+
71
+ it('emits exactly one edge per direction when both stops are origins', () => {
72
+ const generator = new StraightLineTransferGenerator({
73
+ maxDistanceMeters: 500,
74
+ });
75
+ const transfers = generator.generate([subway, tram], stops);
76
+
77
+ const subwayToTram = (transfers.get(subway.id) ?? []).filter(
78
+ (t) => t.destination === tram.id,
79
+ );
80
+ const tramToSubway = (transfers.get(tram.id) ?? []).filter(
81
+ (t) => t.destination === subway.id,
82
+ );
83
+ assert.strictEqual(subwayToTram.length, 1);
84
+ assert.strictEqual(tramToSubway.length, 1);
85
+ });
86
+
87
+ it('only emits transfers from the given origins', () => {
88
+ const generator = new StraightLineTransferGenerator({
89
+ maxDistanceMeters: 500,
90
+ });
91
+ // Only the subway is an origin; the reverse tram -> subway edge is only
92
+ // emitted when the tram is itself visited as an origin.
93
+ const transfers = generator.generate([subway], stops);
94
+
95
+ const subwayToTram = (transfers.get(subway.id) ?? []).some(
96
+ (t) => t.destination === tram.id,
97
+ );
98
+ assert.ok(subwayToTram, 'expected subway -> tram');
99
+ assert.ok(
100
+ !transfers.has(tram.id),
101
+ 'expected no reverse edge when the tram is not an origin',
102
+ );
103
+ });
104
+
105
+ it('does not connect stops beyond the radius', () => {
106
+ const generator = new StraightLineTransferGenerator({
107
+ maxDistanceMeters: 500,
108
+ });
109
+ const transfers = generator.generate([subway], stops);
110
+
111
+ const subwayToFar = (transfers.get(subway.id) ?? []).some(
112
+ (t) => t.destination === far.id,
113
+ );
114
+ assert.ok(!subwayToFar);
115
+ assert.ok(!transfers.has(far.id));
116
+ });
117
+
118
+ it('connects farther stops when the radius is large enough', () => {
119
+ const generator = new StraightLineTransferGenerator({
120
+ maxDistanceMeters: 5000,
121
+ });
122
+ const transfers = generator.generate([subway], stops);
123
+
124
+ const subwayToFar = (transfers.get(subway.id) ?? []).some(
125
+ (t) => t.destination === far.id,
126
+ );
127
+ assert.ok(subwayToFar);
128
+ });
129
+
130
+ it('ignores stops without coordinates', () => {
131
+ const generator = new StraightLineTransferGenerator({
132
+ maxDistanceMeters: 5000,
133
+ });
134
+ const transfers = generator.generate([subway, noCoords], stops);
135
+
136
+ assert.ok(!transfers.has(noCoords.id));
137
+ for (const stopTransfers of transfers.values()) {
138
+ assert.ok(!stopTransfers.some((t) => t.destination === noCoords.id));
139
+ }
140
+ });
141
+
142
+ it('derives the transfer time from distance, detour and walking speed', () => {
143
+ const walkingSpeedKmh = 4.5;
144
+ const detourFactor = 1.3;
145
+ const generator = new StraightLineTransferGenerator({
146
+ maxDistanceMeters: 500,
147
+ walkingSpeedKmh,
148
+ detourFactor,
149
+ changePenaltyMinutes: 0,
150
+ });
151
+ const transfers = generator.generate([subway], stops);
152
+ const subwayToTram = (transfers.get(subway.id) ?? []).find(
153
+ (t) => t.destination === tram.id,
154
+ );
155
+ assert.ok(subwayToTram);
156
+
157
+ const straightLineKm = distance(subwayLon, subwayLat, tramLon, tramLat);
158
+ const expected = Math.round(
159
+ ((straightLineKm * detourFactor) / walkingSpeedKmh) * 60,
160
+ );
161
+ assert.strictEqual(subwayToTram.minTransferTime, expected);
162
+ });
163
+
164
+ // Walking time between the subway and tram at the default speed and detour,
165
+ // with the change penalty disabled, as a baseline the penalty test builds on.
166
+ // Derived from the generator so it tracks the default speed/detour.
167
+ const baseWalkingTime = (() => {
168
+ const generator = new StraightLineTransferGenerator({
169
+ maxDistanceMeters: 500,
170
+ changePenaltyMinutes: 0,
171
+ });
172
+ const transfer = generator
173
+ .generate([subway], stops)
174
+ .get(subway.id)
175
+ ?.find((t) => t.destination === tram.id);
176
+ assert.ok(transfer?.minTransferTime !== undefined);
177
+ return transfer.minTransferTime;
178
+ })();
179
+
180
+ it('adds a flat change penalty to every transfer', () => {
181
+ const generator = new StraightLineTransferGenerator({
182
+ maxDistanceMeters: 500,
183
+ changePenaltyMinutes: 5,
184
+ });
185
+ const transfers = generator.generate([subway], stops);
186
+ const subwayToTram = (transfers.get(subway.id) ?? []).find(
187
+ (t) => t.destination === tram.id,
188
+ );
189
+ assert.ok(subwayToTram);
190
+ assert.strictEqual(subwayToTram.minTransferTime, baseWalkingTime + 5);
191
+ });
192
+
193
+ it('applies the default change penalty out of the box', () => {
194
+ // Default changePenaltyMinutes is 3.
195
+ const generator = new StraightLineTransferGenerator({
196
+ maxDistanceMeters: 500,
197
+ });
198
+ const transfers = generator.generate([subway], stops);
199
+ const subwayToTram = (transfers.get(subway.id) ?? []).find(
200
+ (t) => t.destination === tram.id,
201
+ );
202
+ assert.strictEqual(subwayToTram?.minTransferTime, baseWalkingTime + 3);
203
+ });
204
+ });
@@ -0,0 +1,45 @@
1
+ import { Stop, StopId } from '../stops/stops.js';
2
+ import { StopsIndex } from '../stops/stopsIndex.js';
3
+ import { Transfer } from '../timetable/timetable.js';
4
+
5
+ /**
6
+ * A map of directed transfers keyed by their origin stop id. Structurally
7
+ * identical to the parser's `TransfersMap`, so generated transfers can be
8
+ * merged directly into a feed's parsed transfers.
9
+ */
10
+ export type GeneratedTransfers = Map<StopId, Transfer[]>;
11
+
12
+ /**
13
+ * Strategy for synthesizing additional stop-to-stop (walking) transfers that
14
+ * are missing from a transit feed.
15
+ *
16
+ * It runs as a pre-processing step between parsing and serialization and is
17
+ * therefore agnostic of the source parser: it depends only on the generic
18
+ * stops model, not on any GTFS-specific types. This keeps the door open for
19
+ * smarter generators (e.g. an OSM pedestrian router) and for reuse across
20
+ * parsers.
21
+ */
22
+ export interface TransferGenerator {
23
+ /**
24
+ * Generates candidate directed transfers between nearby stops.
25
+ *
26
+ * Implementations connect each origin to its nearby stops as directed edges
27
+ * (emit `origin -> neighbor`; the reverse edge is produced when the neighbor
28
+ * is itself visited as an origin). The caller deduplicates the result against
29
+ * the transfers already present in the feed and only keeps the relevant ones,
30
+ * so an implementation can freely emit a transfer for every nearby pair
31
+ * without tracking what already exists.
32
+ *
33
+ * Implementations may be synchronous (return the transfers directly) or
34
+ * asynchronous (return a promise, e.g. when calling out to a routing engine);
35
+ * the caller awaits the result either way.
36
+ *
37
+ * @param originStops - Stops to generate outgoing transfers from.
38
+ * @param stops - Index of all stops, providing coordinates and geo search.
39
+ * @returns Candidate transfers to merge into the feed's transfers.
40
+ */
41
+ generate(
42
+ originStops: Stop[],
43
+ stops: StopsIndex,
44
+ ): GeneratedTransfers | Promise<GeneratedTransfers>;
45
+ }
@@ -0,0 +1,143 @@
1
+ import { distance } from 'geokdbush';
2
+
3
+ import { Stop } from '../stops/stops.js';
4
+ import { StopsIndex } from '../stops/stopsIndex.js';
5
+ import { Duration } from '../timetable/time.js';
6
+ import { Transfer, TransferTypes } from '../timetable/timetable.js';
7
+ import { getOrInsert } from '../utils/map.js';
8
+ import { GeneratedTransfers, TransferGenerator } from './generator.js';
9
+
10
+ export type StraightLineTransferGeneratorOptions = {
11
+ /**
12
+ * Maximum straight-line distance between two stops for a transfer to be generated, in meters.
13
+ */
14
+ maxDistanceMeters?: number;
15
+
16
+ /**
17
+ * Assumed pedestrian walking speed, in kilometers per hour.
18
+ */
19
+ walkingSpeedKmh?: number;
20
+
21
+ /**
22
+ * Multiplier applied to the straight-line distance to approximate the real
23
+ * walking path length. Use 1 to disable.
24
+ */
25
+ detourFactor?: number;
26
+
27
+ /**
28
+ * Flat penalty, in minutes, added to every generated transfer to account for
29
+ * the general overhead of changing (orientation, walking through the
30
+ * station). Use 0 to disable.
31
+ */
32
+ changePenaltyMinutes?: number;
33
+ };
34
+
35
+ const DEFAULT_MAX_DISTANCE_METERS = 500;
36
+ const DEFAULT_WALKING_SPEED_KMH = 4;
37
+ const DEFAULT_DETOUR_FACTOR = 1.3;
38
+ const DEFAULT_CHANGE_PENALTY_MINUTES = 3;
39
+
40
+ /**
41
+ * A {@link TransferGenerator} that connects geographically close stops with
42
+ * virtual walking transfers.
43
+ *
44
+ * The transfer time is derived from the straight-line (great-circle / haversine)
45
+ * distance between two stops, scaled up by a detour factor to approximate the
46
+ * real walking path and divided by an assumed walking speed. This is a
47
+ * deliberately crude heuristic meant to fill in footpaths for feeds that
48
+ * under-specify `transfers.txt` (e.g. large interchanges where subway, tram and
49
+ * bus stops sit close together but are left unconnected).
50
+ *
51
+ * Generated transfers use {@link TransferTypes.REQUIRES_MINIMAL_TIME} so they
52
+ * are treated as walking connections by both the router's transfer step and
53
+ * its access/egress computation. Each origin is connected to its nearby stops
54
+ * with a single directed edge; the reverse edge is produced when the neighbor
55
+ * is itself visited as an origin.
56
+ *
57
+ * On top of the walking time, a flat `changePenaltyMinutes` is added for the
58
+ * general overhead of any change. It is symmetric, so the two directions of a
59
+ * transfer share the same minimum time.
60
+ */
61
+ export class StraightLineTransferGenerator implements TransferGenerator {
62
+ private readonly maxDistanceMeters: number;
63
+ private readonly walkingSpeedKmh: number;
64
+ private readonly detourFactor: number;
65
+ private readonly changePenaltyMinutes: number;
66
+
67
+ constructor(options: StraightLineTransferGeneratorOptions = {}) {
68
+ this.maxDistanceMeters =
69
+ options.maxDistanceMeters ?? DEFAULT_MAX_DISTANCE_METERS;
70
+ this.walkingSpeedKmh = options.walkingSpeedKmh ?? DEFAULT_WALKING_SPEED_KMH;
71
+ this.detourFactor = options.detourFactor ?? DEFAULT_DETOUR_FACTOR;
72
+ this.changePenaltyMinutes =
73
+ options.changePenaltyMinutes ?? DEFAULT_CHANGE_PENALTY_MINUTES;
74
+ }
75
+
76
+ generate(originStops: Stop[], stops: StopsIndex): GeneratedTransfers {
77
+ const generated: GeneratedTransfers = new Map();
78
+ const maxDistanceKm = this.maxDistanceMeters / 1000;
79
+
80
+ for (const origin of originStops) {
81
+ const { id, lat, lon } = origin;
82
+ if (lat === undefined || lon === undefined) {
83
+ continue;
84
+ }
85
+ const neighbors = stops.findStopsByLocation(
86
+ lat,
87
+ lon,
88
+ Infinity,
89
+ maxDistanceKm,
90
+ );
91
+ for (const neighbor of neighbors) {
92
+ const { id: neighborId, lat: neighborLat, lon: neighborLon } = neighbor;
93
+ if (
94
+ neighborId === id ||
95
+ neighborLat === undefined ||
96
+ neighborLon === undefined
97
+ ) {
98
+ continue;
99
+ }
100
+ const walkingTime = this.walkingDuration(
101
+ lon,
102
+ lat,
103
+ neighborLon,
104
+ neighborLat,
105
+ );
106
+ // Emit a single directed edge; the reverse edge is emitted when the
107
+ // neighbor is itself visited as an origin. The penalty is symmetric, so
108
+ // both directions end up with the same minimum time.
109
+ const minTransferTime = walkingTime + this.changePenaltyMinutes;
110
+ this.addTransfer(generated, id, neighborId, minTransferTime);
111
+ }
112
+ }
113
+ return generated;
114
+ }
115
+
116
+ private addTransfer(
117
+ transfers: GeneratedTransfers,
118
+ from: number,
119
+ to: number,
120
+ minTransferTime: Duration,
121
+ ): void {
122
+ const transfer: Transfer = {
123
+ destination: to,
124
+ type: TransferTypes.REQUIRES_MINIMAL_TIME,
125
+ minTransferTime,
126
+ };
127
+ getOrInsert(transfers, from, []).push(transfer);
128
+ }
129
+
130
+ /**
131
+ * Estimates the walking time between two coordinates, in whole minutes.
132
+ */
133
+ private walkingDuration(
134
+ lon1: number,
135
+ lat1: number,
136
+ lon2: number,
137
+ lat2: number,
138
+ ): Duration {
139
+ const straightLineKm = distance(lon1, lat1, lon2, lat2);
140
+ const walkingKm = straightLineKm * this.detourFactor;
141
+ return Math.round((walkingKm / this.walkingSpeedKmh) * 60);
142
+ }
143
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Returns the value stored under `key`, inserting and returning `defaultValue`
3
+ * when the key is absent. Mirrors the TC39 `Map.prototype.getOrInsert`
4
+ * proposal.
5
+ *
6
+ * TODO: remove this helper and call `map.getOrInsert(key, defaultValue)`
7
+ * directly once we only target Node 26+ (where the method is built in).
8
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/getOrInsert
9
+ */
10
+ export const getOrInsert = <K, V>(
11
+ map: Map<K, V>,
12
+ key: K,
13
+ defaultValue: V,
14
+ ): V => {
15
+ if (map.has(key)) {
16
+ // Key is present, so `get` returns the stored value (which may itself be a
17
+ // legitimately-stored `undefined`).
18
+ return map.get(key) as V;
19
+ }
20
+ map.set(key, defaultValue);
21
+ return defaultValue;
22
+ };