Skip to content

Commit df9651e

Browse files
committed
Fixes weight being Double.MAX_VALUE when from=to for edge-based algos.
Signed-off-by: easbar <[email protected]>
1 parent afb5a4b commit df9651e

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

core/src/main/java/com/graphhopper/routing/AbstractBidirAlgo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ protected void postInit(int from, int to) {
128128
}
129129
} else if (from == to) {
130130
// special case of identical start and end
131+
if (currFrom.weight != 0 || currTo.weight != 0) {
132+
throw new IllegalStateException("If from=to, the starting weight must be zero for from and to");
133+
}
131134
bestPath.sptEntry = currFrom;
132135
bestPath.edgeTo = currTo;
136+
bestPath.setWeight(0);
133137
finishedFrom = true;
134138
finishedTo = true;
135139
}

core/src/main/java/com/graphhopper/routing/Path.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@ public InstructionList calcInstructions(BooleanEncodedValue roundaboutEnc, final
379379
*/
380380
public Map<String, List<PathDetail>> calcDetails(List<String> requestedPathDetails, PathDetailsBuilderFactory pathBuilderFactory, int previousIndex) {
381381
if (!isFound() || requestedPathDetails.isEmpty())
382-
return Collections.EMPTY_MAP;
382+
return Collections.emptyMap();
383383
List<PathDetailsBuilder> pathBuilders = pathBuilderFactory.createPathDetailsBuilders(requestedPathDetails, encoder, weighting);
384384
if (pathBuilders.isEmpty())
385-
return Collections.EMPTY_MAP;
385+
return Collections.emptyMap();
386386

387387
forEveryEdge(new PathDetailsFromEdges(pathBuilders, previousIndex));
388388

@@ -399,7 +399,7 @@ public Map<String, List<PathDetail>> calcDetails(List<String> requestedPathDetai
399399

400400
@Override
401401
public String toString() {
402-
return "distance:" + getDistance() + ", edges:" + edgeIds.size();
402+
return "found: " + found + ", weight: " + weight + ", time: " + time + ", distance: " + distance + ", edges: " + edgeIds.size();
403403
}
404404

405405
public String toDetailsString() {

core/src/test/java/com/graphhopper/routing/AbstractRoutingAlgorithmTester.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ public void testCalcShortestPath_sourceEqualsTarget() {
201201

202202
RoutingAlgorithm algo = createAlgo(graph);
203203
Path p = algo.calcPath(0, 0);
204-
assertEquals(p.toString(), IntArrayList.from(new int[]{0}), p.calcNodes());
205-
assertEquals(p.toString(), 0, p.getDistance(), 1.e-6);
204+
assertPathFromEqualsTo(p, 0);
206205
}
207206

208207
@Test
@@ -633,26 +632,19 @@ public void testWithCoordinates() {
633632
@Test
634633
public void testCalcIfEmptyWay() {
635634
Path p = createAlgo(createTestStorage()).calcPath(0, 0);
636-
assertEquals(p.calcNodes().toString(), 1, p.calcNodes().size());
637-
assertEquals(p.toString(), 0, p.getDistance(), 1e-4);
635+
assertPathFromEqualsTo(p, 0);
638636
}
639637

640638
@Test
641639
public void testViaEdges_FromEqualsTo() {
642640
GraphHopperStorage ghStorage = createTestStorage();
643641
// identical tower nodes
644642
Path p = calcPathViaQuery(ghStorage, 0.001, 0.000, 0.001, 0.000);
645-
assertTrue(p.isFound());
646-
assertEquals(IntArrayList.from(0), p.calcNodes());
647-
// assertEquals(1, p.calcPoints().size());
648-
assertEquals(p.toString(), 0, p.getDistance(), 1e-4);
643+
assertPathFromEqualsTo(p, 0);
649644

650645
// identical query points on edge
651646
p = calcPath(ghStorage, 0, 1, 0, 1);
652-
assertTrue(p.isFound());
653-
assertEquals(IntArrayList.from(8), p.calcNodes());
654-
// assertEquals(1, p.calcPoints().size());
655-
assertEquals(p.toString(), 0, p.getDistance(), 1e-4);
647+
assertPathFromEqualsTo(p, 8);
656648

657649
// very close
658650
p = calcPathViaQuery(ghStorage, 0.00092, 0, 0.00091, 0);
@@ -967,4 +959,14 @@ Graph initEleGraph(Graph g) {
967959
protected GraphHopperStorage createMatrixGraph() {
968960
return createMatrixAlikeGraph(createGHStorage(false));
969961
}
962+
963+
private void assertPathFromEqualsTo(Path p, int node) {
964+
assertTrue(p.isFound());
965+
assertEquals(p.toString(), IntArrayList.from(node), p.calcNodes());
966+
assertEquals(p.toString(), 1, p.calcPoints().size());
967+
assertEquals(p.toString(), 0, p.calcEdges().size());
968+
assertEquals(p.toString(), 0, p.getWeight(), 1e-4);
969+
assertEquals(p.toString(), 0, p.getDistance(), 1e-4);
970+
assertEquals(p.toString(), 0, p.getTime(), 1e-4);
971+
}
970972
}

0 commit comments

Comments
 (0)