How to use Graphhopper routing requests with Profile in GH 1.0

Graphhopper routing with (multiple) profiles

Hallo Graphhopper community,

I have a little story to tell about GH routing and hopefully can give some advise.

I’m a student working for an university project that simulates human movement. For routing we used OSRM but now switched to GH because it allows better integration on local machines, supports multiple routing profiles at once and is faster.

Previously we used just foot routing but now we need multiple routing profiles. Graphhopper 1.0 will introduce some breaking changes and we want to be ready. Previously we did:


hopper = new GraphHopperOSM().forServer();

hopper.setDataReaderFile(osmPath);

hopper.setGraphHopperLocation(GH_PATH);

hopper.setEncodingManager(EncodingManager.create(new FootFlagEncoder()));

hopper.importOrLoad();

GHRequest req = new GHRequest(src.lat(), src.lon(), dst.lat(), dst.lon()).

                setWeighting(this.metric).

                setVehicle(this.vehicle);

This does no longer work due to the removal of .setVehicle() and .setWeighting()!

Without an up to date docs it is kind of hard to figure out the new way (at least for beginners). But thanks to one of the developers I figured it out. He asked me to share this to help others.

No we can do:


hopper = new GraphHopperOSM().forServer();

hopper.setDataReaderFile(osmPath);

hopper.setGraphHopperLocation(GH_PATH);

hopper.setEncodingManager(EncodingManager.create(new FootFlagEncoder(), new CarFlagEncoder));

hopper.importOrLoad();

hopper.setProfiles(

                new ProfileConfig("my_foot_profile").setVehicle("foot").setWeighting("fastest"),

                new ProfileConfig("my_car_profile").setVehicle("car").setWeighting("fastest")

                );

GHRequest req1 = new GHRequest(src.lat(), src.lon(), dst.lat(), dst.lon()).setProfile("my_foot_profile")

GHRequest req2 = new GHRequest(src.lat(), src.lon(), dst.lat(), dst.lon()).setProfile("my_car_profile")

You just have to initially set the Encoders you want, set the profiles for the graph and than can use whatever profile you want while doing a request.

NOTE: While in GH1.0-pre39 it is called ProfileConfig in the final release it will be called Profile

While the final docs are not yet there here is an updated guide on the GH-GitHub docs

Hopefully this was helpful

VG
Henne

2 Likes

Cool, thanks :slight_smile: And glad you got it up and running.

Yes, exactly :slight_smile:

1 Like
Powered by Discourse