Skip to content

Commit

Permalink
class to prepare plans for new research scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed Sep 5, 2022
1 parent 72528b6 commit e7a4db3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
66 changes: 66 additions & 0 deletions src/main/java/org/matsim/run/prepare/ChangeDrtModeInPlans.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/matsim/run/prepare/PrepareNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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;
Expand All @@ -46,6 +46,7 @@ public static void main(String[] args) {

@Override
public Integer call() throws Exception {
Set<String> modesToAdd = new HashSet<>(Arrays.asList(modes.split(",")));
Geometry drtOperationArea = null;
Geometry avOperationArea = null;
Geometry cityArea = null;
Expand Down

0 comments on commit e7a4db3

Please sign in to comment.