Skip to content

Commit 22466f0

Browse files
author
Peter
committed
fixed graphhopper#242, Exception if speed is 0 but valid access flags and using shortest path algo.
1 parent 6fa2557 commit 22466f0

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ protected long calcMillis( double distance, long flags, boolean revert )
223223
if (Double.isInfinite(speed) || Double.isNaN(speed) || speed < 0)
224224
throw new IllegalStateException("Invalid speed stored in edge! " + speed);
225225

226+
if (speed == 0)
227+
throw new IllegalStateException("Speed cannot be 0 for unblocked edge, use access properties to mark edge blocked! Should only occur for shortest path calculation. See #242.");
228+
226229
return (long) (distance * 3600 / speed);
227230
}
228231

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void testCalcFastestPath()
8787

8888
Graph graphFastest = createGraph(false);
8989
initDirectedAndDiffSpeed(graphFastest);
90-
Path p2 = createAlgo(graphFastest,
90+
Path p2 = createAlgo(graphFastest,
9191
AlgorithmOptions.start().flagEncoder(carEncoder).weighting(new FastestWeighting(carEncoder)).build()).
9292
calcPath(0, 3);
9393
assertEquals(Helper.createTList(0, 4, 6, 7, 5, 3), p2.calcNodes());
@@ -665,6 +665,26 @@ public void testTwoWeightsPerEdge()
665665
assertEquals(6568, p.getWeight(), 1);
666666
}
667667

668+
@Test
669+
public void test0SpeedButUnblocked_Issue242()
670+
{
671+
Graph graph = createGraph(false);
672+
long flags = carEncoder.setAccess(carEncoder.setSpeed(0, 0), true, true);
673+
674+
graph.edge(0, 1).setFlags(flags).setDistance(10);
675+
graph.edge(1, 2).setFlags(flags).setDistance(10);
676+
677+
RoutingAlgorithm algo = createAlgo(graph);
678+
try
679+
{
680+
Path p = algo.calcPath(0, 2);
681+
assertTrue(false);
682+
} catch (Exception ex)
683+
{
684+
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Speed cannot be 0"));
685+
}
686+
}
687+
668688
@Test
669689
public void testTwoWeightsPerEdge2()
670690
{

0 commit comments

Comments
 (0)