Skip to content

Commit

Permalink
Split transfer generation into methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
VillePihlava committed Jan 16, 2025
1 parent c649ab7 commit 91782ef
Showing 1 changed file with 104 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,70 +141,15 @@ public void buildGraph() {

LOG.debug("Linking stop '{}' {}", stop, ts0);

// Calculate default transfers.
for (RouteRequest transferProfile : transferConfiguration.defaultTransferRequests()) {
StreetMode mode = transferProfile.journey().transfer().mode();
var nearbyStops = transferConfiguration
.defaultNearbyStopFinderForMode()
.get(mode)
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), false);
for (NearbyStop sd : nearbyStops) {
// Skip the origin stop, loop transfers are not needed.
if (sd.stop == stop) {
continue;
}
if (sd.stop.transfersNotAllowed()) {
continue;
}
createPathTransfer(stop, sd.stop, sd, distinctTransfers, mode);
}
}
// Calculate flex transfers if flex routing is enabled.
for (RouteRequest transferProfile : transferConfiguration.flexTransferRequests()) {
// Flex transfer requests only use the WALK mode.
StreetMode mode = StreetMode.WALK;
var nearbyStops = transferConfiguration
.defaultNearbyStopFinderForMode()
.get(mode)
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), true);
// This code is for finding transfers from AreaStops to Stops, transfers
// from Stops to AreaStops and between Stops are already covered above.
for (NearbyStop sd : nearbyStops) {
// Skip the origin stop, loop transfers are not needed.
if (sd.stop == stop) {
continue;
}
if (sd.stop instanceof RegularStop) {
continue;
}
// The TransferKey and PathTransfer are created differently for flex routing.
createPathTransfer(sd.stop, stop, sd, distinctTransfers, mode);
}
}
// Calculate transfers between stops that are visited by trips that allow cars, if configured.
if (carsAllowedStops.contains(stop)) {
for (RouteRequest transferProfile : transferConfiguration.carsAllowedStopTransferRequests()) {
StreetMode mode = transferProfile.journey().transfer().mode();
var nearbyStops = transferConfiguration
.carsAllowedStopNearbyStopFinderForMode()
.get(mode)
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), false);
for (NearbyStop sd : nearbyStops) {
// Skip the origin stop, loop transfers are not needed.
if (sd.stop == stop) {
continue;
}
if (sd.stop.transfersNotAllowed()) {
continue;
}
// Only calculate transfers between carsAllowedStops.
if (!carsAllowedStops.contains(sd.stop)) {
continue;
}
createPathTransfer(stop, sd.stop, sd, distinctTransfers, mode);
}
}
}
calculateDefaultTransfers(transferConfiguration, ts0, stop, distinctTransfers);
calculateFlexTransfers(transferConfiguration, ts0, stop, distinctTransfers);
calculateCarsAllowedTransfers(
transferConfiguration,
ts0,
stop,
distinctTransfers,
carsAllowedStops
);

LOG.debug(
"Linked stop {} with {} transfers to stops with different patterns.",
Expand Down Expand Up @@ -373,6 +318,101 @@ private TransferConfiguration parseTransferParameters(NearbyStopFinder nearbySto
);
}

/**
* This method calculates default transfers.
*/
private void calculateDefaultTransfers(
TransferConfiguration transferConfiguration,
TransitStopVertex ts0,
RegularStop stop,
Map<TransferKey, PathTransfer> distinctTransfers
) {
for (RouteRequest transferProfile : transferConfiguration.defaultTransferRequests()) {
StreetMode mode = transferProfile.journey().transfer().mode();
var nearbyStops = transferConfiguration
.defaultNearbyStopFinderForMode()
.get(mode)
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), false);
for (NearbyStop sd : nearbyStops) {
// Skip the origin stop, loop transfers are not needed.
if (sd.stop == stop) {
continue;
}
if (sd.stop.transfersNotAllowed()) {
continue;
}
createPathTransfer(stop, sd.stop, sd, distinctTransfers, mode);
}
}
}

/**
* This method calculates flex transfers if flex routing is enabled.
*/
private void calculateFlexTransfers(
TransferConfiguration transferConfiguration,
TransitStopVertex ts0,
RegularStop stop,
Map<TransferKey, PathTransfer> distinctTransfers
) {
for (RouteRequest transferProfile : transferConfiguration.flexTransferRequests()) {
// Flex transfer requests only use the WALK mode.
StreetMode mode = StreetMode.WALK;
var nearbyStops = transferConfiguration
.defaultNearbyStopFinderForMode()
.get(mode)
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), true);
// This code is for finding transfers from AreaStops to Stops, transfers
// from Stops to AreaStops and between Stops are already covered above.
for (NearbyStop sd : nearbyStops) {
// Skip the origin stop, loop transfers are not needed.
if (sd.stop == stop) {
continue;
}
if (sd.stop instanceof RegularStop) {
continue;
}
// The TransferKey and PathTransfer are created differently for flex routing.
createPathTransfer(sd.stop, stop, sd, distinctTransfers, mode);
}
}
}

/**
* This method calculates transfers between stops that are visited by trips that allow cars, if configured.
*/
private void calculateCarsAllowedTransfers(
TransferConfiguration transferConfiguration,
TransitStopVertex ts0,
RegularStop stop,
Map<TransferKey, PathTransfer> distinctTransfers,
Set<StopLocation> carsAllowedStops
) {
if (carsAllowedStops.contains(stop)) {
for (RouteRequest transferProfile : transferConfiguration.carsAllowedStopTransferRequests()) {
StreetMode mode = transferProfile.journey().transfer().mode();
var nearbyStops = transferConfiguration
.carsAllowedStopNearbyStopFinderForMode()
.get(mode)
.findNearbyStops(ts0, transferProfile, transferProfile.journey().transfer(), false);
for (NearbyStop sd : nearbyStops) {
// Skip the origin stop, loop transfers are not needed.
if (sd.stop == stop) {
continue;
}
if (sd.stop.transfersNotAllowed()) {
continue;
}
// Only calculate transfers between carsAllowedStops.
if (!carsAllowedStops.contains(sd.stop)) {
continue;
}
createPathTransfer(stop, sd.stop, sd, distinctTransfers, mode);
}
}
}
}

private record TransferConfiguration(
List<RouteRequest> defaultTransferRequests,
List<RouteRequest> carsAllowedStopTransferRequests,
Expand Down

0 comments on commit 91782ef

Please sign in to comment.