diff --git a/src/main/java/org/matsim/prepare/PrepareOpenPopulation.java b/src/main/java/org/matsim/prepare/PrepareOpenPopulation.java index f2a69e4..ad87ea7 100644 --- a/src/main/java/org/matsim/prepare/PrepareOpenPopulation.java +++ b/src/main/java/org/matsim/prepare/PrepareOpenPopulation.java @@ -1,14 +1,16 @@ package org.matsim.prepare; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.matsim.api.core.v01.population.Activity; import org.matsim.api.core.v01.population.Person; +import org.matsim.api.core.v01.population.Plan; import org.matsim.api.core.v01.population.Population; import org.matsim.application.MATSimAppCommand; import org.matsim.core.population.PopulationUtils; +import org.matsim.core.router.TripStructureUtils; import picocli.CommandLine; import java.nio.file.Path; +import java.util.List; @CommandLine.Command( name = "open-population", @@ -16,8 +18,6 @@ ) public class PrepareOpenPopulation implements MATSimAppCommand { - private static final Logger log = LogManager.getLogger(PrepareOpenPopulation.class); - @CommandLine.Option(names = "--open-population", description = "Path to population using open data", required = true) private Path openPopulationPath; @@ -34,9 +34,6 @@ public static void main(String[] args) { @Override public Integer call() throws Exception { - // TODO read open population - // replace coordinates from - Population openPopulation = PopulationUtils.readPopulation(openPopulationPath.toString()); Population calibratedPopulation = PopulationUtils.readPopulation(calibratedPopulationPath.toString()); @@ -62,8 +59,37 @@ public Integer call() throws Exception { */ private void prepare(Person targetPerson, Person sourcePerson) { + // Activities with type "other" were added synthetically, these coordinates are not copied + List activities = TripStructureUtils.getActivities(sourcePerson.getSelectedPlan(), TripStructureUtils.StageActivityHandling.ExcludeStageActivities); - // TODO - } + for (Plan plan : targetPerson.getPlans()) { + + List targetActs = TripStructureUtils.getActivities(plan, TripStructureUtils.StageActivityHandling.ExcludeStageActivities); + + int toActivityCounter = 0; + for (int fromActivityCounter = 0; fromActivityCounter < activities.size(); fromActivityCounter++) { + Activity fromActivity = activities.get(fromActivityCounter); + Activity toActivity = targetActs.get(toActivityCounter); + + // Skip other and the second part of the actual activity (which are both not present in the target) + if (fromActivity.getType().startsWith("other")) { + fromActivityCounter++; + continue; + } + if (toActivity.getType().startsWith("other")) { + Activity secondOccurrenceOfLastFromActivity = targetActs.get(toActivityCounter + 1); + secondOccurrenceOfLastFromActivity.setCoord(activities.get(fromActivityCounter - 1).getCoord()); + + toActivityCounter += 2; + continue; + } + + toActivity.setCoord(fromActivity.getCoord()); + toActivityCounter++; + } + + TripStructureUtils.getLegs(plan).forEach(leg -> leg.setRoute(null)); + } + } }