-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
class to prepare plans for new research scenario
- Loading branch information
Showing
2 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/org/matsim/run/prepare/ChangeDrtModeInPlans.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters