Get edge priority, distance and speed

Helle everyone!

I’m new at Graphhopper and I’m trying to create my own custom weighting class for testing purposes. I want to write my own weighting formula in the calcEdgeWeight method, meaning i need to access the edges properties (speed, priority and distance) and I don’t know how to do it.

Any help would be greatly appreciated!

Are you trying to use your own version of the CustomWeighting class to use the yaml/json syntax, or would you just like to write your own Weighting from scratch?

I’m trying to write my own weighting from scratch

Take a look at DefaultWeightingFactory. You can return your own implementation of the Weighting interface there. There you already have access to encodingManager, which allows you to read the edge properties. For example a very simple weighting based on road class might look like this:

 private static class MyWeighting implements Weighting {
        private final EnumEncodedValue<RoadClass> roadClassEnc;
        MyWeighting(EncodingManager encodingManager) {
            roadClassEnc = encodingManager.getEnumEncodedValue(RoadClass.KEY, RoadClass.class);
        }

        @Override
        public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
            RoadClass roadClass = edgeState.get(roadClassEnc);
            if (roadClass == RoadClass.MOTORWAY) return edgeState.getDistance();
            else return 2 * edgeState.getDistance();
        }
    }

Thank you very much for your quick answer, it’s clear for me now!

I just have one last question. I want this weighting to be used only for a foot
profile, what changes in my java code and in the config file do I need to perform?

You can add your weighting here using some arbitrary name like my_weighting. Then in your config.yml file you will just add the foot profile like:

profiles:
  - name: my_foot
    weighting: my_weighting

Thank you very much!!