Skip to content

Commit 1f2084f

Browse files
committed
avoidance of destination only for car, fixes graphhopper#1831, bug introduced in graphhopper#1752
1 parent 3a163c1 commit 1f2084f

File tree

8 files changed

+43
-16
lines changed

8 files changed

+43
-16
lines changed

core/src/main/java/com/graphhopper/routing/util/BikeCommonFlagEncoder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.graphhopper.reader.ReaderWay;
2121
import com.graphhopper.routing.profiles.*;
22+
import com.graphhopper.routing.util.spatialrules.TransportationMode;
2223
import com.graphhopper.routing.weighting.PriorityWeighting;
2324
import com.graphhopper.storage.IntsRef;
2425
import com.graphhopper.util.Helper;
@@ -195,6 +196,11 @@ protected BikeCommonFlagEncoder(int speedBits, double speedFactor, int maxTurnCo
195196
setAvoidSpeedLimit(71);
196197
}
197198

199+
@Override
200+
public TransportationMode getTransportationMode() {
201+
return TransportationMode.BICYCLE;
202+
}
203+
198204
@Override
199205
public int getVersion() {
200206
return 3;

core/src/main/java/com/graphhopper/routing/util/CarFlagEncoder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
*/
1818
package com.graphhopper.routing.util;
1919

20-
import com.graphhopper.reader.OSMTurnRelation;
2120
import com.graphhopper.reader.ReaderWay;
2221
import com.graphhopper.routing.profiles.EncodedValue;
2322
import com.graphhopper.routing.profiles.UnsignedDecimalEncodedValue;
23+
import com.graphhopper.routing.util.spatialrules.TransportationMode;
2424
import com.graphhopper.storage.IntsRef;
2525
import com.graphhopper.util.Helper;
2626
import com.graphhopper.util.PMap;
@@ -144,6 +144,10 @@ public CarFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) {
144144
speedDefault = defaultSpeedMap.get("secondary");
145145
}
146146

147+
public TransportationMode getTransportationMode() {
148+
return TransportationMode.MOTOR_VEHICLE;
149+
}
150+
147151
@Override
148152
public int getVersion() {
149153
return 2;

core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.graphhopper.routing.profiles.BooleanEncodedValue;
2121
import com.graphhopper.routing.profiles.DecimalEncodedValue;
2222
import com.graphhopper.routing.profiles.EncodedValueLookup;
23+
import com.graphhopper.routing.util.spatialrules.TransportationMode;
2324

2425
/**
2526
* This class provides methods to define how a value (like speed or direction) converts to a flag
@@ -35,6 +36,8 @@ public interface FlagEncoder extends EncodedValueLookup {
3536
*/
3637
int getVersion();
3738

39+
TransportationMode getTransportationMode();
40+
3841
/**
3942
* @return the maximum speed in km/h
4043
*/

core/src/main/java/com/graphhopper/routing/util/FootFlagEncoder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.graphhopper.reader.ReaderWay;
2121
import com.graphhopper.routing.profiles.*;
22+
import com.graphhopper.routing.util.spatialrules.TransportationMode;
2223
import com.graphhopper.routing.weighting.PriorityWeighting;
2324
import com.graphhopper.storage.IntsRef;
2425
import com.graphhopper.util.PMap;
@@ -143,6 +144,10 @@ public FootFlagEncoder(int speedBits, double speedFactor) {
143144
speedDefault = MEAN_SPEED;
144145
}
145146

147+
public TransportationMode getTransportationMode() {
148+
return TransportationMode.FOOT;
149+
}
150+
146151
@Override
147152
public int getVersion() {
148153
return 5;

core/src/main/java/com/graphhopper/routing/util/spatialrules/TransportationMode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
*/
2828
public enum TransportationMode {
2929

30-
MOTOR_VEHICLE(0),
31-
BICYCLE(1),
32-
FOOT(2);
30+
OTHER(0), MOTOR_VEHICLE(1), BICYCLE(2), FOOT(3);
3331

3432
private final int value;
3533

core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.graphhopper.routing.profiles.RoadAccess;
2222
import com.graphhopper.routing.util.FlagEncoder;
2323
import com.graphhopper.routing.util.HintsMap;
24+
import com.graphhopper.routing.util.spatialrules.TransportationMode;
2425
import com.graphhopper.util.EdgeIteratorState;
2526
import com.graphhopper.util.PMap;
2627
import com.graphhopper.util.Parameters.Routing;
@@ -45,7 +46,7 @@ public class FastestWeighting extends AbstractWeighting {
4546
private final double maxSpeed;
4647
private final EnumEncodedValue<RoadAccess> roadAccessEnc;
4748
// this factor puts a penalty on roads with a "destination"-only access, see #733
48-
private final double roadAccessPenalty;
49+
private final double destinationPenalty;
4950

5051
public FastestWeighting(FlagEncoder encoder) {
5152
this(encoder, new HintsMap(0));
@@ -65,14 +66,13 @@ public FastestWeighting(FlagEncoder encoder, PMap map, TurnCostProvider turnCost
6566
headingPenaltyMillis = Math.round(headingPenalty * 1000);
6667
maxSpeed = encoder.getMaxSpeed() / SPEED_CONV;
6768

68-
if (encoder.hasEncodedValue(RoadAccess.KEY)) {
69-
// ensure that we do not need to change getMinWeight, i.e. road_access_factor >= 1
70-
roadAccessPenalty = checkBounds("road_access_factor", map.getDouble("road_access_factor", 10), 1, 10);
71-
roadAccessEnc = encoder.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
72-
} else {
73-
roadAccessPenalty = 0;
74-
roadAccessEnc = null;
75-
}
69+
if (!encoder.hasEncodedValue(RoadAccess.KEY))
70+
throw new IllegalArgumentException("road_access is not available but expected for FastestWeighting");
71+
72+
// ensure that we do not need to change getMinWeight, i.e. road_access_factor >= 1
73+
double defaultFactor = encoder.getTransportationMode() == TransportationMode.MOTOR_VEHICLE ? 10 : 1;
74+
destinationPenalty = checkBounds("road_access_destination_factor", map.getDouble("road_access_destination_factor", defaultFactor), 1, 10);
75+
roadAccessEnc = destinationPenalty > 1 ? encoder.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class) : null;
7676
}
7777

7878
@Override
@@ -88,9 +88,11 @@ public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
8888

8989
double time = edgeState.getDistance() / speed * SPEED_CONV;
9090

91-
if (roadAccessEnc != null && edgeState.get(roadAccessEnc) == RoadAccess.DESTINATION)
92-
time *= roadAccessPenalty;
93-
91+
if (roadAccessEnc != null) {
92+
RoadAccess access = edgeState.get(roadAccessEnc);
93+
if (access == RoadAccess.DESTINATION)
94+
time *= destinationPenalty;
95+
}
9496
// add direction penalties at start/stop/via points
9597
boolean unfavoredEdge = edgeState.get(EdgeIteratorState.UNFAVORED_EDGE);
9698
if (unfavoredEdge)

core/src/test/java/com/graphhopper/routing/util/CarFlagEncoderTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,20 @@ public void testDestinationTag() {
221221
IntsRef relFlags = em.createRelationFlags();
222222

223223
FastestWeighting weighting = new FastestWeighting(encoder);
224+
FastestWeighting bikeWeighting = new FastestWeighting(em.getEncoder("bike"));
225+
224226
ReaderWay way = new ReaderWay(1);
225227
way.setTag("highway", "secondary");
226228
EncodingManager.AcceptWay acceptWay = new EncodingManager.AcceptWay();
227229
assertTrue(em.acceptWay(way, acceptWay));
228230
IntsRef edgeFlags = em.handleWayTags(way, acceptWay, relFlags);
229231
assertEquals(60, weighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
232+
assertEquals(200, bikeWeighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
230233

231234
way.setTag("vehicle", "destination");
232235
edgeFlags = em.handleWayTags(way, acceptWay, relFlags);
233236
assertEquals(600, weighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
237+
assertEquals(200, bikeWeighting.calcEdgeWeight(GHUtility.createMockedEdgeIteratorState(1000, edgeFlags), false), 0.1);
234238
}
235239

236240
@Test

core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.graphhopper.routing.profiles.EnumEncodedValue;
2424
import com.graphhopper.routing.profiles.Roundabout;
2525
import com.graphhopper.routing.profiles.RouteNetwork;
26+
import com.graphhopper.routing.util.spatialrules.TransportationMode;
2627
import com.graphhopper.storage.IntsRef;
2728
import org.junit.Test;
2829

@@ -83,6 +84,10 @@ public void testWrongEncoders() {
8384
@Test
8485
public void testToDetailsStringIncludesEncoderVersionNumber() {
8586
FlagEncoder encoder = new AbstractFlagEncoder(1, 2.0, 0) {
87+
public TransportationMode getTransportationMode() {
88+
return TransportationMode.BICYCLE;
89+
}
90+
8691
@Override
8792
public int getVersion() {
8893
return 10;

0 commit comments

Comments
 (0)