|
23 | 23 | import com.carrotsearch.hppc.cursors.IntCursor; |
24 | 24 | import com.carrotsearch.hppc.cursors.LongCursor; |
25 | 25 | import com.graphhopper.storage.BaseGraph; |
| 26 | +import com.graphhopper.util.ArrayUtil; |
26 | 27 | import com.graphhopper.util.EdgeIteratorState; |
27 | 28 |
|
28 | 29 | import java.util.ArrayList; |
@@ -116,26 +117,36 @@ public EdgeResult convertForViaWays(LongArrayList fromWays, LongArrayList viaWay |
116 | 117 | throw new OSMRestrictionException("has disconnected member ways"); |
117 | 118 | else if (solutions.size() > fromWays.size() * toWays.size()) |
118 | 119 | throw new OSMRestrictionException("has member ways that do not form a unique path"); |
119 | | - return buildResult(solutions, new EdgeResult(fromWays.size(), viaWays.size(), toWays.size())); |
| 120 | + return buildResult(solutions, fromWays, viaWays, toWays); |
120 | 121 | } |
121 | 122 |
|
122 | | - private static EdgeResult buildResult(List<IntArrayList> edgeChains, EdgeResult result) { |
123 | | - for (IntArrayList edgeChain : edgeChains) { |
124 | | - result.fromEdges.add(edgeChain.get(0)); |
125 | | - if (result.nodes.isEmpty()) { |
126 | | - // the via-edges and nodes are the same for edge chain |
127 | | - for (int i = 1; i < edgeChain.size() - 3; i += 2) { |
128 | | - result.nodes.add(edgeChain.get(i)); |
129 | | - result.viaEdges.add(edgeChain.get(i + 1)); |
130 | | - } |
131 | | - result.nodes.add(edgeChain.get(edgeChain.size() - 2)); |
132 | | - } |
133 | | - result.toEdges.add(edgeChain.get(edgeChain.size() - 1)); |
| 123 | + private static EdgeResult buildResult(List<IntArrayList> edgeChains, LongArrayList fromWays, LongArrayList viaWays, LongArrayList toWays) { |
| 124 | + EdgeResult result = new EdgeResult(fromWays.size(), viaWays.size(), toWays.size()); |
| 125 | + // we get multiple edge chains, but they are expected to be identical except for their first or last members |
| 126 | + IntArrayList firstChain = edgeChains.get(0); |
| 127 | + result.fromEdges.add(firstChain.get(0)); |
| 128 | + for (int i = 1; i < firstChain.size() - 3; i += 2) { |
| 129 | + result.nodes.add(firstChain.get(i)); |
| 130 | + result.viaEdges.add(firstChain.get(i + 1)); |
134 | 131 | } |
| 132 | + result.nodes.add(firstChain.get(firstChain.size() - 2)); |
| 133 | + result.toEdges.add(firstChain.get(firstChain.size() - 1)); |
| 134 | + // We keep the first/last elements of all chains in case there are multiple from/to ways |
| 135 | + List<IntArrayList> otherChains = edgeChains.subList(1, edgeChains.size()); |
| 136 | + if (fromWays.size() > 1) { |
| 137 | + if (otherChains.stream().anyMatch(chain -> chain.get(chain.size() - 1) != firstChain.get(firstChain.size() - 1))) |
| 138 | + throw new IllegalArgumentException("edge chains were supposed to be the same except for their first elements, but got: " + edgeChains + " - for: " + fromWays + ", " + viaWays + ", " + toWays); |
| 139 | + otherChains.forEach(chain -> result.fromEdges.add(chain.get(0))); |
| 140 | + } else if (toWays.size() > 1) { |
| 141 | + if (otherChains.stream().anyMatch(chain -> chain.get(0) != firstChain.get(0))) |
| 142 | + throw new IllegalArgumentException("edge chains were supposed to be the same except for their last elements, but got: " + edgeChains + " - for: " + fromWays + ", " + viaWays + ", " + toWays); |
| 143 | + otherChains.forEach(chain -> result.toEdges.add(chain.get(chain.size() - 1))); |
| 144 | + } else if (!otherChains.isEmpty()) |
| 145 | + throw new IllegalStateException("If there are multiple chains there must be either multiple from- or to-ways."); |
135 | 146 | return result; |
136 | 147 | } |
137 | 148 |
|
138 | | - private void findEdgeChain(long fromWay, LongArrayList viaWays, long toWay, List<IntArrayList> solutions) throws OSMRestrictionException { |
| 149 | + private void findEdgeChain(long fromWay, LongArrayList viaWays, long toWay, List<IntArrayList> solutions) { |
139 | 150 | // For each edge chain there must be one edge associated with the from-way, at least one for each via-way and one |
140 | 151 | // associated with the to-way. We use DFS with backtracking to find all edge chains that connect an edge |
141 | 152 | // associated with the from-way with one associated with the to-way. |
|
0 commit comments