From 4438381149f3d3e877e1ae46166aa51c0110f9f6 Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Thu, 8 Feb 2024 22:13:20 +0100 Subject: [PATCH] adjust homeCoord detection --- .../DemandReaderFromCSV.java | 3 +- .../FreightDemandGenerationUtils.java | 43 ++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java index e9b09dd85d0..79d8efddbc1 100644 --- a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java +++ b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/DemandReaderFromCSV.java @@ -1353,12 +1353,11 @@ private static HashMap, Person> findPossiblePersons(Population popula */ static void findLinksForPerson(Scenario scenario, HashMap, HashMap> nearestLinkPerPerson, Person person) { - + Coord homePoint = getHomeCoord(person); for (Link link : scenario.getNetwork().getLinks().values()) if (!link.getId().toString().contains("pt") && (!link.getAttributes().getAsMap().containsKey("type") || !link.getAttributes().getAsMap().get("type").toString().contains("motorway"))) { - Coord homePoint = getHomeCoord(person); Coord middlePointLink = FreightDemandGenerationUtils.getCoordOfMiddlePointOfLink(link); double distance = NetworkUtils.getEuclideanDistance(homePoint, middlePointLink); if (!nearestLinkPerPerson.containsKey(person.getId()) diff --git a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java index c85382575d9..234d657c0b6 100644 --- a/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java +++ b/contribs/application/src/main/java/org/matsim/freightDemandGeneration/FreightDemandGenerationUtils.java @@ -76,16 +76,39 @@ static void preparePopulation(Population population, double sampleSizeInputPopul personsToRemove.add(person.getId()); continue; } - for (Plan plan : person.getPlans()) - for (PlanElement element : plan.getPlanElements()) - if (element instanceof Activity) - if (((Activity) element).getType().contains("home")) { - double x = ((Activity) element).getCoord().getX(); - double y = ((Activity) element).getCoord().getY(); - person.getAttributes().putAttribute("homeX", x); - person.getAttributes().putAttribute("homeY", y); - break; - } + Coord homeCoord = null; + if (person.getSelectedPlan() != null) { + if (PopulationUtils.getActivities(person.getSelectedPlan(), + TripStructureUtils.StageActivityHandling.ExcludeStageActivities).stream().anyMatch( + activity -> activity.getType().contains("home"))) { + homeCoord = PopulationUtils.getActivities(person.getSelectedPlan(), + TripStructureUtils.StageActivityHandling.ExcludeStageActivities).stream().filter( + activity -> activity.getType().contains("home")).findFirst().get().getCoord(); + } + } else if (!person.getPlans().isEmpty()) + for (Plan plan : person.getPlans()) + if (PopulationUtils.getActivities(plan, + TripStructureUtils.StageActivityHandling.ExcludeStageActivities).stream().anyMatch( + activity -> activity.getType().contains("home"))) { + homeCoord = PopulationUtils.getActivities(plan, + TripStructureUtils.StageActivityHandling.ExcludeStageActivities).stream().filter( + activity -> activity.getType().contains("home")).findFirst().get().getCoord(); + } + if (homeCoord == null){ + double home_x = (double) person.getAttributes().getAsMap().entrySet().stream().filter( + entry -> entry.getKey().contains("home") && entry.getKey().contains("X") || entry.getKey().contains("x")).findFirst().get().getValue(); + double home_y = (double) person.getAttributes().getAsMap().entrySet().stream().filter( + entry -> entry.getKey().contains("home") && (entry.getKey().contains("Y") || entry.getKey().contains("y"))).findFirst().get().getValue(); + homeCoord = new Coord(home_x, home_y); + } + + + if (homeCoord != null) { + person.getAttributes().putAttribute("homeX", homeCoord.getX()); + person.getAttributes().putAttribute("homeY", homeCoord.getY()); + } else { + log.warn("No home found for person " + person.getId()); + } person.removePlan(person.getSelectedPlan()); } for (Id id : personsToRemove)