|
3 | 3 | import java.util.Iterator;
|
4 | 4 | import java.util.LinkedList;
|
5 | 5 | import java.util.List;
|
| 6 | +import java.util.stream.Collectors; |
6 | 7 |
|
7 | 8 | import org.eqasim.core.misc.ParallelProgress;
|
8 | 9 | import org.eqasim.core.scenario.cutter.network.RoadNetwork;
|
9 | 10 | import org.matsim.api.core.v01.network.Link;
|
10 |
| -import org.matsim.core.network.NetworkUtils; |
| 11 | +import org.matsim.core.utils.collections.QuadTree; |
| 12 | +import org.matsim.core.utils.collections.QuadTrees; |
11 | 13 | import org.matsim.facilities.ActivityFacilities;
|
12 | 14 | import org.matsim.facilities.ActivityFacility;
|
13 | 15 | import org.matsim.facilities.ActivityFacilityImpl;
|
14 | 16 |
|
15 | 17 | public class FacilityPlacement {
|
16 | 18 | private final int numberOfThreads;
|
17 | 19 | private final int batchSize;
|
18 |
| - private final RoadNetwork network; |
| 20 | + private final QuadTree<Link> spatialIndex; |
19 | 21 |
|
20 |
| - public FacilityPlacement(int numberOfThreads, int batchSize, RoadNetwork network) { |
21 |
| - this.network = network; |
| 22 | + public FacilityPlacement(int numberOfThreads, int batchSize, RoadNetwork network, FacilityPlacementVoter voter) { |
22 | 23 | this.numberOfThreads = numberOfThreads;
|
23 | 24 | this.batchSize = batchSize;
|
| 25 | + |
| 26 | + this.spatialIndex = QuadTrees.createQuadTree( |
| 27 | + network.getLinks().values().stream().filter(voter::canPlaceFacility).collect(Collectors.toList())); |
24 | 28 | }
|
25 | 29 |
|
26 | 30 | public void run(ActivityFacilities facilities) throws InterruptedException {
|
@@ -69,12 +73,56 @@ public void run() {
|
69 | 73 | }
|
70 | 74 |
|
71 | 75 | for (ActivityFacility facility : localTasks) {
|
72 |
| - Link link = NetworkUtils.getNearestLink(network, facility.getCoord()); |
| 76 | + Link link = spatialIndex.getClosest(facility.getCoord().getX(), facility.getCoord().getY()); |
73 | 77 | ((ActivityFacilityImpl) facility).setLinkId(link.getId());
|
74 | 78 | }
|
75 | 79 |
|
76 | 80 | progress.update(localTasks.size());
|
77 | 81 | } while (localTasks.size() > 0);
|
78 | 82 | }
|
79 | 83 | }
|
| 84 | + |
| 85 | + static public interface FacilityPlacementVoter { |
| 86 | + boolean canPlaceFacility(Link link); |
| 87 | + } |
| 88 | + |
| 89 | + static public class OSMFacilityPlacementVoter implements FacilityPlacementVoter { |
| 90 | + private final static String HIGHWAY_TAG = "osm:way:highway"; |
| 91 | + |
| 92 | + public OSMFacilityPlacementVoter(RoadNetwork network) { |
| 93 | + boolean foundAttribute = false; |
| 94 | + |
| 95 | + for (Link link : network.getLinks().values()) { |
| 96 | + if (link.getAttributes().getAttribute(HIGHWAY_TAG) != null) { |
| 97 | + foundAttribute = true; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (!foundAttribute) { |
| 103 | + throw new IllegalStateException("Did not find osm:highway attribute in network"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean canPlaceFacility(Link link) { |
| 109 | + String highway = (String) link.getAttributes().getAttribute(HIGHWAY_TAG); |
| 110 | + |
| 111 | + if (highway != null) { |
| 112 | + if (highway.contains("motorway")) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + if (highway.contains("trunk")) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + if (highway.contains("_link")) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | + } |
80 | 128 | }
|
0 commit comments