Skip to content

Commit

Permalink
Add support tag combinations cycleway:left and cycleway:right, fixes g…
Browse files Browse the repository at this point in the history
  • Loading branch information
ratrun committed Apr 12, 2024
1 parent 3f9b3bd commit 1d6b5d9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
24 changes: 19 additions & 5 deletions core/src/main/java/com/graphhopper/reader/ReaderElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,27 @@ public boolean hasTag(List<String> keyList, Object value) {
return false;
}

/**
* Returns the first matching value of the specified list of keys where the order is important.
*
* @return an empty string if nothing found or the value does not match to one of the list of expected values
*/
public String getFirstMatchingValue(List<String> searchedTags, List<String> searchedValues) {
for (String str : searchedTags) {
Object value = properties.get(str);
if ((value != null) && (searchedValues.contains(value)))
return (String) value;
}
return "";
}

/**
* Returns the first existing value of the specified list of keys where the order is important.
*
* @return an empty string if nothing found
*/
public String getFirstValue(List<String> restrictions) {
for (String str : restrictions) {
public String getFirstValue(List<String> searchedTags) {
for (String str : searchedTags) {
Object value = properties.get(str);
if (value != null)
return (String) value;
Expand All @@ -178,9 +192,9 @@ public String getFirstValue(List<String> restrictions) {
/**
* @return -1 if not found
*/
public int getFirstIndex(List<String> restrictions) {
for (int i = 0; i < restrictions.size(); i++) {
String str = restrictions.get(i);
public int getFirstIndex(List<String> searchedTags) {
for (int i = 0; i < searchedTags.size(); i++) {
String str = searchedTags.get(i);
Object value = properties.get(str);
if (value != null)
return i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.graphhopper.storage.IntsRef;

import java.util.*;
import java.util.stream.Stream;

import static com.graphhopper.routing.ev.RouteNetwork.*;
import static com.graphhopper.routing.util.PriorityCode.*;
Expand Down Expand Up @@ -182,9 +183,12 @@ void collect(ReaderWay way, double wayTypeSpeed, TreeMap<Double, PriorityCode> w
weightToPrioMap.put(50d, worse == EXCLUDE ? REACH_DESTINATION : worse);
}
}

String cycleway = way.getFirstValue(Arrays.asList("cycleway", "cycleway:left", "cycleway:right", "cycleway:both"));
if (Arrays.asList("lane", "opposite_track", "shared_lane", "share_busway", "shoulder").contains(cycleway)) {
final List<String> searchedTags = Arrays.asList("cycleway", "cycleway:left", "cycleway:right", "cycleway:both");
final List<String> slightPreferredTags = Arrays.asList("lane", "opposite_track", "shared_lane", "share_busway", "shoulder");
final List<String> preferredTags = Arrays.asList("track");
final List<String> searchedValues = Stream.concat(slightPreferredTags.stream(), preferredTags.stream()).toList();
String cycleway = way.getFirstMatchingValue(searchedTags , searchedValues);
if (slightPreferredTags.contains(cycleway)) {
weightToPrioMap.put(100d, SLIGHT_PREFER);
} else if ("track".equals(cycleway)) {
weightToPrioMap.put(100d, PREFER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ public void testCycleway() {
way.setTag("highway", "primary");
way.setTag("cycleway:right", "lane");
assertPriority(SLIGHT_PREFER, way);
way.setTag("cycleway:left", "no");
assertPriority(SLIGHT_PREFER, way);

way.clearTags();
way.setTag("highway", "primary");
Expand Down
4 changes: 2 additions & 2 deletions docs/core/weighting.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Weighting

Instead of creating a new Weighting implementation is is highly recommended to use the CustomWeighting instead, which is explained in
Instead of creating a new Weighting implementation it is highly recommended to use the CustomWeighting instead, which is explained in
the [profiles](profiles.md) and [custom models](custom-models.md) section.

A weighting calculates the "weight" for an edge. The weight of an edge reflects the cost of travelling along this edge.
Expand All @@ -24,4 +24,4 @@ If you only want to change small parts of an existing weighting, it might be a g
a sample can be found in the [AvoidEdgesWeighting](https://github.com/graphhopper/graphhopper/blob/master/core/src/main/java/com/graphhopper/routing/weighting/AvoidEdgesWeighting.java).
If your weights change on a per-request base you cannot use the 'speed mode', but have to use the 'hybrid mode' or 'flexible mode' (more details [here](https://github.com/graphhopper/graphhopper#technical-overview)).
If you haven't disabled the 'speed mode' in your config, you have to disable it for the requests by appending `ch.disable=true`
in the request url.
in the request url.

0 comments on commit 1d6b5d9

Please sign in to comment.