Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write custom cost tags #653

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/conveyal/osmlib/main/SpeedSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public static void main (String[] args) throws Exception {
String[] fields = line.split(",");
long osmWayId = Long.parseLong(fields[0]);
double speedKph = Double.parseDouble(fields[1]);
double walkFactor = Double.parseDouble(fields[2]);
double bikeFactor = Double.parseDouble(fields[2]);
Way way = osm.ways.get(osmWayId);
// R5 currently prioritizes maxspeed:motorcar above all other maxspeed tags
way.addOrReplaceTag("maxspeed:motorcar", String.format("%1.1f kph", speedKph));
way.addOrReplaceTag("walk_factor", String.format("%1.1f", walkFactor));
way.addOrReplaceTag("bike_factor", String.format("%1.1f", bikeFactor));
osm.ways.put(osmWayId, way);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/conveyal/r5/streets/CustomBikeCostSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.conveyal.r5.streets;

public class CustomBikeCostSupplier implements SingleModeTraversalTimes.Supplier {
private final CustomCostTags tags;

public CustomBikeCostSupplier(CustomCostTags tags) {
this.tags = tags;
}

@Override
public double perceivedLengthMultipler () {
return tags.bikeFactor;
}

@Override
public int turnTimeSeconds (SingleModeTraversalTimes.TurnDirection turnDirection) {
return 0;
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/conveyal/r5/streets/CustomCostTags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.conveyal.r5.streets;

import com.conveyal.osmlib.Way;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* For both directions (forward and backward) of a single OSM Way, tags used to represent the per-edge factors
* contributing to generalized costs.
*/
public class CustomCostTags {

private static final Logger LOG = LoggerFactory.getLogger(CustomCostTags.class);

final double walkFactor;
final double bikeFactor;

public CustomCostTags(Way way) {
walkFactor = parseDoubleTag(way, "walk_factor");
bikeFactor = parseDoubleTag(way, "bike_factor");
}

/**
* Read a single tag from the given OSM way and interpret it as a double-precision floating point value.
* If no tag is present, or if the tag cannot be parsed as a double, use default generalized cost value (1).
*/
private static double parseDoubleTag (Way way, String tagKey) {
String tagValue = way.getTag(tagKey);
if (tagValue == null) {
return 1;
}
try {
return Double.parseDouble(tagValue);
} catch (NumberFormatException nfe) {
LOG.error("Could not parse generalized cost tag as a double: " + tagValue);
return 1;
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/conveyal/r5/streets/CustomWalkCostSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.conveyal.r5.streets;

public class CustomWalkCostSupplier implements SingleModeTraversalTimes.Supplier {
private final CustomCostTags tags;

public CustomWalkCostSupplier(CustomCostTags tags) {
this.tags = tags;
}

@Override
public double perceivedLengthMultipler () {
return tags.walkFactor;
}

@Override
public int turnTimeSeconds (SingleModeTraversalTimes.TurnDirection turnDirection) {
return 0;
}
}
13 changes: 7 additions & 6 deletions src/main/java/com/conveyal/r5/streets/EdgeTraversalTimes.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ public int turnTimeSeconds (int fromEdge, int toEdge, StreetMode streetMode) {

public void setEdgePair (int forwardEdge, Way way) {
int backwardEdge = forwardEdge + 1;
LaDotCostTags forwardTags = new LaDotCostTags(way, FORWARD);
walkTraversalTimes.setOneEdge(forwardEdge, new LaDotWalkCostSupplier(forwardTags));
bikeTraversalTimes.setOneEdge(forwardEdge, new LaDotBikeCostSupplier(forwardTags));
LaDotCostTags backwardTags = new LaDotCostTags(way, BACKWARD);
walkTraversalTimes.setOneEdge(backwardEdge, new LaDotWalkCostSupplier(backwardTags));
bikeTraversalTimes.setOneEdge(backwardEdge, new LaDotBikeCostSupplier(backwardTags));
CustomCostTags tags = new CustomCostTags(way);
CustomWalkCostSupplier walkSupplier = new CustomWalkCostSupplier(tags);
CustomBikeCostSupplier bikeSupplier = new CustomBikeCostSupplier(tags);
walkTraversalTimes.setOneEdge(forwardEdge, walkSupplier);
bikeTraversalTimes.setOneEdge(forwardEdge, bikeSupplier);
walkTraversalTimes.setOneEdge(backwardEdge, walkSupplier);
bikeTraversalTimes.setOneEdge(backwardEdge, bikeSupplier);
}

public void summarize () {
Expand Down