Get length of each edge in my route


I want to get the length and osm-way-id for each edge in my route. I got the osm-way-id from encoded values, however, the ‘max_length’ in the encoded values did nothing.

The length of an edge is definitely not zero, otherwise it won’t be a route of 2 km. Is this max-length (in encoded values) the length of the each edge or it is something else? How I can get the length of each edge of my route?


You can use the distance path detail instead, which will give you the length per edge (note that OSM ways can consist of multiple edges). For distance (or time) path details there is no separate encoded value required.

The max_length encoded value is for length-restricted roads (roads with a rather rare sign that indicates a maximum length of the vehicle)

Thank you so much. I got the length of each edge.

image image

I can now match the osm-ids with edges (for same osm-id for multiple edges). However, is there a way to get all the edges of the graph and corresponding osm-ids and length at once before searching for routes for all my OD pairs? It will save me a lot of post-processing for each of the routes separately.

If you use Java you can fetch the information easily in a loop over all edges.

Another idea could be to use the shortest path tree API: GitHub - graphhopper/graphhopper: Open source routing engine for OpenStreetMap. Use it as Java library or standalone web server.

I tried the following. Please let me know if this is the right way…


However, when I try to match with the individual edge lengths of the route (that I got from distance details), it does not match for some edges. for example:


Also, would you please suggest me how to add the corresponding osm-ids here.

You can fetch the osmIDs via allEdges.get(osmIDEnc) and you get osmIDEnc via
IntEncodedValue osmIDEnc = encodingManager.getIntEncodedValue(OSMWayID.KEY)

it does not match for some edges

This shouldn’t be the case. Make sure that the first and last edge are complete (i.e. the start and end are on junctions) when you compare the distances.

I was trying to add the lines you suggested. Added …

EncodingManager encodingManager = hopper.getEncodingManager();
IntEncodedValue osmIDEnc = encodingManager.getIntEncodedValue(OSMWayID.KEY);

But it gives an error of “Exception EncodingManager not yet built”. How to set the encoding manager? I also tried the following but it did not work.


And now, the previous working part is not working anymore and gives me error of “GraphHopper storage not initialized”. What wrong did I do and why this one is not working anymore?

while ( {
int alledgeId = allEdges.getEdge();
double edge_len = allEdges.getDistance();
double osm_ids = allEdges.get(osmIDEnc);

gh_test - (6.3 KB)

Have a look into the LowLevelAPIExample and then you can also read the low level API documentation for some more details:

Oh…now it works. I was putting them under createGraphHopperInstance rather routing.
Thank you so much. (5.4 KB)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.