You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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:
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:
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:
@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
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.
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:
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.
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.
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.
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 :).
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.
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
Activity
otbutz commentedon Nov 29, 2019
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:
Otherwise we would end up reimplementing something like JsonLogic 🤕
@karussell @boldtrn Your opinion on this?
karussell commentedon Nov 29, 2019
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.
For better maintaining these rules we could have a separate folder with 2 files per SpatialRule like
de.rule.jsonandde.border.jsonand then set this folder in the config.yml.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:
Let's start small and move this into a separate issue.
otbutz commentedon Nov 29, 2019
My favorite aswell.
I should've phrased that better: the rule file doesn't include any geojson content. I had the same concept in mind.
I can think of two valid use cases:
detailsparameter.I like the approach 👍
Sounds good.
otbutz commentedon Dec 19, 2019
@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 IDboldtrn commentedon Jan 2, 2020
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.
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.
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.
otbutz commentedon Jan 2, 2020
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
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.
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.
boldtrn commentedon Jan 2, 2020
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.
otbutz commentedon Jan 2, 2020
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: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.
boldtrn commentedon Jan 2, 2020
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 :).
otbutz commentedon Jan 3, 2020
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.
otbutz commentedon Jun 30, 2020
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.
e.g max speed fallback values for certain road types of a country
e.g ditch certain access limitations because the driver has a permission for the area
e.g define upper/lower bounds as a workaround for mapping errors
otbutz commentedon Oct 29, 2021
Pretty much solved with #2353