diff --git a/core/files/Hoyerswerda.osm.pbf b/core/files/Hoyerswerda.osm.pbf new file mode 100644 index 00000000000..7e94e72aca6 Binary files /dev/null and b/core/files/Hoyerswerda.osm.pbf differ diff --git a/core/src/main/resources/com/graphhopper/custom_models/noisy_road_nearby.json b/core/src/main/resources/com/graphhopper/custom_models/noisy_road_nearby.json index feb29af506f..e85c133cc5d 100644 --- a/core/src/main/resources/com/graphhopper/custom_models/noisy_road_nearby.json +++ b/core/src/main/resources/com/graphhopper/custom_models/noisy_road_nearby.json @@ -1,10 +1,12 @@ -// avoid noisy segments +// avoid noisy roads nearby +// to use this custom model you need to add the following option in the config.yml +// graph.encoded_values: noisy_road_nearby { "priority": [ { "if": "noisy_road_nearby", - "multiply_by": "0.9" + "multiply_by": "0.8" } ] } diff --git a/core/src/test/java/com/graphhopper/GraphHopperTest.java b/core/src/test/java/com/graphhopper/GraphHopperTest.java index c196e5a57a2..a74f13f885c 100644 --- a/core/src/test/java/com/graphhopper/GraphHopperTest.java +++ b/core/src/test/java/com/graphhopper/GraphHopperTest.java @@ -85,6 +85,7 @@ public class GraphHopperTest { private static final String BAUTZEN = DIR + "/bautzen.osm"; private static final String BERLIN = DIR + "/berlin-siegessaeule.osm.gz"; private static final String KREMS = DIR + "/krems.osm.gz"; + private static final String HOYERSWERDA = DIR + "/Hoyerswerda.osm.pbf"; private static final String LAUF = DIR + "/Laufamholzstrasse.osm.xml"; private static final String MONACO = DIR + "/monaco.osm.gz"; private static final String MOSCOW = DIR + "/moscow.osm.gz"; @@ -2817,6 +2818,69 @@ void testLoadingWithAnotherSpeedFactorWorks() { } } + @Test + public void testAvoidNoiseNearby() { + final String profileName = "profile"; + GraphHopper hopper = new GraphHopper(). + setGraphHopperLocation(GH_LOCATION). + setOSMFile(HOYERSWERDA). + setEncodedValuesString("bike_access, bike_priority, bike_average_speed, noisy_road_nearby"). + setProfiles(TestProfiles.accessSpeedAndPriority(profileName, "bike")). + setStoreOnFlush(true); + hopper.importOrLoad(); + + Profile profile = TestProfiles.accessSpeedAndPriority(profileName, "bike"); + + GHRequest req = new GHRequest(51.434933,14.24468, 51.438776,14.256618); + req.setPathDetails(Collections.singletonList("noisy_road_nearby")); + + //Check the result of the route without avoiding the noisy roads nearby + GHResponse rspTakeNoisyRoute = hopper.route(req.setProfile(profileName)); + assertFalse(rspTakeNoisyRoute.hasErrors(), rspTakeNoisyRoute.getErrors().toString()); + ResponsePath bestPath = rspTakeNoisyRoute.getBest(); + // Shorter result of route west of river "Schwarze Elster" + assertEquals(1133.0, bestPath.getDistance(), 1); + assertEquals(43, bestPath.getPoints().size()); + Map> details = bestPath.getPathDetails(); + assertEquals(1, details.size()); + List detailList = details.get("noisy_road_nearby"); + assertEquals(3, detailList.size()); + assertEquals(false, detailList.get(0).getValue()); + assertEquals(0, detailList.get(0).getFirst()); + assertEquals(19, detailList.get(0).getLast()); + assertEquals(true, detailList.get(1).getValue()); + assertEquals(19, detailList.get(1).getFirst()); + assertEquals(30, detailList.get(1).getLast()); + assertEquals(false, detailList.get(2).getValue()); + assertEquals(30, detailList.get(2).getFirst()); + assertEquals(42, detailList.get(2).getLast()); + + //Check the result of the route with avoiding the noisy roads nearby + CustomModel customModel = profile.getCustomModel(); + customModel.getPriority().add(If("noisy_road_nearby", MULTIPLY, "0.8")); + GHResponse rspAvoidNoisyRoad = hopper.route(req.setProfile(profileName).setCustomModel(customModel)); + assertFalse(rspAvoidNoisyRoad.hasErrors(), rspAvoidNoisyRoad.getErrors().toString()); + bestPath = rspAvoidNoisyRoad.getBest(); + // Longer result of route east of river "Schwarze Elster" + assertEquals(1168.3, bestPath.getDistance(), 1); + assertEquals(47, bestPath.getPoints().size()); + details = bestPath.getPathDetails(); + assertEquals(1, details.size()); + // There is also one noisy segment here, but it is much shorter in distance + detailList = details.get("noisy_road_nearby"); + assertEquals(3, detailList.size()); + assertEquals(false, detailList.get(0).getValue()); + assertEquals(0, detailList.get(0).getFirst()); + assertEquals(19, detailList.get(0).getLast()); + assertEquals(true, detailList.get(1).getValue()); + assertEquals(19, detailList.get(1).getFirst()); + assertEquals(29, detailList.get(1).getLast()); + assertEquals(false, detailList.get(2).getValue()); + assertEquals(29, detailList.get(2).getFirst()); + assertEquals(46, detailList.get(2).getLast()); + } + + @ParameterizedTest() @ValueSource(booleans = {true, false}) void legDistanceWithDuplicateEndpoint(boolean simplifyResponse) {