Skip to content

Commit

Permalink
improved facility generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Apr 9, 2024
1 parent cf4af3a commit 39e0763
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/main/java/org/matsim/prepare/CreateMATSimFacilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -84,23 +83,17 @@ public Integer call() throws Exception {

List<SimpleFeature> fts = shp.readFeatures();

Map<Id<Link>, Holder> data = new ConcurrentHashMap<>();

fts.parallelStream().forEach(ft -> processFeature(ft, carOnlyNetwork, data));
List<Holder> 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<Id<Link>, 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();
Expand Down Expand Up @@ -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<Id<Link>, Holder> data) {
private Holder processFeature(SimpleFeature ft, Network network) {

// Actual id is the last part
String[] id = ft.getID().split("\\.");
Set<String> activities = activities(ft);
if (activities.isEmpty())
return null;

// Pairs of coords and corresponding links
List<Coord> coords = samplePoints((MultiPolygon) ft.getDefaultGeometry(), 23);
Expand All @@ -147,26 +141,24 @@ private void processFeature(SimpleFeature ft, Network network, Map<Id<Link>, Hol

// Everything could be filtered and map empty
if (map.isEmpty())
return;
return null;

List<Map.Entry<Id<Link>, Long>> counts = map.entrySet().stream().sorted(Map.Entry.comparingByValue())
.toList();

// The "main" link of the facility
Id<Link> 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)) {
holder.coords.add(coords.get(i));
break;
}
}

return holder;
}

/**
Expand Down Expand Up @@ -243,7 +235,7 @@ private Set<String> activities(SimpleFeature ft) {
/**
* Temporary data holder for facilities.
*/
private record Holder(Set<String> ids, Set<String> activities, List<Coord> coords) {
private record Holder(Id<Link> linkId, Set<String> activities, List<Coord> coords) {

}

Expand Down

0 comments on commit 39e0763

Please sign in to comment.