Skip to content

Add support for user defined SpatialRules #1808

Closed

Description

@otbutz

As discussed in #1799 we'd like to move from SpatialRules defined in the code to external rule files which can be edited and extended by the user.

To sum up what we've gathered so far:

  • allow the user to define a directory with multiple geojson files describing the countries/states/areas he wants to manipulate when the graph is being built
  • the accompanying rule files(e.g written in YAML) reside in a second directory
    • a rule targets one or more (multi)polygons by selecting them according to their properties
    • rules should have readable names in order to show applied rules later in the PathDetails
    • the area of rules might overlap(e.g state rules overwrite country rules) or multiple rules might share the same polygon. Therefore a rule should have some kind of priority score
    • rules can be defined sparsely; if a rule has no value for a specific situation the next rule with a lower priority will be queried with a fallback/default rule being the last one in the chain

TODO:

  • load border polygons from multiple files
  • define a rule format
  • store the applied rules in the graph
    • StringIndex seems like a good idea but the long pointer is problematic. Maybe introduce a second layer of indirection? int id -> long pointer
  • extend the SpatialRuleLookup to handle overlapping rules
  • how do we treat overlapping rules with the same priority?

Related issues:

Activity

  1. otbutz commented on Nov 29, 2019

    @otbutz
    ContributorAuthor

    I put a bit of thought into #1427 and came to the conclusion that rule files are simply not able cover the full span of potential use cases. They'd have to define logical operations on arbitrary tags, deal with malformed values and tag priorities.

    This would lead us to one of the following scenarios:

    • abandon Make SpatialRule more flexible #1427 and limit the rules to "case x: value y" logic
    • hybrid approach: use external files for simple rules and allow to create more indepth ones via code
    • drop the rule files idea

    Otherwise we would end up reimplementing something like JsonLogic 🤕

    @karussell @boldtrn Your opinion on this?

  2. karussell commented on Nov 29, 2019

    @karussell
    Member

    I like the hybrid approach and we could start with the simple use cases that can be done in the files (like setting a certain EncodedValue like car.average_speed or car.access or max_speed to the same value for the defined area) and allow later hooking into this process.

    Thanks for the JsonLogic link, interesting.

    load border polygons from multiple files

    For better maintaining these rules we could have a separate folder with 2 files per SpatialRule like de.rule.json and de.border.json and then set this folder in the config.yml.

    store the applied rules in the graph

    When we have them in separate files and already have the parsing logic for that there is no real need for this (?) The only thing that we would probably need to store is in which order we have to read them but we could use the order from the config.yml file:

    spatial_rule_ids: de,fr,ru
    spatial_rule_location: /some/folder/to/jsons

    extend the SpatialRuleLookup to handle overlapping rules
    how do we treat overlapping rules with the same priority?

    Let's start small and move this into a separate issue.

  3. otbutz commented on Nov 29, 2019

    @otbutz
    ContributorAuthor

    I like the hybrid approach and we could start with the simple use cases that can be done in the files (like setting a certain EncodedValue like car.average_speed or car.access or max_speed to the same value for the defined area) and allow later hooking into this process.

    My favorite aswell.

    For better maintaining these rules we could have a separate folder with 2 files per SpatialRule like de.rule.json and de.border.json and then set this folder in the config.yml.

    I should've phrased that better: the rule file doesn't include any geojson content. I had the same concept in mind.

    When we have them in separate files and already have the parsing logic for that there is no real need for this (?)

    I can think of two valid use cases:

    • provide country information via PathDetails
    • Transparency: the rules can have a drastic effect on routing decisions so we should be able to check which ones got applied. This gets even more important if we add support for overlapping rules later. e.g the user defines a rule which is valid for a city and wants to display when its active via the details parameter.

    The only thing that we would probably need to store is in which order we have to read them but we could use the order from the config.yml file:

    spatial_rule_ids: de,fr,ru
    spatial_rule_location: /some/folder/to/jsons

    I like the approach 👍

    extend the SpatialRuleLookup to handle overlapping rules
    how do we treat overlapping rules with the same priority?

    Let's start small and move this into a separate issue.

    Sounds good.

  4. otbutz commented on Dec 19, 2019

    @otbutz
    ContributorAuthor

    @karussell I think i have a solution for the persistence problem of applied rules. We're currently storing the integer id of the applied rule as returned by SpatialRuleLookup#getSpatialId(SpatialRule) in the graph. We could keep this behavior for multiple rules but only store the id of the last rule which has been applied. Using the SpatialRuleLookup we could populate the PathDetails with the associated String id: load the encoded spatial id -> lookup the related SpatialRule -> return its String ID

  5. boldtrn commented on Jan 2, 2020

    @boldtrn
    Member

    I also agree that we should go with the hybrid approach. Especially for more complex scenarios one needs to be able to create a bit more complex logic, while most cases might work fine with a simple key:value concept in yaml.

    provide country information via PathDetails

    That's something that's already supported, isn't it? We "just" need to store the id of the country and then we can create PathDetails for it, or we could even retrieve the rules during the runtime from the SpatialRuleLookup, if it's fast enough.

    Transparency: the rules can have a drastic effect on routing decisions so we should be able to check which ones got applied. This gets even more important if we add support for overlapping rules later. e.g the user defines a rule which is valid for a city and wants to display when its active via the details parameter.

    Yes, I think this is an extremely important factor and probably only works if we look up the rules during the runtime for every edge?

    Storing multiple rules for one edge is a bit tricky. There are two scenarios that we need to think about: extracting spatial rules, after the route has been calculated and extracting spatial rules during the route calculation (e.g. in the weighting). If we want to do this in the weighting, our approach needs to be extremely fast, like a simple array lookup. The weighting methods might be called millions of times for one route calculation. If we only do this after the route has been calculated, performance is not that critical.

    Something I had thought about is to create artificial ids for multiple rules. For example if we have Europe (0) and Russia (1), edges that are inside of Europe and inside of Russia would get the id 2, Edges only in Europe get the id 0 and only in Russia get the id 1. Looking up these rules would be reasonably fast, but the number of combinations can easily explode, if you have too many overlapping rules. Also, after importing the graph, you would have to "remember" these ids, so probably we would have to make the SpatialRuleLookup a Storable, so that we save it with the graph and load it again.

  6. otbutz commented on Jan 2, 2020

    @otbutz
    ContributorAuthor

    or we could even retrieve the rules during the runtime from the SpatialRuleLookup, if it's fast enough.

    That's the solution i'd favor at the moment but this would require loading the SpatialRuleLookup every time GH starts. The approach in #1829 should be fast enough to be used when creating PathDetails:

    https://github.com/graphhopper/graphhopper/pull/1829/files#diff-04bf7aff41045bb7ff81ef554ce48181R117-R119

    Yes, I think this is an extremely important factor and probably only works if we look up the rules during the runtime for every edge?

    I kept thinking about it and maybe it's enough to just keep the ID of the rule which got applied last. It's easier to encode and we don't lose that much information. The overhead of loading the applicable rules during runtime is just too much.

    Something I had thought about is to create artificial ids for multiple rules. For example if we have Europe (0) and Russia (1), edges that are inside of Europe and inside of Russia would get the id 2, Edges only in Europe get the id 0 and only in Russia get the id 1. Looking up these rules would be reasonably fast, but the number of combinations can easily explode, if you have too many overlapping rules. Also, after importing the graph, you would have to "remember" these ids, so probably we would have to make the SpatialRuleLookup a Storable, so that we save it with the graph and load it again.

    If we only store the ID of the last rule we can stick to a simple encoded value. The lookup using JTS classes shouldn't need any splitting as its lookup is already envelope based which should yield only a small number of candidates which can be checked rather quickly.

  7. boldtrn commented on Jan 2, 2020

    @boldtrn
    Member

    I kept thinking about it and maybe it's enough to just keep the ID of the rule which got applied last. It's easier to encode and we don't lose that much information. The overhead of loading the applicable rules during runtime is just too much.

    But then we don't support multiple rules? This is how it works right now in the SpatialRuleLookup. We simply set the last ID that applies. IMHO, this is good enough for country rules, but if we start thinking about custom rules, I don't think it's enough. In a different project we have quite a lot of overlapping rules with different meanings, so our solution was to simply have multiple SpatialRuleLookups to avoid any overlapping. Looking up rules in the 2D aray of the SpatialRuleLookupArray is pretty fast, so multiple lookups are acceptable for us.

  8. otbutz commented on Jan 2, 2020

    @otbutz
    ContributorAuthor

    My approach would be to apply the rules for a certain spot according to the priority of the rules and store only the ID of the last one.

    eg: the following rules would apply to the location 48.11894,11.53815:

    • europe (SpatialId: 1, Priority: 90)
    • germany (SpatialId: 2, Priority: 100)
    • bavaria (SpatialId: 3, Priority: 110)

    The rules would be applied according to their priority and the spatial ID of the last one would be stored as an encoded value. (bavaria)

    We could still support a second details key to lookup all applied rules when building the Path but i fear that this is rather time consuming and only needed if the user has overlapping rules and wants to know the exact rule set which got applied.

  9. boldtrn commented on Jan 2, 2020

    @boldtrn
    Member

    Yes sure, priorities should work for many cases and would already be a nice improvement to the current situation. If we only need this for PathDetails, multiple rules are not that relevant anyway. If we want to use it for the routing itself, things get more trickier :). For hierarchical rules, like Europe, Germany, Bavaria, we could nest rules and lookup the "super rule", but we should start with the simple version first and then we can always improve on that if we need more :).

  10. otbutz commented on Jan 3, 2020

    @otbutz
    ContributorAuthor

    but we should start with the simple version first

    IMHO the priority approach is quite easy as i already gather all applicable rules for a point. Sorting and applying them in order doesn't add much complexity.

  11. otbutz commented on Jun 30, 2020

    @otbutz
    ContributorAuthor

    After talking with @karussell about the matter in #1799 (comment) i think it's a good idea to define which usecases we'd like to solve with the spatial rule system and agree on a definition of their intended scope.

    • providing fallback values
      e.g max speed fallback values for certain road types of a country
    • override existing values
      e.g ditch certain access limitations because the driver has a permission for the area
    • cap existing values
      e.g define upper/lower bounds as a workaround for mapping errors
  12. otbutz commented on Oct 29, 2021

    @otbutz
    ContributorAuthor

    Pretty much solved with #2353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions