0

Wanted to share some of my code I use to get all possible routes from a random geopoint in osm2po.

 Graph graph = loadGraph();

 OverlayNode node = new OverlayNode(lat, lon);
 if (node.snap(graph, (int[])null)) {
        List<Integer> edges = new LinkedList<>();
        edges.add(node.getEdgeIdx());
        int reverseId = graph.findReverseEdge(node.getEdgeIdx());

        if (reverseId > 0) {
            edges.add(reverseId);
        }
        for (int edgeIdx : edges) {
            routeFrom(edgeIdx);
        }
    }

Seems to work like I want to work (only getting the reverse edge if it's drivable) - but is this the correct way to do this or is there already a method that will give me all edges for a given point?

2
  • are you looking for outgoing edges of real crossings only or rather random, even virtual locations somewhere on arbitrary edges?
    – Carsten
    Commented Feb 25, 2015 at 10:35
  • No, just real edges for real crossings. I basically just want to know all possible legal ways to drive out of the current point.
    – kozyr
    Commented Feb 25, 2015 at 18:35

1 Answer 1

1

With reference to the answer in the comment above:
It is sufficient to use the static Graph.

The OverlayGraph is not needed here:

Graph graph = new Graph(...)
int vertexId = graph.findClosestVertexId(lat, lon); // Find nearest crossing
int[] edgeIdxs = graph.findOutgoingEdges(vertexId);

edgeIdxs is an array of indexes (pointers) to the edges inside the graph

Further informations of the edge can be retrieved by getting one of the getEdge*()-arrays. e.g.: Determine sourceId and targetId of a given edge:

int targetId = graph.getEdgeTargetIds()[edgeIdx];
int sourceId = graph.getEdgeSourceIds()[edgeIdx];

In order to retrieve geometry or (street)name call

graph.lookupGeometry(edgeIdx) or graph.lookupName(edgeIdx)

or for lazy people: lookup almost everything: graph.lookupSegment(edgeIdx)

3
  • So one way it doesn't work for me is for one-directional streets - so if let's say my street goes from 1 (source) to 2 (target), and I click in the middle, I'm really only interested in this particular edge 1->2 as my starting street. But if findClosestVertexId returns 2 for me, I don't really know where the street starts - I could get all incoming edges but then I wouldn't know if I'm on 3->2 or 1->2 street, for example.
    – kozyr
    Commented Mar 9, 2015 at 19:41
  • 1
    Then it's a misunderstanding. I asked if it is sufficient to find crossings (see above). To find the closest edge, your first approach was already correct. node.snap(...) returns the needed information, namely exactly one edge (forward). To find the reverse edge (if exists) call graph.findReverseEdge(...) as you did. In some rare cases a vertex is closer than an edge. Therefore call node.isOnVertex() and/or node.isOnEdge() before and fallback to graph.findOutgoingEdges(node.getId()) if necessary.
    – Carsten
    Commented Mar 11, 2015 at 8:36
  • Thanks Carsten. I just run into a situation where edgeId was < 0, and I couldn't figure out why. Now I know :)
    – kozyr
    Commented Mar 11, 2015 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.