Custom edge weighting by OSM type "highway"

Hi @all,

i want to weight an OSM way with a custom weighting factor. For example:

  • ‘highway:trunk’ --> good (factor 1)
  • ‘highway:residential’ --> bad (factor 5)

In other words: I prefer a routing over trunks.

How can i do that with graphhopper?
Where can i inject my code (CustomWeighting) or extend which classes? Can i, and how, encode this by a flag of an edge?

I use both modes: CH and none CH.

My first draft was to save for each edge a custom factor and apply this factor in my weighting-class:

public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
double weight = original.calcWeight(edgeState, reverse, prevOrNextEdgeId);
int factor = osmDataProvider.getOSMWayFactor(edgeState.getEdge());
return weight * factor;

However, it’s not a good practice to map an ID to a factor. In addition: There are incoming edge IDs which are unknown by my osm-data-provider. (The osm-data-provider saves in a preprocessing step for each edge ID a factor.)

Is there a better way to achieve a custom edge weighting depended by some OSM-tags?
Does graphhopper supports this weighting mechanism for “highway” out of the box?

Best regards,


Is there a better way to achieve a custom edge weighting depended by some OSM-tags?

The DataFlagEncoder might help you here (?)

Yes, some edges are virtual as we introduce new edges and nodes for every location in the request (of course we introduce this on a per-request basis, so that requests are independent of each other)

Ok, thanks for your advice.

Can you share an example how can we have an access to DataFlagEncoder.class methods from calcWeight() in weighting class? While we don’t have an exact object of DataFlagEncoder here.

DataFlagEncoder is long gone. Customizing edge weights can now be done with custom profiles, see in the documentation.

Powered by Discourse