2

I would like to block certain road and avoid it when generating route.

I'm using the Graphhopper basic map sample

I found this code Weighting. I believe this is the function I'm looking for but I have no luck integrating it.

I really appreciate any help in showing how I can put the two code together. A sample code is very much appreciated.

Thanks in advance.

Mellorine
  • 265
  • 1
  • 5
  • 13
  • Have a look into this class: https://github.com/graphhopper/graphhopper/blob/master/core/src/main/java/com/graphhopper/routing/weighting/AvoidEdgesWeighting.java or this repository: https://github.com/karussell/graphhopper-traffic-data-integration – Karussell Nov 24 '16 at 09:45
  • Thank you sir @karussell, i examined the sample but unfortunately i cant figure out to combine the two codes. Sorry for being newbie. Is there any simpler sample that I can look at? Somewhat, showing a basic grapphopper map code, calling a weight class or the avoid edge inside the basic grapphoer sample code. Thank you in advance – Mellorine Nov 27 '16 at 13:33
  • See this very recent pull request https://github.com/graphhopper/graphhopper/pull/890 – Karussell Nov 30 '16 at 08:54

1 Answers1

3

So, I will share my code samples, which show how I implemented custom weighting, but they are similar to example you mentioned.

Firstly, you must extend class GraphHopper and override method createWeighting(WeightingMap weightingMap, FlagEncoder encoder).

public class MyGraphHopper extends GraphHopper {

    @Override
    public Weighting createWeighting(WeightingMap weightingMap, FlagEncoder encoder) {
        String weighting = weightingMap.getWeighting();
        if (Consts.CURRENT_TRAFFIC.equalsIgnoreCase(weighting)) {
            return new CurrentTrafficWeighting(encoder);
        } else {
            return super.createWeighting(weightingMap, encoder);
        }
    }

}

Then, you implement custom weighting, where, in your case, you implement logic of blocking some roads.

public class CurrentTrafficWeighting extends AbstractWeighting {

    protected final static double SPEED_CONV = 3.6;

    public CurrentTrafficWeighting(FlagEncoder encoder) {
        super(encoder);
        System.out.println("Current traffic weighting");
    }

    @Override
    public double getMinWeight(double distance) {
        return distance;
    }

    @Override
    public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
        double speed = reverse ? flagEncoder.getReverseSpeed(edgeState.getFlags()) : flagEncoder.getSpeed(edgeState.getFlags());
        if (speed == 0)
            return Double.POSITIVE_INFINITY;
        double time = edgeState.getDistance() / speed * SPEED_CONV;
        return time;
    }

    @Override
    public String getName() {
        return Consts.CURRENT_TRAFFIC;
    }

}

And now, maybe the most important part for you, which shows how to join these two pieces of code. When you create new request, you must set weighting to custom one (the one which you implemented). In this way, your custom weighting with some roads blocked will be used, while calculating optimal route.

public static void main(String[] args) {

    MyGraphHopper hopper = new MyGraphHopper();
    hopper.setOSMFile(OSM_FILE_PATH);

    hopper.setGraphHopperLocation(GRAPH_FOLDER);
    hopper.clean();
    hopper.setEncodingManager(new EncodingManager("car"));
    hopper.setCHEnable(false);
    hopper.importOrLoad();

    GHRequest req = new GHRequest(startPoint.getX(), startPoint.getY(), finishPoint.getX(), finishPoint.getY())
            .setWeighting(Consts.CURRENT_TRAFFIC)
            .setVehicle("car")
            .setLocale(Locale.US)
            .setAlgorithm(AlgorithmOptions.DIJKSTRA_BI);

    GHResponse rsp = hopper.route(req);

}
Łukasz Fijas
  • 113
  • 1
  • 8