Skip to content

Commit

Permalink
fix: correct reverse route direction
Browse files Browse the repository at this point in the history
  • Loading branch information
munterfi committed Nov 13, 2024
1 parent c688ade commit 6d47bbd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.matsim.pt.transitSchedule.api.TransitRouteStop;
import org.matsim.pt.transitSchedule.api.TransitStopFacility;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -56,22 +55,27 @@ TransitStopFacility buildTransitStopFacility(StopFacilityInfo stopFacilityInfo)
}

TransitRoute buildTransitRoute(TransitLineInfo transitLineInfo, List<RouteElement> routeElements, RouteDirection direction) {

List<Id<Link>> routeLinks = new ArrayList<>();
List<TransitRouteStop> routeStops = new ArrayList<>();
final double[] travelTime = {0};

List<RouteElement> directedRouteElements = switch (direction) {
case FORWARD -> routeElements;
case REVERSE -> routeElements.reversed();
};

// add first stop
double dwellTime = transitLineInfo.getDwellTimeAtOrigin().toSeconds();
TransitStopFacility stopFacility = stopFacilities.get(transitLineInfo.getOriginStop().getId());
RouteStop firstRouteStop = (RouteStop) directedRouteElements.getFirst();
double dwellTime = firstRouteStop.getDwellTime().toSeconds();
TransitStopFacility stopFacility = stopFacilities.get(firstRouteStop.getStopFacilityInfo().getId());
TransitRouteStop transitRouteStop = factory.createTransitRouteStop(stopFacility, -dwellTime, travelTime[0],
true);
routeStops.add(transitRouteStop);

// loop over route elements, set first stop as last element and start with second element
RouteElement lastElement = new RouteStop(transitLineInfo.getOriginStop(), Duration.ofSeconds(0),
transitLineInfo.getDwellTimeAtOrigin());
for (RouteElement currentElement : routeElements) {
for (int i = 1; i < directedRouteElements.size(); i++) {
RouteElement lastElement = directedRouteElements.get(i - 1);
RouteElement currentElement = directedRouteElements.get(i);

// visit element
currentElement.accept(new RouteElementVisitor() {
Expand All @@ -81,6 +85,7 @@ public void visit(RouteStop routeStop) {
travelTime[0] = travelTime[0] + routeStop.getTravelTime().toSeconds();
double dwellTime = routeStop.getDwellTime().toSeconds();

// add route stop
TransitStopFacility stopFacility = stopFacilities.get(routeStop.getStopFacilityInfo().getId());
TransitRouteStop transitRouteStop = factory.createTransitRouteStop(stopFacility, travelTime[0],
travelTime[0] + dwellTime, true);
Expand All @@ -96,15 +101,8 @@ public void visit(RoutePass passInfo) {

});

switch (direction) {
case FORWARD -> routeLinks.addAll(
connect(stopFacilities, addedSegments, transitLineInfo, lastElement, currentElement));
case REVERSE -> routeLinks.addAll(
connect(stopFacilities, addedSegments, transitLineInfo, currentElement, lastElement));
}

// move one element forward
lastElement = currentElement;
// connect stop facilities on network
routeLinks.addAll(connect(stopFacilities, addedSegments, transitLineInfo, lastElement, currentElement));
}

// get or create transit line
Expand All @@ -114,7 +112,7 @@ public void visit(RoutePass passInfo) {
String.format("%s_%s", transitLineInfo.getId(), direction.name()), routeLinks, routeStops);
}


// connects transit route stops on network, calls infrastructure repository for track information
private List<Id<Link>> connect(Map<String, TransitStopFacility> stopFacilities, Map<String, Id<Link>> addedSegments, TransitLineInfo transitLineInfo, RouteElement from, RouteElement to) {
List<TrackSegmentInfo> segments = infrastructureRepository.getTrack(from.getStopFacilityInfo(),
to.getStopFacilityInfo(), transitLineInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public SupplyBuilder addTransitLine(String lineId, String vehicleTypeId, String

transitLineContainers.put(lineId,
new TransitLineContainer(new TransitLineInfo(lineId, vehicleTypeInfo, originStop, dwellTimeAtOrigin),
new ArrayList<>(), new EnumMap<>(RouteDirection.class)));
new ArrayList<>(List.of(new RouteStop(originStop, Duration.ZERO, dwellTimeAtOrigin))),
new EnumMap<>(RouteDirection.class)));

return this;
}
Expand Down

0 comments on commit 6d47bbd

Please sign in to comment.