Reduce number of turns

Do you have any suggestion on how to control the number of turns in a path? The goal would be to create paths with a reduced number of turns (I’m using a custom vehicle and weighting scheme that produces very fragmented paths while I’d like to have “smoother” ones).

I played a bit with the getTurnCost function but I didn’t see any changes in the resulting paths.


You’ll need to add turn costs for all the junctions or turns you need and then evaluate this in the TurnWeighting. See the unit tests for these classes.

To go more into the details of what I’m trying to implement, the ideal scenario would be to reduce the angle in a turn. Let’s imagine each turn has a cost in degrees of rotation, e.g., a 90 degrees turn is worse than a 45 degrees turn and in the same framework going straight should be preferred when possible since it doesn’t involve any rotation. Do you think is feasible to implement such a schema?

Moreover, is it possible to access from a flagencoder to the geometry of an edge?


Yes, you’ll need to add a turn cost to every junction that is affected.

Moreover, is it possible to access from a flagencoder to the geometry of an edge?

This is nothing vehicle dependent, so you can do edge.fetchWayGeometry

Could you please be more specifically on the actual classes and steps I have to follow to implement so?

For example:

  • Should I implement a subclass of TurnCostExtension? My final goal would be to assign a cost that depends on the angle of rotation of a turn.
  • What is the role of the TurnCostEncoder interface on that?
  • How can I enable the turn cost weighting using a custom weighting I’ve already implemented? I’ve seen the example in TurnWeighting, should I just add the prefix “turn|” to my weighting scheme in the configuration file?

Any help would be very appreciated!



  1. Specify in e.g. the CarFlagEncoder constructor how many bits you want to use for turn costs. E.g. 1 bit means just allow or block, 2 bits could be used to differentiate between 4 states (e.g. no costs, small, big, blocking) and so on
  2. Get TurnCostExtension from graph
  3. Use addTurnInfo() in TurnCostExtension class
  4. the ‘turn flags’ for turn costs can be retrieved via: “turnCostFlagEncoder.getTurnFlags(false, 2);”

See e.g. EdgeBasedRoutingAlgorithmTest for some use cases.

1 Like