Recomended Way to Extend Graphhopper

What would be the recommended way to extend GraphHopper? I.e. add a new FlagEncoder with a custom Weighting class.

I’ve been looking at replicating the structure from OpenRailRouting, but I think that is too much for just adding a new profile.

I am currently using the custom FlagEncoder and Weighting with:

public class RoutingExample {
    public static void main(String[] args) {
        GraphHopper hopper = new CustomHopper().forServer();
        hopper.setCHEnabled(false);
        hopper.setDataReaderFile("data/austria-latest.osm.pbf");
        hopper.setGraphHopperLocation("data/austria-gh");
        
        FlagEncoder encoder = new CustomFlagEncoder();
        hopper.setEncodingManager(EncodingManager.create(encoder));
        
        hopper.importOrLoad();
        
        GHRequest req = new GHRequest(47.811195, 13.033229, 48.210033, 16.363449).
                setWeighting("custom_weighting").
                setVehicle("custom_encoder").
                setLocale(Locale.US);
        
        GHResponse rsp = hopper.route(req);
        System.out.println(rsp);
    }
}

and with the graphhopper-core and graphhopper-reader-osm maven repositories in the pom file. But I would like to also use the server as an API with the new routing profile. I was thinking to either include Graphhopper as a git submodule or add the appropriate maven repositories (graphhopper-web maybe?)

What would you recommend? Is there an example for this? I am happy to create a simple hello world template as a starting point if there is nothing yet in this direction.

For example you can just implement it in a spring boot application where you get an api call to some endpoint and so you can call graphhopper the way you want it (for example with custom weighting and the flagEncoder you want to use.

Is there a way to use the Graphhopper Web API with a custom routing profile instead of rolling my own? I am not to fresh on Maven, but can I use the graphhopper-web or graphhopper-web-bundle repository maybe?

As an example see our map matching module: https://github.com/graphhopper/map-matching/blob/master/matching-web/src/main/java/com/graphhopper/matching/http/MapMatchingApplication.java

1 Like

I managed to create a minimal working example: minimal-graphhopper I hope it is a useful starting point. Would you add something to that or have some things that could be improved in the structure of the code?

Also, I assume that create-new-flagencoder.md needs to be updated since as far as I understood I needed to update DefaultFlagEncoderFactory instead of adding the new FlagEncoder to EncodingManager.parseEncoderString.

The API works now fine, but I experience two issues when running the routing in the browser:

  1. If I select two points, the routing works. But if I first press on one of the encoders I get No area description found and I cannot select points until I reload the page back to localhost:8989. Am I missing something?
    image

  2. Is there a simple way in my minimal example for the new FlagEncoder to have its own symbol (or reuses another symbol) in the browser?

Would you have time to write this down in a pull request?

But if I first press on one of the encoders I get No area description found

I think this is just a bug in the UI. I.e. you need to route first and then change the vehicle. But not sure.

Is there a simple way in my minimal example for the new FlagEncoder

Yes, you can create an icon <encoder_name>.png and put it here: https://github.com/graphhopper/graphhopper/tree/master/web/src/main/resources/assets/img

Yes, I can add an update to the documentation. I think it would be a good idea to link to the minimal-graphhopper repository as an example for people getting started. What do you think? I guess you can also copy this repository to the graphhopper repo as a Hello World example for people getting started.

A minimal graphhopper repo with all the different facets (simple hello world, location index, low level API) would be an interesting idea, especially since we could link from the documentation to real code instead of fixing the code in the docs. Would also improve this issue

Sounds good! Yes, I also think a minimal working example might help people get started. I can help add some things if they are missing. Also feel free to copy the project to the graphhopper repository

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