Java low level API custom weighting not working

Hello,

I am trying to create a custom weighting that is based both on distance as well as proximity to a forest. I have already gathered the required data, as well as created a method that returns the distance in meters to the nearest forest for a lat/long coordinate.

This is how I implemented my weighting:

    @Override
    public double calcEdgeWeight(EdgeIteratorState edgeIteratorState, boolean reverse) {
        int totalValue = 0;
        for (GHPoint3D ghPoint3D : edgeIteratorState.fetchWayGeometry(FetchMode.ALL)) {
            final double lon = ghPoint3D.getLon(), lat = ghPoint3D.getLat();
            final double[] converted = conversionUtil.convertGeoToPixel(lat, lon);

            try {
                totalValue += tiffReader.getValueAt((int) Math.round(converted[0]), (int) Math.round(converted[1]));
            } catch (IOException e) {
                System.out.println("Couldn't read value from data sheet!");
            }
        }
        return edgeIteratorState.getDistance() / 2 + Math.pow(totalValue, 2);
    }

I use the custom weighting by overriding the createWeightingFactory() method in the GraphHopper class. Using the java demo code, the weight simply does not apply. I thought it might have something to do with caching, so I disabled speed mode by removing the following line from the default createGraphHopperInstance method.

    static GraphHopper createGraphHopperInstance(String ghLoc) {
        GraphHopper hopper = new WeightedGraphHopper(tiffReader);

        hopper.setOSMFile(ghLoc);
        // specify where to store graphhopper files
        hopper.setGraphHopperLocation("target/routing-graph-cache");

        // see docs/core/profiles.md to learn more about profiles
        hopper.setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest").setTurnCosts(false));

        // this enables speed mode for the profile we called car
        // ->> REMOVED hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));

        // now this can take minutes if it imports or a few seconds for loading of course this is dependent on the area you import
        hopper.importOrLoad();
        return hopper;
    }

The routing request now loads infinitely, even when changing my forest weight to only use the distance. I tried adding this line to enable flexible routing:

hopper.getLMPreparationHandler().setLMProfiles(new LMProfile("car"));

The routing request now loads for a very long time, but still doesn’t use my weighting.
At this point I’m convinced it’s just a small piece of code I’m missing, but I can’t seem to figure it out.

If anyone could point me in the right direction, that would be greatly appreciated.
Please note, I do not want to use a config file, I only want to use bare java and the low level API I imported using maven.

Thanks in advance!

Cheers,
Fynn

First thing I would do is disabling CH (like you did), disable LM as well, delete the target/routing-graph-cache folder, and then run your code using a debugger to find out if your desired weighting is actually being created.

Yes! That did the trick! I just had to clear out the cache folder, leaving it in CH is fine too. Thanks a lot!

Powered by Discourse