diff --git a/src/main/java/org/matsim/run/prepare/ChangeDrtModeInPlans.java b/src/main/java/org/matsim/run/prepare/ChangeDrtModeInPlans.java new file mode 100644 index 00000000..91402370 --- /dev/null +++ b/src/main/java/org/matsim/run/prepare/ChangeDrtModeInPlans.java @@ -0,0 +1,66 @@ +package org.matsim.run.prepare; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.matsim.analysis.LeipzigMainModeIdentifier; +import org.matsim.api.core.v01.TransportMode; +import org.matsim.api.core.v01.population.*; +import org.matsim.application.MATSimAppCommand; +import org.matsim.core.population.PopulationUtils; +import org.matsim.core.population.algorithms.TripsToLegsAlgorithm; +import org.matsim.core.router.TripStructureUtils; +import picocli.CommandLine; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class ChangeDrtModeInPlans implements MATSimAppCommand { + + private static final Logger log = LogManager.getLogger(ChangeDrtModeInPlans.class); + + @CommandLine.Option(names = "--plans", description = "Input original plan file. Should be selected + cleaned plans.", required = true) + private Path plans; + + @CommandLine.Option(names = "--old-modes", description = "List of modes to change. Use comma as delimiter", required = true, defaultValue = TransportMode.drt) + String modes; + + @CommandLine.Option(names = "--new-mode", description = "Mode to replace old modes", required = true, defaultValue = TransportMode.drt) + private String modeToReplace; + + @CommandLine.Option(names = "--output", description = "Output file name", required = true) + private Path output; + + public static void main(String[] args) { new ChangeDrtModeInPlans().execute(args); } + + @Override + public Integer call() throws Exception { + Population population = PopulationUtils.readPopulation(plans.toString()); + TripsToLegsAlgorithm trips2Legs = new TripsToLegsAlgorithm(new LeipzigMainModeIdentifier()); + + Set modesToDelete = new HashSet<>(Arrays.asList(modes.split(","))); + + for(Person person : population.getPersons().values()) { + + for(Plan plan : person.getPlans()) { + trips2Legs.run(plan); + + for(PlanElement el : plan.getPlanElements()) { + if(el instanceof Leg) { + if(modesToDelete.contains(((Leg) el).getMode())) { + ((Leg) el).setMode(modeToReplace); + } else { + if(modesToDelete.contains(el.getAttributes().getAttribute("routingMode"))) { + TripStructureUtils.setRoutingMode((Leg) el, modeToReplace); + } + } + } + } + } + } + PopulationUtils.writePopulation(population, output.toString()); + + return 0; + } +} diff --git a/src/main/java/org/matsim/run/prepare/PrepareNetwork.java b/src/main/java/org/matsim/run/prepare/PrepareNetwork.java index 5e54a0cd..0d443144 100644 --- a/src/main/java/org/matsim/run/prepare/PrepareNetwork.java +++ b/src/main/java/org/matsim/run/prepare/PrepareNetwork.java @@ -34,8 +34,8 @@ public class PrepareNetwork implements MATSimAppCommand { @CommandLine.Option(names = "--output", description = "Output path of the prepared network", required = true) private String outputPath; - @CommandLine.Option(names = "--modes", description = "List of modes to add", required = true, defaultValue = TransportMode.drt) - private Set modesToAdd; + @CommandLine.Option(names = "--modes", description = "List of modes to add. Use comma as delimiter", required = true, defaultValue = TransportMode.drt) + private String modes; @CommandLine.Option(names = "--cityAreaNetwork", description = "Cut out network of city area only", required = false) private boolean cityAreaNetwork; @@ -46,6 +46,7 @@ public static void main(String[] args) { @Override public Integer call() throws Exception { + Set modesToAdd = new HashSet<>(Arrays.asList(modes.split(","))); Geometry drtOperationArea = null; Geometry avOperationArea = null; Geometry cityArea = null;