Mellorine
Mellorine

Reputation: 285

Block certain road in graphhopper

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.

Upvotes: 2

Views: 639

Answers (1)

Łukasz Fijas
Łukasz Fijas

Reputation: 133

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);

}

Upvotes: 3

Related Questions