Custom edge weights from external data in GraphHopper 4.0

Hey everyone,
I am currently reading through a lot of older posts, code and documentation, trying to figure out how to implement my own custom weights to consider during the route calculation.

My input would be a Map of OSM way-id and custom weight pairs which I would then apply to the graphhopper segments. I’ve already bumped into posts like these or these where similar points have been discussed. However the answers seem to be either missing, or outdated.

I understand that in principle I would need to extend the default GraphHopper class with an own weighting method, and also extend some FlagEncoder to support my custom properties, however I am unsure how everything fits together in v4.0.

Can anyone provide some first steps for me to look at, or maybe there is documentation that I might have skipped?

I suggest to create a new EncodedValue that you fill with the value of your choice (name it e.g. myweight) and then consider this value in the custom model via

"priority": [
{ "if": "myweight < 0.5",
  "multiply_by": 0.3 
}]

For this there is usually no need to touch FlagEncoders or Weighting code.

Regarding the custom models see the documentation and this blog post.

See also e.g. this discussion.

1 Like

Thanks a lot for the quick reply, this makes perfect sense.
So after implementing my own EncodedValue, I would need to implement a custom TagParser class which then applies my weightings during handleWayTags, right?

The only question then is, if ReaderWay Ids are the same thing as OSM Way Ids, and if not how I should map them. Otherwise, thank you so much!

I would need to implement a custom TagParser class which then applies my weightings during handleWayTags, right?

Yes, and add it to your EncodedValueFactory and TagParserFactory (extend the default factories) which you then set to the GraphHopper class.

Optionally you can add myweight to a subclass of PathDetailsBuilderFactory so that you can easily see the values in the response path via &details=myweight.

The only question then is, if ReaderWay Ids are the same thing as OSM Way Ids

Yes, they are the same. (ReadyWay = OSM way and ReaderNode = OSM node etc.)

1 Like
Powered by Discourse