From 39e0763892bd784d5d4424f50063f1c7b02a32bd Mon Sep 17 00:00:00 2001 From: rakow Date: Tue, 9 Apr 2024 20:36:28 +0200 Subject: [PATCH] improved facility generation --- .../prepare/CreateMATSimFacilities.java | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/matsim/prepare/CreateMATSimFacilities.java b/src/main/java/org/matsim/prepare/CreateMATSimFacilities.java index b020285a..3d3431d3 100644 --- a/src/main/java/org/matsim/prepare/CreateMATSimFacilities.java +++ b/src/main/java/org/matsim/prepare/CreateMATSimFacilities.java @@ -22,7 +22,6 @@ import java.nio.file.Path; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.stream.Collectors; @@ -84,23 +83,17 @@ public Integer call() throws Exception { List fts = shp.readFeatures(); - Map, Holder> data = new ConcurrentHashMap<>(); - - fts.parallelStream().forEach(ft -> processFeature(ft, carOnlyNetwork, data)); + List data = fts.parallelStream() + .map(ft -> processFeature(ft, carOnlyNetwork)) + .filter(Objects::nonNull) + .toList(); ActivityFacilities facilities = FacilitiesUtils.createActivityFacilities(); SplittableRandom rnd = new SplittableRandom(); ActivityFacilitiesFactory f = facilities.getFactory(); - for (Map.Entry, Holder> e : data.entrySet()) { - - Holder h = e.getValue(); - - // May not contain relevant activities (residential) - if (h.activities.isEmpty()) { - continue; - } + for (Holder h : data) { // Create mean coordinate OptionalDouble x = h.coords.stream().mapToDouble(Coord::getX).average(); @@ -132,10 +125,11 @@ public Integer call() throws Exception { /** * Sample points and choose link with the nearest points. Aggregate everything so there is at most one facility per link. */ - private void processFeature(SimpleFeature ft, Network network, Map, Holder> data) { + private Holder processFeature(SimpleFeature ft, Network network) { - // Actual id is the last part - String[] id = ft.getID().split("\\."); + Set activities = activities(ft); + if (activities.isEmpty()) + return null; // Pairs of coords and corresponding links List coords = samplePoints((MultiPolygon) ft.getDefaultGeometry(), 23); @@ -147,7 +141,7 @@ private void processFeature(SimpleFeature ft, Network network, Map, Hol // Everything could be filtered and map empty if (map.isEmpty()) - return; + return null; List, Long>> counts = map.entrySet().stream().sorted(Map.Entry.comparingByValue()) .toList(); @@ -155,11 +149,7 @@ private void processFeature(SimpleFeature ft, Network network, Map, Hol // The "main" link of the facility Id link = counts.get(counts.size() - 1).getKey(); - Holder holder = data.computeIfAbsent(link, k -> new Holder(ConcurrentHashMap.newKeySet(), ConcurrentHashMap.newKeySet(), Collections.synchronizedList(new ArrayList<>()))); - - holder.ids.add(id[id.length - 1]); - holder.activities.addAll(activities(ft)); - + Holder holder = new Holder(link, activities, new ArrayList<>()); // Search for the original drawn coordinate of the associated link for (int i = 0; i < links.size(); i++) { if (links.get(i).equals(link)) { @@ -167,6 +157,8 @@ private void processFeature(SimpleFeature ft, Network network, Map, Hol break; } } + + return holder; } /** @@ -243,7 +235,7 @@ private Set activities(SimpleFeature ft) { /** * Temporary data holder for facilities. */ - private record Holder(Set ids, Set activities, List coords) { + private record Holder(Id linkId, Set activities, List coords) { }