How to avoid toll roads and ferries with GH 3.0?

Hello,

I’m trying to set up a server configuration to avoid toll roads and ferries. I’m not using the yml-based configuration.

I understood from previous posts how to avoid toll roads but with the May’21 version (3.0) everything changed :confused:

Anyone has a working example of this…?

Thanks,
JM.

We collected many examples in this blog post. To exclude ferries you do:

{
 "priority": [
  {
   "if": "road_environment == FERRY",
   "multiply_by": 0.0
  }
 ]
}

And to exclude toll roads you add it either in the condition:

{
 "priority": [
  {
   "if": "road_environment == FERRY || toll != NO",
   "multiply_by": 0.0
  }
 ]
}

or as a second statement:

{
 "priority": [
  {
   "if": "road_environment == FERRY",
   "multiply_by": 0.0
  },{
   "if": "toll != NO",
   "multiply_by": 0.0
  }
 ]
}

Depending on your needs. You can always use GraphHopper Maps that includes auto completion and an export button to share these models (click the “custom” button to see the custom model editor).

Thank you so much for taking the time to assist me @karussell!

I’m invoking from Java and I was following this post Avoiding road classes and environments - #14 by diego-peteiro

I had read the post you mention before but I think my main issue is on how to use those JSON configurations from Java. Also, it seems like I can’t use the init() method with the params to load the toll road information and at the same time set a CustomProfile with the Statement expressions… :frowning:

This is where I’m stuck :confused:

Any help with an update of the Java codein this blog post would be deeply appreciated… Avoiding road classes and environments - #14 by diego-peteiro

Yes, you should either use GraphHopper#init or the different setters of the GraphHopper class, but not both. You can set profiles both ways though: Both GraphHopperConfig and GraphHopper offer a setProfiles method.

Anyway, here is some code that sets up a server-side profile that avoids ferries and toll roads using GraphHopperConfig and GraphHopper#init:

        GraphHopperConfig config = new GraphHopperConfig()
                .putObject("datareader.file", "your-country.osm.pbf")
                .putObject("graph.location", "your-country-gh")
                .putObject("graph.encoded_values", "road_class,road_class_link,road_environment,max_speed,road_access,toll")
                .setProfiles(Arrays.asList(
                        new CustomProfile("car_no_toll").setVehicle("car").setCustomModel(
                                new CustomModel()
                                        .addToPriority(If("toll != NO", MULTIPLY, 0))
                                        .addToPriority(If("road_environment == FERRY", MULTIPLY, 0))
                        )
                ))
                // optional (allows much faster routing)
                .setCHProfiles(Arrays.asList(new CHProfile("car_no_toll")));
        GraphHopper hopper = new GraphHopper();
        hopper.init(config);
        hopper.importOrLoad();
        GHRequest request = new GHRequest(45, 10, 46, 11);
        request.setProfile("car_no_toll");
        GHResponse response = hopper.route(request);
2 Likes

Thank you so much! :raised_hands:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.