Instead of creating a new Weighting implementation is is highly recommended to use the CustomWeighting instead, which is explained in the profiles and custom models section.
A weighting allows to create a "weight" for an edge. The weight of an edge reflects the cost of travelling along this edge. All implementations of RoutingAlgorithm in GraphHopper calculate the shortest path, which means the path with the lowest overall cost from A to B. The definition of the cost function is up to you, so you can create a cost function (we call this weighting) for the fastest path. GraphHopper will still calculate the path with the lowest cost, but you can define the cost as a mix of distance speed, as shown in the FastestWeighting.
In order to create a custom weighting you need to do the following:
- Implement the Weighting class and the WeightingFactory
- Let the GraphHopper class know about your WeightingFactory via overriding createWeightingFactory
A simple weighting is the ShortestWeighting, it calculates the shortest path. You could go from there and create your own weighting.
If you only want to change small parts of an existing weighting, it might be a good idea to extend the AbstractAdjustedWeighting,
a sample can be found in the AvoidEdgesWeighting.
If your weights change on a per-request base, like the BlockAreaWeighting,
you cannot use the 'speed mode', but have to use the 'hybrid mode' or 'flexible mode' (more details here).
If you haven't disabled the 'speed mode' in your config, you have to disable it for the requests by appending ch.disable=true
in the request url.