From 6d47bbd8509ca0525c701464718a649ee2a47b50 Mon Sep 17 00:00:00 2001 From: u228298 Date: Wed, 13 Nov 2024 13:33:47 +0100 Subject: [PATCH] fix: correct reverse route direction --- .../matsim/InfrastructureBuilder.java | 32 +++++++++---------- .../converter/matsim/MatsimSupplyBuilder.java | 3 +- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/InfrastructureBuilder.java b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/InfrastructureBuilder.java index acd8963..59b8fdc 100644 --- a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/InfrastructureBuilder.java +++ b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/InfrastructureBuilder.java @@ -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; @@ -56,22 +55,27 @@ TransitStopFacility buildTransitStopFacility(StopFacilityInfo stopFacilityInfo) } TransitRoute buildTransitRoute(TransitLineInfo transitLineInfo, List routeElements, RouteDirection direction) { - List> routeLinks = new ArrayList<>(); List routeStops = new ArrayList<>(); final double[] travelTime = {0}; + List 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() { @@ -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); @@ -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 @@ -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> connect(Map stopFacilities, Map> addedSegments, TransitLineInfo transitLineInfo, RouteElement from, RouteElement to) { List segments = infrastructureRepository.getTrack(from.getStopFacilityInfo(), to.getStopFacilityInfo(), transitLineInfo); diff --git a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/MatsimSupplyBuilder.java b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/MatsimSupplyBuilder.java index b41db52..9d98514 100644 --- a/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/MatsimSupplyBuilder.java +++ b/src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/matsim/MatsimSupplyBuilder.java @@ -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; }