How to determine allowed directions of travel for an edge?

When querying data from the LocationIndex, is it possible to determine the allowed directions of travel for the returned EdgeIteratorState? I’m trying to run a getClosestEdge() query and find whether or not the road is one-way, and if so what direction it is in.

I’ve tried looking at the isForward and isBackward properties, but from ohter questions here it seems that is to do with how the query is iterating over the graph.

Those properties are relative to the base node and should indeed give you this information, of course you need to use a car or bike not e.g. a foot encoder. Is this not working for you?

Thank you for such a quick reply!

I am currently inspecting the forward and backward properties the manner edge.isForward(new CarFlagEncoder()) - is this right? I have tried on both one-way and two-way roads and in each case the result always seems to be false for both. I have CH disabled.

Also, the EdgeIteratorState returned from my queries always seems to have the base and adjacent node in the opposite direction to the allowed direction of travel. This matches up with what you said at https://lists.openstreetmap.org/pipermail/graphhopper/2013-October/000492.html, “the edge iterator does not necessarily reflect the direction of a street”. So even if I can get the forward and backward properties working OK, surely this means I don’t know the “real” direction of travel?

Currently that is the API, yes. But do not use a new CarFlagEncoder instance, instead use the one you passed to the EncodingManager, or fetch it from there: encodingManager.getEncoder(“car”)

BTW: we try to improve the usage of this in this issue: Refactoring to reduce set/getFlags usage · Issue #472 · graphhopper/graphhopper · GitHub

the EdgeIteratorState returned from my queries always seems to have the base and adjacent node in the opposite direction to the allowed direction of travel

The adjacent and base are relative to the exploration in the graph. Assume the one-way 1->2 then if base node = 1 and adj node = 2 then forward is true and backward is false, but if base=2 and adj=1 then forward is false and backward is true.

Using the same encoder instance did the trick. Thank you! :slight_smile:

1 Like

Hi I have exactly the same question as the original post - from a getClosestEdge() query how do i determine if the edge is one way in the underlying OSM data and what that direction is. If i call fetchWayGeometry it sometimes returns the points in the right order and other times not. Really struggling to find any good documentation on this. Any help would be greatly appreciated.

Information about which roads are one-ways is stored using access flags for each edge. This depends on the vehicle/encoder. To find out if an edge is a one way you therefore need to use the access encoded value:

EdgeIteratorState edge = queryResult.getClosestEdge();
int nodeA = edge.getBaseNode();
int nodeB = edge.getAdjNode();
boolean edgeCanBeTraveledFromAToB = edge.get(flagEncoder.getAccessEnc());
boolean edgeCanBeTraveledFromBToA = edge.getReverse(flagEncoder.getAccessEnc());

Thank you so much. This is a massive help, i was beginning to think i would have to give up and do this all by hand!

This is an old topic but we’re running into an issue where 2 stationary points at the same GPS location at getting routed onto a uni-directional edge in both the forward and backwards direction:

p1 on edge1 where edge1 A->B
p2 on edge1 where edge1 B->A (reverse=true)

p1 and p2 coordinates are identical
edge1 is one-way where the only allowable direction of travel is from A->B

Questions:

  • Why is p2 routed onto the same edge but in the reverse direction?
  • When reverse=true, does this imply that the way geometry of the edge and all its internal flags are reversed relative to the travel direction?

Can you provide a reproducible example?

Why is p2 routed onto the same edge but in the reverse direction?

Maybe the snapping to the edge is not fully deterministic? If this is the case we should of course fix this. This is especially surprising because you said it is a one-way edge.

When reverse=true, does this imply that the way geometry of the edge and all its internal flags are reversed relative to the travel direction?

Yes, at least for those edge properties that differ between the two directions. For example the speed can be different for the two directions and the way geometry will be reversed, but encoded values like ‘surface’ are usually the same for both directions.