Skip to content

Commit

Permalink
Run bicycles at tagged maxspeed if allowed and possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ratrun committed Jul 7, 2024
1 parent 2fbfa81 commit 52f4496
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ protected BikeCommonAverageSpeedParser(DecimalEncodedValue speedEnc, EnumEncoded
*/
double applyMaxSpeed(ReaderWay way, double speed, boolean bwd) {
double maxSpeed = getMaxSpeed(way, bwd);
if (way.getTag("highway", "").equals("living_street"))
return !isValidSpeed(maxSpeed) ? highwaySpeeds.get("living_street") : maxSpeed <= highwaySpeeds.get("cycleway") ? maxSpeed : highwaySpeeds.get("cycleway");
// We strictly obey speed limits, see #600
return isValidSpeed(maxSpeed) && speed > maxSpeed ? maxSpeed : speed;
}
Expand All @@ -140,12 +138,8 @@ public void handleWayTags(int edgeId, EdgeIntAccess edgeIntAccess, ReaderWay way
}

double speed = highwaySpeeds.getOrDefault(highwayValue, PUSHING_SECTION_SPEED);
String surfaceValue = way.getTag("surface");
String trackTypeValue = way.getTag("tracktype");
boolean pushingRestriction = Arrays.stream(way.getTag("vehicle", "").split(";")).anyMatch(restrictedValues::contains);
if ("steps".equals(highwayValue)) {
// ignore
} else if (way.hasTag("bicycle", "dismount")
if (way.hasTag("bicycle", "dismount")
|| way.hasTag("railway", "platform")
|| pushingRestriction && !way.hasTag("bicycle", INTENDED)) {
speed = PUSHING_SECTION_SPEED;
Expand All @@ -156,8 +150,13 @@ public void handleWayTags(int edgeId, EdgeIntAccess edgeIntAccess, ReaderWay way
else if (way.hasTag("bicycle", "yes"))
speed = 12;
}
// Increase speed in case maxspeed tag allows it
double maxSpeed = getMaxSpeed(way, false);
if (isValidSpeed(maxSpeed) && (speed < maxSpeed))
speed = Math.min(maxSpeed, highwaySpeeds.get("tertiary"));

Integer surfaceSpeed = surfaceSpeeds.get(surfaceValue);
Integer surfaceSpeed = surfaceSpeeds.get(way.getTag("surface"));
String trackTypeValue = way.getTag("tracktype");
if (way.hasTag("surface") && surfaceSpeed == null) {
speed = PUSHING_SECTION_SPEED; // unknown surface
} else if (way.hasTag("service")) {
Expand All @@ -173,6 +172,8 @@ else if (trackTypeSpeeds.containsKey(trackTypeValue))

Smoothness smoothness = smoothnessEnc.getEnum(false, edgeId, edgeIntAccess);
speed = Math.max(MIN_SPEED, smoothnessFactor.get(smoothness) * speed);
if ("steps".equals(highwayValue))
speed = MIN_SPEED;
setSpeed(false, edgeId, edgeIntAccess, applyMaxSpeed(way, speed, false));
if (avgSpeedEnc.isStoreTwoDirections())
setSpeed(true, edgeId, edgeIntAccess, applyMaxSpeed(way, speed, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ protected RacingBikeAverageSpeedParser(DecimalEncodedValue speedEnc, EnumEncoded
setHighwaySpeed("secondary_link", 20);
setHighwaySpeed("tertiary", 20);
setHighwaySpeed("tertiary_link", 20);
setHighwaySpeed("motorway", 20);
setHighwaySpeed("motorway_link", 20);

addPushingSection("path");

Expand Down
10 changes: 5 additions & 5 deletions core/src/test/java/com/graphhopper/GraphHopperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void testAlternativeRoutesBike() {
setProfiles(TestProfiles.accessSpeedAndPriority(profile, "bike"));
hopper.importOrLoad();

GHRequest req = new GHRequest(50.028917, 11.496506, 49.982089,11.599224).
GHRequest req = new GHRequest(50.028917, 11.496506, 49.982862, 11.598022).
setAlgorithm(ALT_ROUTE).setProfile(profile);

req.putHint("alternative_route.max_paths", 3);
Expand All @@ -464,11 +464,11 @@ public void testAlternativeRoutesBike() {

assertEquals(3, rsp.getAll().size());
// via ramsenthal
assertEquals(2636, rsp.getAll().get(0).getTime() / 1000);
assertEquals(2603, rsp.getAll().get(0).getTime() / 1000);
// via eselslohe -> theta;
assertEquals(2694, rsp.getAll().get(1).getTime() / 1000);
// via unterwaiz
assertEquals(2985, rsp.getAll().get(1).getTime() / 1000);
// via eselslohe -> theta; BTW: here smaller time as 2nd alternative due to priority influences time order
assertEquals(2783, rsp.getAll().get(2).getTime() / 1000);
assertEquals(2957, rsp.getAll().get(2).getTime() / 1000);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public void testKremsBikeRelation() {
@Test
public void testKremsMountainBikeRelation() {
List<Query> queries = new ArrayList<>();
queries.add(new Query(48.409523, 15.602394, 48.375466, 15.72916, 12574, 169));
queries.add(new Query(48.409523, 15.602394, 48.375466, 15.72916, 12493, 159));
queries.add(new Query(48.410061, 15.63951, 48.411386, 15.604899, 3101, 94));
queries.add(new Query(48.412294, 15.62007, 48.398306, 15.609667, 3965, 95));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public void testSpeedAndPriority() {
assertPriorityAndSpeed(PREFER, 18, way);
way.setTag("maxspeed", "10");
assertPriorityAndSpeed(VERY_NICE, 10, way);
way.setTag("maxspeed", "20");
way.setTag("surface", "sett");
assertPriorityAndSpeed(PREFER, 10, way);

way.clearTags();
way.setTag("highway", "service");
way.setTag("maxspeed", "20");
assertPriorityAndSpeed(PREFER, 18, way);

// Pushing section: this is fine as we obey the law!
way.clearTags();
Expand Down Expand Up @@ -249,7 +257,7 @@ public void testSpeedAndPriority() {
way.setTag("highway", "steps");
way.setTag("surface", "wood");
assertPriorityAndSpeed(BAD, MIN_SPEED, way);
way.setTag("maxspeed", "20");
way.setTag("maxspeed", "20"); // senseless but rare combination
assertPriorityAndSpeed(BAD, MIN_SPEED, way);

way.clearTags();
Expand Down Expand Up @@ -580,7 +588,7 @@ public void testMaxSpeed() {
way = new ReaderWay(1);
way.setTag("highway", "track");
way.setTag("maxspeed", "90");
assertPriorityAndSpeed(UNCHANGED, 12, way);
assertPriorityAndSpeed(UNCHANGED, 18, way);

// here we are limited by the maxspeed
way = new ReaderWay(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ public void testLivingStreet() {
osmWay.setTag("highway", "living_street");
assertPriorityAndSpeed(UNCHANGED, 6, osmWay);
osmWay.setTag("maxspeed", "20");
assertPriorityAndSpeed(PREFER, 18, osmWay);
assertPriorityAndSpeed(VERY_NICE, 20, osmWay);
osmWay.setTag("maxspeed", "10");
assertPriorityAndSpeed(VERY_NICE, 10, osmWay);
osmWay.clearTags();
osmWay.setTag("highway", "residential");
osmWay.setTag("maxspeed", "30");
assertPriorityAndSpeed(SLIGHT_AVOID, 18, osmWay);
assertPriorityAndSpeed(SLIGHT_AVOID, 20, osmWay);
}

@Test
Expand All @@ -107,9 +107,6 @@ public void testService() {
ReaderWay way = new ReaderWay(1);
way.setTag("highway", "service");
assertPriorityAndSpeed(SLIGHT_AVOID, 12, way);

way.setTag("service", "parking_aisle");
assertPriorityAndSpeed(SLIGHT_AVOID, 6, way);
}

@Test
Expand Down Expand Up @@ -291,33 +288,36 @@ public void testPriority_avoidanceOfHighMaxSpeed() {
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, UNCHANGED, 20, osmWay);

osmWay.setTag("highway", "motorway");
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, BAD, 18, osmWay);
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, BAD, 20, osmWay);

osmWay.setTag("tunnel", "yes");
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, REACH_DESTINATION, 18, osmWay);
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, REACH_DESTINATION, 20, osmWay);

osmWay.setTag("surface", "cobblestone");
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, REACH_DESTINATION, 8, osmWay);

osmWay.clearTags();
osmWay.setTag("highway", "motorway");
osmWay.setTag("tunnel", "yes");
osmWay.setTag("maxspeed", "80");
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, REACH_DESTINATION, 18, osmWay);
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, REACH_DESTINATION, 20, osmWay);

osmWay.clearTags();
osmWay.setTag("highway", "motorway");
osmWay.setTag("tunnel", "yes");
osmWay.setTag("maxspeed", "120");
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, REACH_DESTINATION, 18, osmWay);
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, REACH_DESTINATION, 20, osmWay);

osmWay.clearTags();
osmWay.setTag("highway", "notdefined");
osmWay.setTag("tunnel", "yes");
osmWay.setTag("maxspeed", "120");
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, BAD, PUSHING_SECTION_SPEED, osmWay);
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, BAD, 20, osmWay);

osmWay.clearTags();
osmWay.setTag("highway", "notdefined");
osmWay.setTag("maxspeed", "50");
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, UNCHANGED, PUSHING_SECTION_SPEED, osmWay);
assertPriorityAndSpeed(encodingManager, priorityEnc, speedEnc, parsers, UNCHANGED, 20, osmWay);
}

private void assertPriorityAndSpeed(EncodingManager encodingManager, DecimalEncodedValue priorityEnc, DecimalEncodedValue speedEnc,
Expand Down

0 comments on commit 52f4496

Please sign in to comment.