minotor 11.2.3 → 11.4.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.
- package/CHANGELOG.md +3 -3
- package/README.md +27 -0
- package/dist/cli.mjs +310 -102
- package/dist/cli.mjs.map +1 -1
- package/dist/gtfs/parser.d.ts +3 -1
- package/dist/gtfs/routes.d.ts +1 -2
- package/dist/gtfs/transfers.d.ts +7 -3
- package/dist/gtfs/trips.d.ts +2 -1
- package/dist/parser.cjs.js +235 -85
- package/dist/parser.cjs.js.map +1 -1
- package/dist/parser.d.ts +6 -2
- package/dist/parser.esm.js +234 -86
- package/dist/parser.esm.js.map +1 -1
- package/dist/router.cjs.js +1 -1
- package/dist/router.cjs.js.map +1 -1
- package/dist/router.d.ts +4 -3
- package/dist/router.esm.js +1 -1
- package/dist/router.esm.js.map +1 -1
- package/dist/router.umd.js +1 -1
- package/dist/router.umd.js.map +1 -1
- package/dist/routing/result.d.ts +1 -4
- package/dist/routing/route.d.ts +9 -5
- package/dist/timetable/route.d.ts +9 -5
- package/dist/timetable/timetable.d.ts +24 -3
- package/dist/transfers/__tests__/straightLineTransferGenerator.test.d.ts +1 -0
- package/dist/transfers/generator.d.ts +40 -0
- package/dist/transfers/straightLineTransferGenerator.d.ts +58 -0
- package/dist/utils/map.d.ts +10 -0
- package/package.json +1 -1
- package/src/__e2e__/router.test.ts +6 -2
- package/src/cli/minotor.ts +21 -1
- package/src/cli/repl.ts +6 -12
- package/src/gtfs/__tests__/parser.test.ts +29 -1
- package/src/gtfs/__tests__/routes.test.ts +4 -3
- package/src/gtfs/__tests__/transfers.test.ts +108 -21
- package/src/gtfs/__tests__/trips.test.ts +60 -24
- package/src/gtfs/parser.ts +51 -2
- package/src/gtfs/profiles/__tests__/extended.test.ts +2 -1
- package/src/gtfs/profiles/extended.ts +14 -14
- package/src/gtfs/profiles/standard.ts +11 -10
- package/src/gtfs/routes.ts +5 -2
- package/src/gtfs/transfers.ts +20 -7
- package/src/gtfs/trips.ts +7 -14
- package/src/parser.ts +20 -2
- package/src/router.ts +12 -6
- package/src/routing/__tests__/access.test.ts +14 -6
- package/src/routing/__tests__/plainRouter.test.ts +25 -23
- package/src/routing/__tests__/plotter.test.ts +8 -3
- package/src/routing/__tests__/rangeResult.test.ts +6 -2
- package/src/routing/__tests__/rangeRouter.test.ts +10 -6
- package/src/routing/__tests__/raptor.test.ts +15 -9
- package/src/routing/__tests__/result.test.ts +9 -4
- package/src/routing/__tests__/route.test.ts +3 -4
- package/src/routing/__tests__/router.test.ts +2 -1
- package/src/routing/access.ts +7 -4
- package/src/routing/plotter.ts +8 -3
- package/src/routing/raptor.ts +5 -5
- package/src/routing/result.ts +32 -14
- package/src/routing/route.ts +13 -10
- package/src/routing/state.ts +1 -1
- package/src/timetable/__tests__/io.test.ts +22 -10
- package/src/timetable/__tests__/route.test.ts +18 -24
- package/src/timetable/__tests__/timetable.test.ts +17 -11
- package/src/timetable/io.ts +30 -28
- package/src/timetable/route.ts +14 -7
- package/src/timetable/timetable.ts +77 -28
- package/src/transfers/README.md +97 -0
- package/src/transfers/__tests__/straightLineTransferGenerator.test.ts +204 -0
- package/src/transfers/generator.ts +45 -0
- package/src/transfers/straightLineTransferGenerator.ts +143 -0
- package/src/utils/map.ts +22 -0
package/src/timetable/io.ts
CHANGED
|
@@ -9,11 +9,13 @@ import {
|
|
|
9
9
|
import { Route } from './route.js';
|
|
10
10
|
import {
|
|
11
11
|
RouteType,
|
|
12
|
+
RouteTypes,
|
|
12
13
|
ServiceRoute,
|
|
13
14
|
ServiceRouteId,
|
|
14
15
|
StopAdjacency,
|
|
15
16
|
Transfer,
|
|
16
17
|
TransferType,
|
|
18
|
+
TransferTypes,
|
|
17
19
|
TripStop,
|
|
18
20
|
TripTransfers,
|
|
19
21
|
} from './timetable.js';
|
|
@@ -255,13 +257,13 @@ const parseTransferType = (type: ProtoTransferType): TransferType => {
|
|
|
255
257
|
case ProtoTransferType.TRANSFER_TYPE_UNSPECIFIED:
|
|
256
258
|
throw new Error('Unspecified protobuf transfer type.');
|
|
257
259
|
case ProtoTransferType.TRANSFER_TYPE_RECOMMENDED_TRANSFER_POINT:
|
|
258
|
-
return
|
|
260
|
+
return TransferTypes.RECOMMENDED;
|
|
259
261
|
case ProtoTransferType.TRANSFER_TYPE_TIMED_TRANSFER:
|
|
260
|
-
return
|
|
262
|
+
return TransferTypes.GUARANTEED;
|
|
261
263
|
case ProtoTransferType.TRANSFER_TYPE_REQUIRES_MINIMAL_TIME:
|
|
262
|
-
return
|
|
264
|
+
return TransferTypes.REQUIRES_MINIMAL_TIME;
|
|
263
265
|
case ProtoTransferType.TRANSFER_TYPE_IN_SEAT_TRANSFER:
|
|
264
|
-
return
|
|
266
|
+
return TransferTypes.IN_SEAT;
|
|
265
267
|
case ProtoTransferType.UNRECOGNIZED:
|
|
266
268
|
throw new Error('Unrecognized protobuf transfer type.');
|
|
267
269
|
}
|
|
@@ -269,13 +271,13 @@ const parseTransferType = (type: ProtoTransferType): TransferType => {
|
|
|
269
271
|
|
|
270
272
|
const serializeTransferType = (type: TransferType): ProtoTransferType => {
|
|
271
273
|
switch (type) {
|
|
272
|
-
case
|
|
274
|
+
case TransferTypes.RECOMMENDED:
|
|
273
275
|
return ProtoTransferType.TRANSFER_TYPE_RECOMMENDED_TRANSFER_POINT;
|
|
274
|
-
case
|
|
276
|
+
case TransferTypes.GUARANTEED:
|
|
275
277
|
return ProtoTransferType.TRANSFER_TYPE_TIMED_TRANSFER;
|
|
276
|
-
case
|
|
278
|
+
case TransferTypes.REQUIRES_MINIMAL_TIME:
|
|
277
279
|
return ProtoTransferType.TRANSFER_TYPE_REQUIRES_MINIMAL_TIME;
|
|
278
|
-
case
|
|
280
|
+
case TransferTypes.IN_SEAT:
|
|
279
281
|
return ProtoTransferType.TRANSFER_TYPE_IN_SEAT_TRANSFER;
|
|
280
282
|
}
|
|
281
283
|
};
|
|
@@ -285,25 +287,25 @@ const parseRouteType = (type: ProtoRouteType): RouteType => {
|
|
|
285
287
|
case ProtoRouteType.ROUTE_TYPE_UNSPECIFIED:
|
|
286
288
|
throw new Error('Unspecified protobuf route type.');
|
|
287
289
|
case ProtoRouteType.ROUTE_TYPE_TRAM:
|
|
288
|
-
return
|
|
290
|
+
return RouteTypes.TRAM;
|
|
289
291
|
case ProtoRouteType.ROUTE_TYPE_SUBWAY:
|
|
290
|
-
return
|
|
292
|
+
return RouteTypes.SUBWAY;
|
|
291
293
|
case ProtoRouteType.ROUTE_TYPE_RAIL:
|
|
292
|
-
return
|
|
294
|
+
return RouteTypes.RAIL;
|
|
293
295
|
case ProtoRouteType.ROUTE_TYPE_BUS:
|
|
294
|
-
return
|
|
296
|
+
return RouteTypes.BUS;
|
|
295
297
|
case ProtoRouteType.ROUTE_TYPE_FERRY:
|
|
296
|
-
return
|
|
298
|
+
return RouteTypes.FERRY;
|
|
297
299
|
case ProtoRouteType.ROUTE_TYPE_CABLE_TRAM:
|
|
298
|
-
return
|
|
300
|
+
return RouteTypes.CABLE_TRAM;
|
|
299
301
|
case ProtoRouteType.ROUTE_TYPE_AERIAL_LIFT:
|
|
300
|
-
return
|
|
302
|
+
return RouteTypes.AERIAL_LIFT;
|
|
301
303
|
case ProtoRouteType.ROUTE_TYPE_FUNICULAR:
|
|
302
|
-
return
|
|
304
|
+
return RouteTypes.FUNICULAR;
|
|
303
305
|
case ProtoRouteType.ROUTE_TYPE_TROLLEYBUS:
|
|
304
|
-
return
|
|
306
|
+
return RouteTypes.TROLLEYBUS;
|
|
305
307
|
case ProtoRouteType.ROUTE_TYPE_MONORAIL:
|
|
306
|
-
return
|
|
308
|
+
return RouteTypes.MONORAIL;
|
|
307
309
|
case ProtoRouteType.UNRECOGNIZED:
|
|
308
310
|
default:
|
|
309
311
|
throw new Error('Unrecognized protobuf route type.');
|
|
@@ -312,25 +314,25 @@ const parseRouteType = (type: ProtoRouteType): RouteType => {
|
|
|
312
314
|
|
|
313
315
|
const serializeRouteType = (type: RouteType): ProtoRouteType => {
|
|
314
316
|
switch (type) {
|
|
315
|
-
case
|
|
317
|
+
case RouteTypes.TRAM:
|
|
316
318
|
return ProtoRouteType.ROUTE_TYPE_TRAM;
|
|
317
|
-
case
|
|
319
|
+
case RouteTypes.SUBWAY:
|
|
318
320
|
return ProtoRouteType.ROUTE_TYPE_SUBWAY;
|
|
319
|
-
case
|
|
321
|
+
case RouteTypes.RAIL:
|
|
320
322
|
return ProtoRouteType.ROUTE_TYPE_RAIL;
|
|
321
|
-
case
|
|
323
|
+
case RouteTypes.BUS:
|
|
322
324
|
return ProtoRouteType.ROUTE_TYPE_BUS;
|
|
323
|
-
case
|
|
325
|
+
case RouteTypes.FERRY:
|
|
324
326
|
return ProtoRouteType.ROUTE_TYPE_FERRY;
|
|
325
|
-
case
|
|
327
|
+
case RouteTypes.CABLE_TRAM:
|
|
326
328
|
return ProtoRouteType.ROUTE_TYPE_CABLE_TRAM;
|
|
327
|
-
case
|
|
329
|
+
case RouteTypes.AERIAL_LIFT:
|
|
328
330
|
return ProtoRouteType.ROUTE_TYPE_AERIAL_LIFT;
|
|
329
|
-
case
|
|
331
|
+
case RouteTypes.FUNICULAR:
|
|
330
332
|
return ProtoRouteType.ROUTE_TYPE_FUNICULAR;
|
|
331
|
-
case
|
|
333
|
+
case RouteTypes.TROLLEYBUS:
|
|
332
334
|
return ProtoRouteType.ROUTE_TYPE_TROLLEYBUS;
|
|
333
|
-
case
|
|
335
|
+
case RouteTypes.MONORAIL:
|
|
334
336
|
return ProtoRouteType.ROUTE_TYPE_MONORAIL;
|
|
335
337
|
}
|
|
336
338
|
};
|
package/src/timetable/route.ts
CHANGED
|
@@ -11,12 +11,19 @@ import { ServiceRouteId } from './timetable.js';
|
|
|
11
11
|
*/
|
|
12
12
|
export type RouteId = number;
|
|
13
13
|
|
|
14
|
-
export
|
|
14
|
+
export const PickUpDropOffTypes = {
|
|
15
|
+
REGULAR: 0,
|
|
16
|
+
NOT_AVAILABLE: 1,
|
|
17
|
+
MUST_PHONE_AGENCY: 2,
|
|
18
|
+
MUST_COORDINATE_WITH_DRIVER: 3,
|
|
19
|
+
} as const;
|
|
15
20
|
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export
|
|
21
|
+
export type PickUpDropOffType =
|
|
22
|
+
(typeof PickUpDropOffTypes)[keyof typeof PickUpDropOffTypes];
|
|
23
|
+
|
|
24
|
+
export type PickUpDropOffTypeString = keyof typeof PickUpDropOffTypes;
|
|
25
|
+
|
|
26
|
+
export type RawPickUpDropOffType = PickUpDropOffType;
|
|
20
27
|
|
|
21
28
|
/*
|
|
22
29
|
* A trip route index corresponds to the index of a given trip in a route.
|
|
@@ -182,8 +189,8 @@ export class Route {
|
|
|
182
189
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
183
190
|
const stop = trip.stops[stopIndex]!;
|
|
184
191
|
const globalIndex = tripIndex * numStops + stopIndex;
|
|
185
|
-
const pickUp = stop.pickUpType ?? REGULAR;
|
|
186
|
-
const dropOff = stop.dropOffType ?? REGULAR;
|
|
192
|
+
const pickUp = stop.pickUpType ?? PickUpDropOffTypes.REGULAR;
|
|
193
|
+
const dropOff = stop.dropOffType ?? PickUpDropOffTypes.REGULAR;
|
|
187
194
|
const byteIndex = Math.floor(globalIndex / 2);
|
|
188
195
|
const isSecondPair = globalIndex % 2 === 1;
|
|
189
196
|
|
|
@@ -13,13 +13,26 @@ import {
|
|
|
13
13
|
serializeTripTransfers,
|
|
14
14
|
} from './io.js';
|
|
15
15
|
import { Timetable as ProtoTimetable } from './proto/v1/timetable.js';
|
|
16
|
-
import {
|
|
17
|
-
|
|
16
|
+
import {
|
|
17
|
+
PickUpDropOffTypes,
|
|
18
|
+
Route,
|
|
19
|
+
RouteId,
|
|
20
|
+
StopRouteIndex,
|
|
21
|
+
TripRouteIndex,
|
|
22
|
+
} from './route.js';
|
|
18
23
|
import { Duration, DURATION_ZERO, Time, TIME_ORIGIN } from './time.js';
|
|
19
24
|
import { encode, TripStopId } from './tripStopId.js';
|
|
20
25
|
|
|
21
|
-
export
|
|
22
|
-
|
|
26
|
+
export const TransferTypes = {
|
|
27
|
+
RECOMMENDED: 1,
|
|
28
|
+
GUARANTEED: 2,
|
|
29
|
+
REQUIRES_MINIMAL_TIME: 3,
|
|
30
|
+
IN_SEAT: 4,
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
export type TransferType = (typeof TransferTypes)[keyof typeof TransferTypes];
|
|
34
|
+
|
|
35
|
+
export type TransferTypeString = keyof typeof TransferTypes;
|
|
23
36
|
|
|
24
37
|
export type Transfer = {
|
|
25
38
|
destination: StopId;
|
|
@@ -42,17 +55,22 @@ export type TripTransfers = Map<TripStopId, TripStop[]>;
|
|
|
42
55
|
|
|
43
56
|
export type ServiceRouteId = number;
|
|
44
57
|
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
export const RouteTypes = {
|
|
59
|
+
TRAM: 1,
|
|
60
|
+
SUBWAY: 2,
|
|
61
|
+
RAIL: 3,
|
|
62
|
+
BUS: 4,
|
|
63
|
+
FERRY: 5,
|
|
64
|
+
CABLE_TRAM: 6,
|
|
65
|
+
AERIAL_LIFT: 7,
|
|
66
|
+
FUNICULAR: 8,
|
|
67
|
+
TROLLEYBUS: 9,
|
|
68
|
+
MONORAIL: 10,
|
|
69
|
+
} as const;
|
|
70
|
+
|
|
71
|
+
export type RouteType = (typeof RouteTypes)[keyof typeof RouteTypes];
|
|
72
|
+
|
|
73
|
+
export type RouteTypeString = keyof typeof RouteTypes;
|
|
56
74
|
|
|
57
75
|
// A service refers to a collection of trips that are displayed to riders as a single service.
|
|
58
76
|
// As opposed to a route which consists of the subset of trips from a service which shares the same list of stops.
|
|
@@ -64,18 +82,9 @@ export type ServiceRoute = {
|
|
|
64
82
|
};
|
|
65
83
|
export type ServiceRouteInfo = Omit<ServiceRoute, 'routes'>;
|
|
66
84
|
|
|
67
|
-
export const ALL_TRANSPORT_MODES: Set<RouteType> = new Set(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
'RAIL',
|
|
71
|
-
'BUS',
|
|
72
|
-
'FERRY',
|
|
73
|
-
'CABLE_TRAM',
|
|
74
|
-
'AERIAL_LIFT',
|
|
75
|
-
'FUNICULAR',
|
|
76
|
-
'TROLLEYBUS',
|
|
77
|
-
'MONORAIL',
|
|
78
|
-
]);
|
|
85
|
+
export const ALL_TRANSPORT_MODES: Set<RouteType> = new Set(
|
|
86
|
+
Object.values(RouteTypes),
|
|
87
|
+
);
|
|
79
88
|
|
|
80
89
|
const EMPTY_TRIP_BOARDINGS: TripStop[] = [];
|
|
81
90
|
|
|
@@ -367,7 +376,7 @@ export class Timetable {
|
|
|
367
376
|
|
|
368
377
|
for (let t = earliestTrip; t < (beforeTrip ?? nbTrips); t++) {
|
|
369
378
|
const pickup = route.pickUpTypeFrom(stopIndex, t);
|
|
370
|
-
if (pickup === NOT_AVAILABLE) {
|
|
379
|
+
if (pickup === PickUpDropOffTypes.NOT_AVAILABLE) {
|
|
371
380
|
continue;
|
|
372
381
|
}
|
|
373
382
|
if (fromTripStop === undefined) {
|
|
@@ -413,3 +422,43 @@ export class Timetable {
|
|
|
413
422
|
return guaranteedTripTransfers;
|
|
414
423
|
}
|
|
415
424
|
}
|
|
425
|
+
|
|
426
|
+
export const routeTypeToString = (type: RouteType): RouteTypeString => {
|
|
427
|
+
switch (type) {
|
|
428
|
+
case RouteTypes.TRAM:
|
|
429
|
+
return 'TRAM';
|
|
430
|
+
case RouteTypes.SUBWAY:
|
|
431
|
+
return 'SUBWAY';
|
|
432
|
+
case RouteTypes.RAIL:
|
|
433
|
+
return 'RAIL';
|
|
434
|
+
case RouteTypes.BUS:
|
|
435
|
+
return 'BUS';
|
|
436
|
+
case RouteTypes.FERRY:
|
|
437
|
+
return 'FERRY';
|
|
438
|
+
case RouteTypes.CABLE_TRAM:
|
|
439
|
+
return 'CABLE_TRAM';
|
|
440
|
+
case RouteTypes.AERIAL_LIFT:
|
|
441
|
+
return 'AERIAL_LIFT';
|
|
442
|
+
case RouteTypes.FUNICULAR:
|
|
443
|
+
return 'FUNICULAR';
|
|
444
|
+
case RouteTypes.TROLLEYBUS:
|
|
445
|
+
return 'TROLLEYBUS';
|
|
446
|
+
case RouteTypes.MONORAIL:
|
|
447
|
+
return 'MONORAIL';
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
export const transferTypeToString = (
|
|
452
|
+
type: TransferType,
|
|
453
|
+
): TransferTypeString => {
|
|
454
|
+
switch (type) {
|
|
455
|
+
case TransferTypes.RECOMMENDED:
|
|
456
|
+
return 'RECOMMENDED';
|
|
457
|
+
case TransferTypes.GUARANTEED:
|
|
458
|
+
return 'GUARANTEED';
|
|
459
|
+
case TransferTypes.REQUIRES_MINIMAL_TIME:
|
|
460
|
+
return 'REQUIRES_MINIMAL_TIME';
|
|
461
|
+
case TransferTypes.IN_SEAT:
|
|
462
|
+
return 'IN_SEAT';
|
|
463
|
+
}
|
|
464
|
+
};
|
|
@@ -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
|
+
}
|