minotor 11.3.0 → 11.5.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.
@@ -13,6 +13,7 @@ import {
13
13
  TripTransfers as TripTransfers,
14
14
  } from '../timetable/timetable.js';
15
15
  import { encode } from '../timetable/tripStopId.js';
16
+ import { ServiceId, ServiceIds } from './services.js';
16
17
  import { GtfsStopsMap } from './stops.js';
17
18
  import { GtfsTripId, TripsMapping } from './trips.js';
18
19
  import { parseCsv } from './utils.js';
@@ -27,6 +28,15 @@ export type GtfsTransferType =
27
28
 
28
29
  export type TransfersMap = Map<StopId, Transfer[]>;
29
30
 
31
+ /**
32
+ * Directed stop pairs for which the feed explicitly disallows transfers.
33
+ *
34
+ * These constraints are retained during parsing so generated transfers do not
35
+ * accidentally re-enable a connection declared with GTFS transfer_type=3.
36
+ * They are not serialized into the routing timetable.
37
+ */
38
+ export type ForbiddenTransfersMap = Map<StopId, Set<StopId>>;
39
+
30
40
  export type GtfsTripTransfer = {
31
41
  fromStop: StopId;
32
42
  fromTrip: GtfsTripId;
@@ -41,6 +51,7 @@ export type TransferEntry = {
41
51
  to_trip_id?: GtfsTripId;
42
52
  from_route_id?: ServiceRouteId;
43
53
  to_route_id?: ServiceRouteId;
54
+ service_id?: ServiceId;
44
55
  transfer_type: GtfsTransferType;
45
56
  min_transfer_time?: number;
46
57
  };
@@ -180,18 +191,23 @@ const processStopToStopTransfer = (
180
191
  /**
181
192
  * Parses the transfers.txt file from a GTFS feed.
182
193
  *
183
- * @param stopsStream The readable stream containing the stops data.
184
- * @return A mapping of stop IDs to corresponding stop details.
194
+ * @param transfersStream The readable stream containing the transfers data.
195
+ * @param stopsMap The parsed GTFS stops indexed by their source IDs.
196
+ * @param activeServiceIds The service IDs active for the requested date.
197
+ * @returns Parsed stop transfers, trip continuations, and guaranteed trip transfers.
185
198
  */
186
199
  export const parseTransfers = async (
187
200
  transfersStream: NodeJS.ReadableStream,
188
201
  stopsMap: GtfsStopsMap,
202
+ activeServiceIds: ServiceIds,
189
203
  ): Promise<{
190
204
  transfers: TransfersMap;
205
+ forbiddenTransfers: ForbiddenTransfersMap;
191
206
  tripContinuations: GtfsTripTransfer[];
192
207
  guaranteedTripTransfers: GtfsTripTransfer[];
193
208
  }> => {
194
209
  const transfers: TransfersMap = new Map();
210
+ const forbiddenTransfers: ForbiddenTransfersMap = new Map();
195
211
  const tripContinuations: GtfsTripTransfer[] = [];
196
212
  const guaranteedTripTransfers: GtfsTripTransfer[] = [];
197
213
 
@@ -202,12 +218,18 @@ export const parseTransfers = async (
202
218
  const transferEntry = rawLine as TransferEntry;
203
219
 
204
220
  if (
205
- transferEntry.transfer_type === 3 ||
206
- transferEntry.transfer_type === 5
221
+ transferEntry.service_id &&
222
+ !activeServiceIds.has(transferEntry.service_id)
207
223
  ) {
208
224
  continue;
209
225
  }
210
226
 
227
+ // Type 5 only prevents remaining seated between two specific trips. It
228
+ // does not prohibit an ordinary stop-to-stop transfer.
229
+ if (transferEntry.transfer_type === 5) {
230
+ continue;
231
+ }
232
+
211
233
  if (!transferEntry.from_stop_id || !transferEntry.to_stop_id) {
212
234
  log.warn(`Missing transfer origin or destination stop.`);
213
235
  continue;
@@ -255,6 +277,24 @@ export const parseTransfers = async (
255
277
  );
256
278
  }
257
279
  break;
280
+ case 3: {
281
+ if (
282
+ transferEntry.from_trip_id ||
283
+ transferEntry.to_trip_id ||
284
+ transferEntry.from_route_id ||
285
+ transferEntry.to_route_id
286
+ ) {
287
+ log.warn(
288
+ `Unsupported transfer of type 3 with trip or route constraints: from_trip_id=${transferEntry.from_trip_id}, to_trip_id=${transferEntry.to_trip_id}, from_route_id=${transferEntry.from_route_id}, to_route_id=${transferEntry.to_route_id}.`,
289
+ );
290
+ break;
291
+ }
292
+ const forbiddenDestinations =
293
+ forbiddenTransfers.get(fromStop.id) ?? new Set<StopId>();
294
+ forbiddenDestinations.add(toStop.id);
295
+ forbiddenTransfers.set(fromStop.id, forbiddenDestinations);
296
+ break;
297
+ }
258
298
  case 0: // Recommended transfer
259
299
  case 2: // Requires minimal time
260
300
  default:
@@ -270,11 +310,113 @@ export const parseTransfers = async (
270
310
 
271
311
  return {
272
312
  transfers,
313
+ forbiddenTransfers,
273
314
  tripContinuations,
274
315
  guaranteedTripTransfers,
275
316
  };
276
317
  };
277
318
 
319
+ /**
320
+ * Adds missing directed transfers between route-served child stops belonging
321
+ * to the same parent station.
322
+ *
323
+ * GTFS feeds commonly use parent_station to group platforms while omitting the
324
+ * corresponding transfers.txt rows. The generated transfers intentionally do
325
+ * not carry a fixed duration: routing applies the query's fallback minimum
326
+ * transfer time. Explicit and forbidden feed entries always take precedence.
327
+ *
328
+ * @returns The number of directed sibling transfers added.
329
+ */
330
+ export const addMissingSiblingTransfers = (
331
+ stopsMap: GtfsStopsMap,
332
+ activeStops: ReadonlySet<StopId>,
333
+ transfers: TransfersMap,
334
+ forbiddenTransfers: ForbiddenTransfersMap = new Map(),
335
+ ): number => {
336
+ let addedTransfers = 0;
337
+
338
+ for (const parent of stopsMap.values()) {
339
+ const activeChildren = parent.children.filter((child) =>
340
+ activeStops.has(child),
341
+ );
342
+ if (activeChildren.length < 2) continue;
343
+
344
+ for (const fromStop of activeChildren) {
345
+ const existing = transfers.get(fromStop) ?? [];
346
+ const connected = new Set(
347
+ existing.map((transfer) => transfer.destination),
348
+ );
349
+ const forbidden = forbiddenTransfers.get(fromStop);
350
+
351
+ for (const toStop of activeChildren) {
352
+ if (
353
+ toStop === fromStop ||
354
+ connected.has(toStop) ||
355
+ forbidden?.has(toStop)
356
+ ) {
357
+ continue;
358
+ }
359
+ existing.push({
360
+ destination: toStop,
361
+ type: TransferTypes.REQUIRES_MINIMAL_TIME,
362
+ });
363
+ connected.add(toStop);
364
+ addedTransfers += 1;
365
+ }
366
+
367
+ if (existing.length > 0) {
368
+ transfers.set(fromStop, existing);
369
+ }
370
+ }
371
+ }
372
+
373
+ return addedTransfers;
374
+ };
375
+
376
+ /**
377
+ * Merges generated transfers into parsed feed transfers.
378
+ *
379
+ * Only transfers between active stops are kept. Explicit feed transfers and
380
+ * forbidden directed pairs take precedence over generated candidates.
381
+ *
382
+ * @returns The number of generated transfers added.
383
+ */
384
+ export const addGeneratedTransfers = (
385
+ generatedTransfers: ReadonlyMap<StopId, readonly Transfer[]>,
386
+ activeStops: ReadonlySet<StopId>,
387
+ transfers: TransfersMap,
388
+ forbiddenTransfers: ForbiddenTransfersMap = new Map(),
389
+ ): number => {
390
+ let addedTransfers = 0;
391
+
392
+ for (const [fromStop, candidates] of generatedTransfers) {
393
+ if (!activeStops.has(fromStop)) continue;
394
+
395
+ const existing = transfers.get(fromStop) ?? [];
396
+ const connected = new Set(existing.map((transfer) => transfer.destination));
397
+ const forbidden = forbiddenTransfers.get(fromStop);
398
+
399
+ for (const transfer of candidates) {
400
+ if (
401
+ !activeStops.has(transfer.destination) ||
402
+ connected.has(transfer.destination) ||
403
+ forbidden?.has(transfer.destination)
404
+ ) {
405
+ continue;
406
+ }
407
+ connected.add(transfer.destination);
408
+ existing.push(transfer);
409
+ addedTransfers += 1;
410
+ }
411
+
412
+ if (existing.length > 0) {
413
+ transfers.set(fromStop, existing);
414
+ }
415
+ }
416
+
417
+ return addedTransfers;
418
+ };
419
+
278
420
  /**
279
421
  * Disambiguates stops involved in a transfer.
280
422
  *