Skip to content

Commit 0e42d31

Browse files
authored
Merge pull request #6 from Trihydro/bug/14729-buffer-dead-end-before-threshold
Fix Dead End Bug when Threshold Not Met
2 parents 8b9ab35 + 4bd414e commit 0e42d31

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

web-bundle/src/main/java/com/graphhopper/resources/BufferResource.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ private BufferFeature computeEdgeAtDistanceThreshold(final BufferFeature startFe
339339
nodeAccess.getLat(currentNode), nodeAccess.getLon(currentNode));
340340
double previousDistance = 0.0;
341341
Integer currentEdge = -1;
342+
// Create previous values to use in case we don't meet the threshold
343+
Integer previousEdge = currentEdge;
344+
int previousNode = currentNode;
342345

343346
if (currentDistance >= thresholdDistance) {
344347
return startFeature;
@@ -431,7 +434,12 @@ else if (tempState.get(this.roundaboutAccessEnc)) {
431434
currentEdge = potentialRoundaboutEdgesWithoutName.get(0);
432435
usedEdges.add(currentEdge);
433436
} else {
434-
throw new WebApplicationException("Dead end found.");
437+
// Instead of throwing an exception, we break the loop and return the
438+
// path up to the current edge even though we haven't met the threshold.
439+
// Assign current edge & node to previous and break the loop.
440+
currentEdge = previousEdge;
441+
currentNode = previousNode;
442+
break;
435443
}
436444
}
437445

@@ -462,6 +470,9 @@ else if (tempState.get(this.roundaboutAccessEnc)) {
462470
// Move to next node
463471
currentNode = otherNode;
464472
currentState = graph.getEdgeIteratorState(currentEdge, Integer.MIN_VALUE);
473+
// Update previous values
474+
previousEdge = currentEdge;
475+
previousNode = currentNode;
465476
}
466477

467478
return new BufferFeature(currentEdge, currentNode,

web/src/test/java/com/graphhopper/application/resources/BufferResourceTest.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ public class BufferResourceTest {
3131
private static GraphHopperServerConfiguration createConfig() {
3232
GraphHopperServerConfiguration config = new GraphHopperServerTestConfiguration();
3333
config.getGraphHopperConfiguration()
34-
.putObject("datareader.file", "../core/files/andorra.osm.pbf")
35-
.putObject("import.osm.ignored_highways", "")
36-
.putObject("graph.location", DIR)
37-
.putObject("graph.encoded_values", "car_access, car_average_speed")
38-
.setProfiles(Arrays.asList(
39-
TestProfiles.accessAndSpeed("fast_car", "car").setTurnCostsConfig(TurnCostsConfig.car()),
40-
TestProfiles.constantSpeed("short_car", 35).setTurnCostsConfig(TurnCostsConfig.car()),
41-
TestProfiles.accessAndSpeed("fast_car_no_turn_restrictions", "car")));
34+
.putObject("datareader.file", "../core/files/andorra.osm.pbf")
35+
.putObject("import.osm.ignored_highways", "")
36+
.putObject("graph.location", DIR)
37+
.putObject("graph.encoded_values", "car_access, car_average_speed")
38+
.setProfiles(Arrays.asList(
39+
TestProfiles.accessAndSpeed("fast_car", "car").setTurnCostsConfig(TurnCostsConfig.car()),
40+
TestProfiles.constantSpeed("short_car", 35).setTurnCostsConfig(TurnCostsConfig.car()),
41+
TestProfiles.accessAndSpeed("fast_car_no_turn_restrictions", "car")));
4242
return config;
4343
}
4444

@@ -333,4 +333,31 @@ public void testUnusualRoadNameFormat() {
333333
assertEquals(200, response.getStatus());
334334
}
335335
}
336+
337+
@Test
338+
public void testOutOfRoad() {
339+
// testing buffer on a road in Andorra that transitions from CG-1 to N-145
340+
JsonFeatureCollection featureCollection;
341+
try (Response response = clientTarget(app, "/buffer?profile=my_car&"
342+
+ "point=42.440606,1.477431&roadName=cg-1&"
343+
+ "thresholdDistance=1600&buildUpstream=true&queryMultiplier=0.000075").request()
344+
.buildGet().invoke()) {
345+
assertEquals(200, response.getStatus());
346+
featureCollection = response.readEntity(JsonFeatureCollection.class);
347+
}
348+
349+
assertEquals(2, featureCollection.getFeatures().size());
350+
Geometry lineString0 = featureCollection.getFeatures().get(0).getGeometry();
351+
Geometry lineString1 = featureCollection.getFeatures().get(1).getGeometry();
352+
353+
// Identical start points (reversed index)
354+
assertEquals(lineString0.getCoordinates()[lineString0.getCoordinates().length - 1],
355+
lineString1.getCoordinates()[lineString1.getCoordinates().length - 1]);
356+
357+
// Different end points (reversed index)
358+
assertNotEquals(lineString0.getCoordinates()[0], lineString1.getCoordinates()[0]);
359+
360+
// Check lengths are different since one path runs out of road
361+
assertNotEquals(lineString0.getLength(), lineString1.getLength());
362+
}
336363
}

0 commit comments

Comments
 (0)