|
| 1 | +package com.conveyal.r5.transit; |
| 2 | + |
| 3 | +import com.conveyal.gtfs.GTFSFeed; |
| 4 | +import com.conveyal.gtfs.model.Transfer; |
| 5 | +import gnu.trove.list.TIntList; |
| 6 | +import gnu.trove.list.array.TIntArrayList; |
| 7 | +import gnu.trove.map.TIntObjectMap; |
| 8 | +import gnu.trove.map.TObjectIntMap; |
| 9 | +import gnu.trove.map.hash.TIntObjectHashMap; |
| 10 | +import gnu.trove.map.hash.TObjectIntHashMap; |
| 11 | + |
| 12 | +import java.util.BitSet; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import static com.conveyal.r5.analyst.cluster.TransportNetworkConfig.TransferConfig; |
| 17 | +import static com.conveyal.r5.analyst.cluster.TransportNetworkConfig.TransferConfig.*; |
| 18 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 19 | + |
| 20 | +/** |
| 21 | + * Transfers between transit stops can come from two sources: transfers.txt in GTFS inputs and |
| 22 | + * routing through the OSM-derived street network. This class handles loading from transfers.txt and |
| 23 | + * retains enough information so the subsequent street routing approach can adopt the behavior |
| 24 | + * specified with TransportNetworkConfig.TransferConfig. The way in which GTFS transfers take |
| 25 | + * priority over transfers found through the street network can be configured. |
| 26 | + * <p> |
| 27 | + * There can be multiple GTFS feeds and they are loaded in a streaming fashion, with only one open |
| 28 | + * and consuming memory at a time. Therefore we lose a lot of context by the time the street routing |
| 29 | + * happens. |
| 30 | + * <p> |
| 31 | + * Use a single instance across all GTFS feeds. Call the load method once on each feed in turn. This |
| 32 | + * accumulates information from all feeds that will later be important for finding transfers through |
| 33 | + * the OSM street network. |
| 34 | + * <p> |
| 35 | + * The GTFS transfers we load are specified in terms of minimum time, while the street transfers are |
| 36 | + * stored as distances and resolved to time during searches based on the specified walk speed. On |
| 37 | + * the downside this requires two separate data structures, but on the upside it simplifies loading |
| 38 | + * because GTFS must be loaded one feed at a time, while on-street transfers must be found later |
| 39 | + * after all GTFS feeds are loaded and all stops linked (because street transfers are expected to |
| 40 | + * connect stops from different feeds). |
| 41 | + * <p> |
| 42 | + * TODO Further increase transfer time accuracy using GTFS pathways.txt |
| 43 | + */ |
| 44 | +public class GtfsTransferLoader { |
| 45 | + |
| 46 | + final TransitLayer transit; |
| 47 | + final TransferConfig transferConfig; |
| 48 | + |
| 49 | + int nMissingStop = 0; |
| 50 | + int nUnsupportedSpecificity = 0; |
| 51 | + int nUnsupportedTransferType = 0; |
| 52 | + TObjectIntMap<String> otherErrors = new TObjectIntHashMap<>(); |
| 53 | + |
| 54 | + public record StopPair(int fromStop, int toStop) { } |
| 55 | + |
| 56 | + final BitSet stopsWithTransfers = new BitSet(); |
| 57 | + final Set<StopPair> stopPairsWithTransfers = new HashSet<>(); |
| 58 | + // Transfer keys are from-stop indexes, values are packed lists of (to-stop, distance) pairs. |
| 59 | + final TIntObjectMap<TIntList> transfers = new TIntObjectHashMap<>(); |
| 60 | + |
| 61 | + public GtfsTransferLoader (TransitLayer transit, TransferConfig transferConfig) { |
| 62 | + this.transit = transit; |
| 63 | + this.transferConfig = transferConfig; |
| 64 | + } |
| 65 | + |
| 66 | + public void loadTransfersTxt (GTFSFeed feed) { |
| 67 | + if (transferConfig == OSM_ONLY) return; |
| 68 | + if (feed.transfers == null || feed.transfers.isEmpty()) return; |
| 69 | + // The keys of GtfsFeed.transfers are just arbitrary unique numbers (the input line numbers). |
| 70 | + for (Transfer transfer : feed.transfers.values()) { |
| 71 | + if (shouldSkipTransfer(transfer)) continue; |
| 72 | + int from = transit.indexForStopId.get(transfer.from_stop_id); |
| 73 | + int to = transit.indexForStopId.get(transfer.to_stop_id); |
| 74 | + if (untrue(from < 0 || to < 0, "Transfer references stop that was not loaded.")) continue; |
| 75 | + if (untrue(transfer.min_transfer_time < 0, "Negative transfer times not allowed.")) continue; |
| 76 | + if (untrue(transfer.min_transfer_time > 3600, "Transfer time suspiciously high.")) continue; |
| 77 | + TIntList packedTransfers = transfers.get(from); |
| 78 | + if (packedTransfers == null) { |
| 79 | + packedTransfers = new TIntArrayList(); |
| 80 | + transfers.put(from, packedTransfers); |
| 81 | + } |
| 82 | + packedTransfers.add(to); |
| 83 | + packedTransfers.add(transfer.min_transfer_time); |
| 84 | + stopsWithTransfers.set(from); |
| 85 | + stopsWithTransfers.set(to); |
| 86 | + // Conditional to avoid excessive object instance bloat when unused. |
| 87 | + if (transferConfig == PER_STOP_PAIR) { |
| 88 | + stopPairsWithTransfers.add(new StopPair(from, to)); |
| 89 | + } |
| 90 | + } |
| 91 | + if (transferConfig == PER_FEED) { |
| 92 | + throw new UnsupportedOperationException(); |
| 93 | + // Prevent later on-street transfer calculation for every stop in this feed. |
| 94 | + // However, this behavior probably needs to be different for inter- and intra-feed transfers. |
| 95 | + // And it may need to be manually set independently for each individual feed. |
| 96 | + // for (String stopId : feed.stops.keySet()) { |
| 97 | + // int stopIndex = transit.indexForStopId.get(stopId); |
| 98 | + // if (stopIndex > 0) stopsWithTransfers.set(stopIndex); |
| 99 | + // } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // TODO encapsulate and reuse elsewhere. |
| 104 | + private boolean untrue (boolean condition, String errorMessage) { |
| 105 | + if (condition) otherErrors.adjustOrPutValue(errorMessage, 1, 1); |
| 106 | + return !condition; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Validate one transfer and decide whether it should be processed by this class, maintaining |
| 111 | + * some counts and flags for debugging and status reporting. |
| 112 | + */ |
| 113 | + private boolean shouldSkipTransfer (Transfer transfer) { |
| 114 | + boolean skip = false; |
| 115 | + if (!(isNullOrEmpty(transfer.from_route_id) && isNullOrEmpty(transfer.from_trip_id) && |
| 116 | + isNullOrEmpty(transfer.to_route_id) && isNullOrEmpty(transfer.to_trip_id))) { |
| 117 | + nUnsupportedSpecificity += 1; |
| 118 | + skip = true; |
| 119 | + } |
| 120 | + if (isNullOrEmpty(transfer.from_stop_id) || isNullOrEmpty(transfer.to_stop_id)) { |
| 121 | + nMissingStop += 1; |
| 122 | + skip = true; |
| 123 | + } |
| 124 | + if (transfer.transfer_type < 2) { |
| 125 | + nUnsupportedTransferType += 1; |
| 126 | + skip = true; |
| 127 | + } |
| 128 | + if (transfer.transfer_type > 3) { |
| 129 | + // In-seat transfer information may be "supported", but not consumed by this class. |
| 130 | + skip = true; |
| 131 | + } |
| 132 | + return skip; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * This is an optimization to avoid a slow street search when every transfer it yields will be ignored (because |
| 137 | + * every stop pair involving this source stop is slated to be skipped). |
| 138 | + * @return whether the osm street transfer generation should skip producing any transfers from the given stop. |
| 139 | + */ |
| 140 | + public boolean shouldSkipFromStop (int fromStopIndex) { |
| 141 | + if (transferConfig == PER_STOP) { |
| 142 | + return stopsWithTransfers.get(fromStopIndex); |
| 143 | + } |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * A GTFS transfer between a specific pair of stops or involving particular stops may take priority over any |
| 149 | + * transfer found by routing through the OSM street network. |
| 150 | + * @return whether osm street transfer generation should skip making transfers between the given pair of stops. |
| 151 | + */ |
| 152 | + public boolean shouldSkipStopPair (int fromStopIndex, int toStopIndex) { |
| 153 | + if (transferConfig == PER_STOP_PAIR) { |
| 154 | + return stopPairsWithTransfers.contains(new StopPair(fromStopIndex, toStopIndex)); |
| 155 | + } else if (transferConfig == PER_STOP) { |
| 156 | + return stopsWithTransfers.get(fromStopIndex) || stopsWithTransfers.get(toStopIndex); |
| 157 | + } |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments