Custom spatial rules


I am quite new to Graphhopper. Recently I have been trying to implement my own spatial rules according to the documentation. Specifically I want to change the speed allowed on different types of roads for different countries.

I have created rules similar to [GermanySpatialRule], and extended [CountriesSpatialRuleFactory] to match the country code in question. I have also uncommented the line spatial_rules.location in config.yml, pointing at core/files/spatialrules/countries.geo.json.

However when I test it, the rules do not appear to take effect, although I do get “Created the SpatialRuleLookup with the following rules:”, and then the list of my rules when I compile.

As a test, I tried to modify the existing spatial rules for Germany and Austria and here again I saw no change in the routing.

What am I missing?

Any help is appreciated!

Are you able to provide your changes (or better for a smaller area like Berlin) as a unit test?

BTW: This would be nice to have in master too :slight_smile:

So far I’ve successfuly implemented spatial rules for ~10 countries. Here are the changes I had to do:

  • Create a new <country-name> file with a class extending DefaultSpatialRule (as with the german file example: core/src/main/java/com/graphhopper/routing/util/spatialrules/countries/<country-name>
  • Override getMaxSpeed method to set the correct speeds.
  • Override getId to return the 3 letter id of the country or whatever id you are using in your geojson file.
  • Add a case to the switch in graphhopper/core/src/main/java/com/graphhopper/routing/util/spatialrules/ with the country id.
  • Specify the spatial rule file with the spatial_rules.location field in config.yml

Now this will work for some profiles only, see Custom spatial rule with custom vehicule profile.
I wanted to get this to work with other profiles so I ended up creating my own custom profile that kind of merges the DataFlagEncoder with the CarFlagEncoder. This required patches to many files along the way, if you want more details let me know.

Hope this helps

Thanks for making this public.

As we intend to merge in the next days and after a few weeks probably also #1460 it is then very easy to add a country code globally (not tied to a specific FlagEncoder). There is already a PR adding lane support and other changes we have in our mind. A contribution for country specific speed values (based on #1460) would be really awesome!

Hi, thanks for replying,

Okay, so I assumed the spatial rules would override the profile-specific rules, but from what you say this is not the case.

Your idea sound promising though. I would appreciate more details on how this custom profile is created.

There isn’t much detail about this at the moment as this is not in master yet. Still there is this discussion: Flexible Vehicle Profiles