Skip to content

Commit 8ba4f11

Browse files
author
Peter
committed
Merge pull request graphhopper#279 from graphhopper/motorbike
motorcycle profile
2 parents ee0a536 + 3c2553f commit 8ba4f11

26 files changed

+708
-134
lines changed

config-example.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ prepare.chWeighting=fastest
1212
# increase from 1 to 5, to reduce way geometry e.g. for android
1313
osmreader.wayPointMaxDistance=1
1414

15-
# Possible options: car,foot,bike,bike2,mtb,racingbike (comma separated)
15+
# Possible options: car,foot,bike,bike2,mtb,racingbike,motorcycle (comma separated)
1616
# When using two or three option together remeber to set "prepare.chWeighting=no" above.
1717
# bike2 takes elevation data into account (like up-hill is slower than down-hill)
1818
# and requires enabling graph.elevation.provider below, e.g. see #169

core/files/changelog.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
renaming of GraphHopper.setCHShortcuts to setCHWeighting, as well as the property prepare.chShortcuts to prepare.chWeighting
1313
jsonp is disabled by default. You need to enable it in the config.properties, see the config-example.properties
1414
EncodingManager cannot be null in GraphHopperStorage since 0.4. If you need to parse EncodingManager configuration from existing graph use EncodingManager.create
15-
no reflection done in EncodingManager which improves portability and makes configuration of encoders
15+
no reflection done in EncodingManager which improves portability and makes configuration of encoders possible before adding to manager
1616
removed dijkstraNativebi as no performance advantage but maintenance disadvantage and similar to oneToManyDijkstra
17-
osmreader.bytesForFlags changed to graph.bytesForFlags
18-
to provide context for turn costs we needed to add prevEdgeId into Weighting.calcWeight
17+
to provide context for turn costs we needed to add prevEdgeId into Weighting.calcWeight, see new documentation
1918
with the introduction of lock protection mechanism (see #112) GraphHopper needs always write access, see also #217
20-
new GraphHopper.clean method to remove the directory via Java API
19+
new GraphHopper.clean method to remove the graph directory via Java API
2120
FlagEncoder: replaced isFoward and isBackward with more generic isBool(flags, FlagEncoder.FORWARD|BACKWARD)
22-
restrictions type was: String[], is: List<String>
2321

2422
0.3.0
2523
introduced prefer bits, now bike uses more bits and 3 bike encoder do not fit into 32 bit anymore, will be fixed later

core/src/main/java/com/graphhopper/GraphHopper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ public Weighting createWeighting( WeightingMap wMap, FlagEncoder encoder )
777777
result = new ShortestWeighting();
778778
} else if ("fastest".equalsIgnoreCase(weighting) || weighting.isEmpty())
779779
{
780-
if (encoder instanceof BikeCommonFlagEncoder)
780+
if (encoder instanceof BikeCommonFlagEncoder || encoder instanceof MotorcycleFlagEncoder)
781781
result = new PriorityWeighting(encoder);
782782
else
783783
result = new FastestWeighting(encoder);

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

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,19 @@ public abstract class AbstractFlagEncoder implements FlagEncoder, TurnCostEncode
6767
protected EdgeExplorer edgeInExplorer;
6868

6969
/* restriction definitions where order is important */
70-
protected List<String> restrictions = new ArrayList<String>(5);
71-
protected HashSet<String> intendedValues = new HashSet<String>(5);
72-
protected HashSet<String> restrictedValues = new HashSet<String>(5);
73-
protected HashSet<String> ferries = new HashSet<String>(5);
74-
protected HashSet<String> oneways = new HashSet<String>(5);
75-
protected HashSet<String> acceptedRailways = new HashSet<String>(5);
70+
protected final List<String> restrictions = new ArrayList<String>(5);
71+
protected final HashSet<String> intendedValues = new HashSet<String>(5);
72+
protected final HashSet<String> restrictedValues = new HashSet<String>(5);
73+
protected final HashSet<String> ferries = new HashSet<String>(5);
74+
protected final HashSet<String> oneways = new HashSet<String>(5);
75+
protected final HashSet<String> acceptedRailways = new HashSet<String>(5);
7676
// http://wiki.openstreetmap.org/wiki/Mapfeatures#Barrier
77-
protected HashSet<String> absoluteBarriers = new HashSet<String>(5);
78-
protected HashSet<String> potentialBarriers = new HashSet<String>(5);
79-
// should potential barriers block when no access limits are given?
80-
protected boolean blockByDefault = true;
81-
protected boolean blockFords = true;
82-
protected int speedBits;
83-
protected double speedFactor;
77+
protected final HashSet<String> absoluteBarriers = new HashSet<String>(5);
78+
protected final HashSet<String> potentialBarriers = new HashSet<String>(5);
79+
private boolean blockByDefault = true;
80+
private boolean blockFords = true;
81+
protected final int speedBits;
82+
protected final double speedFactor;
8483

8584
/**
8685
* @param speedBits specify the number of bits used for speed
@@ -105,14 +104,32 @@ protected AbstractFlagEncoder( int speedBits, double speedFactor, int maxTurnCos
105104
acceptedRailways.add("tram");
106105
acceptedRailways.add("abandoned");
107106
acceptedRailways.add("disused");
108-
107+
109108
// http://wiki.openstreetmap.org/wiki/Demolished_Railway
110109
acceptedRailways.add("dismantled");
111110
acceptedRailways.add("razed");
112111
acceptedRailways.add("historic");
113112
acceptedRailways.add("obliterated");
114113
}
115114

115+
/**
116+
* Should potential barriers block when no access limits are given?
117+
*/
118+
public void setBlockByDefault( boolean blockByDefault )
119+
{
120+
this.blockByDefault = blockByDefault;
121+
}
122+
123+
public void setBlockFords( boolean blockFords )
124+
{
125+
this.blockFords = blockFords;
126+
}
127+
128+
public boolean isBlockFords()
129+
{
130+
return blockFords;
131+
}
132+
116133
/**
117134
* Defines the bits for the node flags, which are currently used for barriers only.
118135
* <p>
@@ -743,6 +760,21 @@ protected static String getStr( String str, String key )
743760
return "";
744761
}
745762

763+
/**
764+
* @param force should be false if speed should be changed only if it is bigger than maxspeed.
765+
*/
766+
protected double applyMaxSpeed( OSMWay way, double speed, boolean force )
767+
{
768+
double maxSpeed = getMaxSpeed(way);
769+
// apply only if smaller maxSpeed
770+
if (maxSpeed >= 0)
771+
{
772+
if (force || maxSpeed < speed)
773+
return maxSpeed * 0.9;
774+
}
775+
return speed;
776+
}
777+
746778
protected String getPropertiesString()
747779
{
748780
return "speedFactor=" + speedFactor + "|speedBits=" + speedBits + "|turnCosts=" + (maxTurnCosts > 0);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Bike2WeightFlagEncoder( int speedBits, double speedFactor, int maxTurnCos
5252
public int defineWayBits( int index, int shift )
5353
{
5454
shift = super.defineWayBits(index, shift);
55-
reverseSpeed = new EncodedDoubleValue("Speed", shift, speedBits, speedFactor, getHighwaySpeed("cycleway"), 30);
55+
reverseSpeed = new EncodedDoubleValue("Reverse Speed", shift, speedBits, speedFactor, getHighwaySpeed("cycleway"), 30);
5656
shift += reverseSpeed.getBits();
5757
return shift;
5858
}

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

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,26 @@
1919

2020
import com.graphhopper.reader.OSMWay;
2121
import com.graphhopper.reader.OSMRelation;
22-
import static com.graphhopper.routing.util.BikeCommonFlagEncoder.PriorityCode.*;
22+
import static com.graphhopper.routing.util.PriorityCode.*;
2323
import com.graphhopper.util.Helper;
2424
import com.graphhopper.util.InstructionAnnotation;
2525
import com.graphhopper.util.Translation;
2626

2727
import java.util.*;
2828

2929
/**
30-
* Defines bit layout of bicycles (not motorbikes) for speed, access and relations (network).
30+
* Defines bit layout of bicycles (not motorcycles) for speed, access and relations (network).
3131
* <p/>
3232
* @author Peter Karich
3333
* @author Nop
3434
* @author ratrun
3535
*/
3636
public class BikeCommonFlagEncoder extends AbstractFlagEncoder
3737
{
38+
/**
39+
* Reports wether this edge is unpaved.
40+
*/
41+
public static final int K_UNPAVED = 100;
3842
protected static final int PUSHING_SECTION_SPEED = 4;
3943
private long unpavedBit = 0;
4044
// Pushing section heighways are parts where you need to get off your bike and push it (German: Schiebestrecke)
@@ -51,13 +55,13 @@ public class BikeCommonFlagEncoder extends AbstractFlagEncoder
5155
private final Map<String, Integer> bikeNetworkToCode = new HashMap<String, Integer>();
5256
EncodedValue relationCodeEncoder;
5357
private EncodedValue wayTypeEncoder;
54-
private EncodedValue preferWayEncoder;
58+
private EncodedValue preferWayEncoder;
5559

5660
protected BikeCommonFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts )
5761
{
5862
super(speedBits, speedFactor, maxTurnCosts);
5963
// strict set, usually vehicle and agricultural/forestry are ignored by cyclists
60-
restrictions = new ArrayList<String>(Arrays.asList("bicycle", "access"));
64+
restrictions.addAll(Arrays.asList("bicycle", "access"));
6165
restrictedValues.add("private");
6266
restrictedValues.add("no");
6367
restrictedValues.add("restricted");
@@ -71,7 +75,7 @@ protected BikeCommonFlagEncoder( int speedBits, double speedFactor, int maxTurnC
7175
oppositeLanes.add("opposite_lane");
7276
oppositeLanes.add("opposite_track");
7377

74-
blockByDefault = false;
78+
setBlockByDefault(false);
7579
potentialBarriers.add("gate");
7680
// potentialBarriers.add("lift_gate");
7781
potentialBarriers.add("swing_gate");
@@ -238,7 +242,7 @@ public long acceptWay( OSMWay way )
238242
return 0;
239243

240244
// do not use fords with normal bikes, flagged fords are in included above
241-
if (blockFords && (way.hasTag("highway", "ford") || way.hasTag("ford")))
245+
if (isBlockFords() && (way.hasTag("highway", "ford") || way.hasTag("ford")))
242246
return 0;
243247

244248
// check access restrictions
@@ -251,7 +255,7 @@ public long acceptWay( OSMWay way )
251255

252256
String sacScale = way.getTag("sac_scale");
253257
if (sacScale != null)
254-
{
258+
{
255259
if (!allowedSacScale(sacScale))
256260
return 0;
257261
}
@@ -298,10 +302,10 @@ public long handleWayTags( OSMWay way, long allowed, long relationFlags )
298302
if (relationFlags != 0)
299303
priorityFromRelation = (int) relationCodeEncoder.getValue(relationFlags);
300304

301-
encoded = setLong(encoded, K_PRIORITY_LONG, handlePriority(way, priorityFromRelation));
305+
encoded = setLong(encoded, PriorityWeighting.KEY, handlePriority(way, priorityFromRelation));
302306

303307
// bike maxspeed handling is different from car as we don't increase speed
304-
speed = reduceToMaxSpeed(way, speed);
308+
speed = applyMaxSpeed(way, speed, false);
305309
encoded = handleSpeed(way, speed, encoded);
306310
encoded = handleBikeRelated(way, encoded, relationFlags > UNCHANGED.getValue());
307311

@@ -316,18 +320,6 @@ public long handleWayTags( OSMWay way, long allowed, long relationFlags )
316320
return encoded;
317321
}
318322

319-
protected double reduceToMaxSpeed( OSMWay way, double speed )
320-
{
321-
double maxSpeed = getMaxSpeed(way);
322-
// apply only if smaller maxSpeed
323-
if (maxSpeed >= 0)
324-
{
325-
if (maxSpeed < speed)
326-
return maxSpeed * 0.9;
327-
}
328-
return speed;
329-
}
330-
331323
int getSpeed( OSMWay way )
332324
{
333325
int speed = PUSHING_SECTION_SPEED;
@@ -458,7 +450,8 @@ void collect( OSMWay way, TreeMap<Double, Integer> weightToPrioMap )
458450
if ("cycleway".equals(highway))
459451
weightToPrioMap.put(100d, VERY_NICE.getValue());
460452

461-
if (preferHighwayTags.contains(highway))
453+
double maxSpeed = getMaxSpeed(way);
454+
if (preferHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 30)
462455
{
463456
weightToPrioMap.put(40d, PREFER.getValue());
464457
if (way.hasTag("tunnel", intendedValues))
@@ -468,7 +461,6 @@ void collect( OSMWay way, TreeMap<Double, Integer> weightToPrioMap )
468461
if (pushingSections.contains(highway) || "parking_aisle".equals(service))
469462
weightToPrioMap.put(50d, AVOID_IF_POSSIBLE.getValue());
470463

471-
double maxSpeed = getMaxSpeed(way);
472464
if (avoidHighwayTags.contains(highway) || maxSpeed > 80)
473465
{
474466
weightToPrioMap.put(50d, REACH_DEST.getValue());
@@ -516,12 +508,6 @@ else if (roadValues.contains(highway))
516508
return wayTypeEncoder.setValue(encoded, wayType.getValue());
517509
}
518510

519-
/**
520-
* Reports wether this edge is unpaved.
521-
*/
522-
public static final int K_UNPAVED = 100;
523-
public static final int K_PRIORITY = 101, K_PRIORITY_LONG = 102;
524-
525511
@Override
526512
public long setBool( long flags, int key, boolean value )
527513
{
@@ -551,7 +537,7 @@ public double getDouble( long flags, int key )
551537
{
552538
switch (key)
553539
{
554-
case K_PRIORITY:
540+
case PriorityWeighting.KEY:
555541
double prio = preferWayEncoder.getValue(flags);
556542
if (prio == 0)
557543
return (double) UNCHANGED.getValue() / BEST.getValue();
@@ -567,7 +553,7 @@ public long getLong( long flags, int key )
567553
{
568554
switch (key)
569555
{
570-
case K_PRIORITY_LONG:
556+
case PriorityWeighting.KEY:
571557
return preferWayEncoder.getValue(flags);
572558
default:
573559
return super.getLong(flags, key);
@@ -579,7 +565,7 @@ public long setLong( long flags, int key, long value )
579565
{
580566
switch (key)
581567
{
582-
case K_PRIORITY_LONG:
568+
case PriorityWeighting.KEY:
583569
return preferWayEncoder.setValue(flags, value);
584570
default:
585571
return super.setLong(flags, key, value);
@@ -612,44 +598,6 @@ protected long handleSpeed( OSMWay way, double speed, long encoded )
612598
return encoded;
613599
}
614600

615-
enum PriorityCode
616-
{
617-
/* Inspired by http://wiki.openstreetmap.org/wiki/Class:bicycle
618-
"-3" = Avoid at all cost.
619-
"-2" = Only use to reach your destination, not well suited.
620-
"-1" = Better take another way
621-
"0" = as well as other ways around.
622-
Try to to avoid using 0 but decide on -1 or +1.
623-
class:bicycle shall only be used as an additional key.
624-
"1" = Prefer
625-
"2" = Very Nice way to cycle
626-
"3" = This way is so nice, it pays out to make a detour also if this means taking
627-
many unsuitable ways to get here. Outstanding for its intendedValues usage class.
628-
*/
629-
//We can't store negative numbers into our map, therefore we add
630-
//unspecifiedRelationWeight=4 to the schema from above
631-
WORST(0),
632-
AVOID_AT_ALL_COSTS(1),
633-
REACH_DEST(2),
634-
AVOID_IF_POSSIBLE(3),
635-
UNCHANGED(4),
636-
PREFER(5),
637-
VERY_NICE(6),
638-
BEST(7);
639-
640-
private final int value;
641-
642-
private PriorityCode( int value )
643-
{
644-
this.value = value;
645-
}
646-
647-
public int getValue()
648-
{
649-
return value;
650-
}
651-
};
652-
653601
private enum WayType
654602
{
655603
ROAD(0),

0 commit comments

Comments
 (0)