Create a new Weighting

I’m trying to create a new Weighting class, weighting.md says that the easiest way to add a new weighting is to extend Graphhopper class and override the createWeighting method.
However the method is final, thus there is no way to override it.
I copied the DefaultWeightingFactory and added the new weighting.
Is there a faster or cleaner way to add a new weighting function?
Thanks

Ah, this info is outdated in master. An update of the docs via a pull request would be appreciated :slight_smile:

Can you try to extend DefaultWeightingFactory (instead of copying it) and add your weighting?

Yeah, I thought about extending the class right after I wrote this topic.
However I would like to use a my version of TurnCostProvider with the CustomWeighting, however the CustomWeighting can be created only using CustomWeighting.Parameters that are accessible only from the graphhopper package.
My idea was to extend the DefaultWeightingFactory and override createWeighting in something like
@Override
public Weighting createWeighting(Profile profile, PMap requestHints, boolean disableTurnCosts) {
TurnCostProvider turnCostProvider = new MyTurnCostProvider();
super.createWeighting(profile, requestHints, disableTurnCosts);

But the createWeighting of DefaultWeightingFactory assigns by itself the turnCostProvider.
Is there a way to solve this problem?

You could create the Weighting in the subclass:

if("myweighting".equals(weightingStr))
  return new FastestWeighting(encoder, hints, new MyTurnCostProvider());
super.createWeighting(profile, requestHints, disableTurnCosts);
1 Like

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