How to expand a matched trip along the graph

I try to use Map matching for my usecase, where I have a set of gpx points, want to Match them on the map and after that select possible Furter travelings on the road the gpx path is at the end.
My question is, how to Traverse the result of the mapmatching Furter, so from the last point of the match, geht the next edges and Nodes inside the graph. I searched the low level API and others Docs but coukdn figure the data structure out.

TL’DR: given a Edge in the graph i want to fetch the next possible, neighbouring edges. How to do that?

Thanks in Advance,

You have two nodes of one edge. Then you can do for one node:

explorer = graph.createEdgeExplorer()
edgeIter = explorer.setBaseNode(node);
// loop through all edges of this node
while( {
   // now you can access the edge properties, e.g. exclude the 'current edge
   if(edgeIter.getEdge() != currentEdgeState.getEdge())
   // edgeIter.getAdjNode, edgeIter.getFlags, ....

Thanks for that snippet, will try that!

EdgeIter.getAdjNode gives me the next node in driving Direction of that edge? (Couldnt find docu on that)

Kind of yes, but ‘driving direction’ is too hard to define in this context.

If you do explorer.setBaseNode(x) you define an ‘anchor’ and get all edges where base node is x and adj node (adjacent node) is the other node of the single edge. It is important to note that the accessibility for every vehicle could be completely different, it could even be not accessible at all for that vehicle. If you want a filter and get just outgoing of one specific vehicle do:

explorer = graph.createEdgeExplorer(new DefaultEdgeFilter(encoder, false, true))

Ok, Thanks for your Input :slight_smile: i will come back here if I have problems implementing it, but it looks Straight forward.

Thanks a lot.

It works quite well, my current problem is, that from map-matching I get the whole edge returned, the match doesn’t end at the nearest point on the road where my gpx-track started and ended.
I think virtual nodes and edges are the solution for this in GraphHopper, isn’t it? How would I insert the nearest point on the graph to my start end endpoints to the graph to let the matching start end end exactly there?

I found GraphHopper.lookup() that I think is supposed to give the points and combined with QueryGraph.lookup() will add the virtual nodes. But I am unable to call hopper.lookup on my GraphHopper instance. Is there an interface for that or am I missing something?

I think virtual nodes and edges are the solution for this in GraphHopper


In the map matching we create a QueryGraph which normally includes these virtual edges&nodes. So I’m currently not sure if your observed behaviour is intended. The edge IDs cannot contain virtual edges, that is true, but the geometry should be GPS-point-precise not only junction-precise.

see this gist:

I’ve generated a track with graphhopper webPage and mapMatched that with the current git master and a current .pbf file of BadenWuerthemberg. The snapped path is longer in both directions.

Sorry, i maybe missed the question:
Is this intended behaviour? And if yes, how can I add virtual nodes to the graph before mapmatching? Because GraphHopper.lookup is protected, I think I cant call it. Is there an interface for it to access it from outside?

Edit: contains the call. So it is probably not intended behaviour. Maybe my accessing of the result is wrong? Do I have to somehow access the queryGraph that the MapMatching generates?

                MatchResult mr = mapMatching.doWork(inputGPXEntries);
                NodeAccess nodeAccess = graph.getNodeAccess();
                int node = mr.getEdgeMatches().get(0).getEdgeState().getBaseNode();
                doSomethingWith(nodeAccess.getLat(node), nodeAccess.getLon(node))
		for (EdgeMatch match : mr.getEdgeMatches()) {
			for (GHPoint3D point : match.getEdgeState().fetchWayGeometry(2)){
			        doSomethingWith(point.getLat(), point.getLon())

Edit2: says:

// replace virtual edges with original *full edge* at start and end!

does that remove the virtual edges from the result and therefore contains the full edge?