Repository navigation
Spatial Rule Lookup (e.g. Country Specific Rules) - #926
Conversation
|
Thanks - will have a look!
Maybe this is not too critical. How large is the 25mb file if compressed? As git and jar normally does compression and it will be big just on the disc and not in production. |
2.5mb. You can view the file here: http://bl.ocks.org/d/7280192619e0cc25f609dafe5c0de2cf I think the borders are not too bad, but we can definetly use the exact borders if this becomes an issue. |
|
|
||
| @Override | ||
| public GHPoint asPoint() { | ||
| return null; |
There was a problem hiding this comment.
Here and in asPointList we need to throw UnsupportedExc
|
|
||
| /** | ||
| * This interface is used to define an area or location being used to block or change areas in the graph. | ||
| * This interface is used to define an area or location in a geojson file. |
There was a problem hiding this comment.
This interface has nothing to do with a file. Maybe we just say define an area or location in a GeoJSON fashion?
| this.nonChMaxWaypointDistance = nonChMaxWaypointDistance; | ||
| } | ||
|
|
||
| public void setSpatialRuleLookup(SpatialRuleLookup spatialRuleLookup) { |
There was a problem hiding this comment.
Hmmh, I do not like this procedure much. We should not provide a setter here, if this is not an additional thing for GraphHopper (infact GraphHopper.spatialRuleLookup is unused).
Instead the user should/could do graphHopper.getEncodingManager().getEncoder("generic").setXY ... or maybe independent on generic and just graphHopper.getEncodingManager().setXY?
There was a problem hiding this comment.
Makes sense 👍 I think it makes sense to start only with generic for now. We can always extend that.
| accessValue = getEdgeValueForAccess(rule.isAccessible(way, "")); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Yes, the idea was to first check the ReaderWay if there is an access restriction value on it. If not check the country rules to eventually add a restriction.
| * | ||
| * @author Robin Boldt | ||
| */ | ||
| public class Polygon { |
There was a problem hiding this comment.
Added some, probably still needs more :)
|
|
||
| @Override | ||
| public String getType() { | ||
| return null; |
There was a problem hiding this comment.
Here we should return Polygon or MultiPolygon or create two implementations?
There was a problem hiding this comment.
Added Polygon for now. We definitely have to think about how we handle this in the future.
|
|
||
| The Geojson has roughly 25MB, so I reduced it with http://www.mapshaper.org/ using Douglas Peuckar at 10%. The resulting file is 2.4mb. | ||
|
|
||
| The rules are taken from the OSM Wiki: ttps://wiki.openstreetmap.org/wiki/OSM_tags_for_routing/Access-Restrictions No newline at end of file |
There was a problem hiding this comment.
Should be in the NOTICE.md instead (?)
And why is this and countries.geo.json in package com/graphhopper/http ?
There was a problem hiding this comment.
Should be in the NOTICE.md instead (?)
Yes thats correct. I wanted to fix this once we have a final location for the countries.geo.json.
And why is this and countries.geo.json in package com/graphhopper/http ?
Currently I load the countries.geo.json with SpatialRuleLookupBuilder.class.getResource("countries.geo.json") and SpatialRuleLookupBuilder is in com.graphhopper.http. I had it in the core first, but wasn't able to read the file in all cases (I think when running the tests with Maven).
| GraphHopper tmp = new GraphHopperOSM().forServer().init(args); | ||
| //TODO We should set the BBox and resolution | ||
| //TODO, move to a more appropriate place | ||
| tmp.setSpatialRuleLookup(SpatialRuleLookupBuilder.build()); |
There was a problem hiding this comment.
related to the TODOs :) ... it is suboptimal that it cannot be configured where to load the json from and the resolution ...
There was a problem hiding this comment.
Yes, I know :) I should fix this.
| GHJson ghJson = new GHJsonBuilder().create(); | ||
| JsonFeatureCollection jsonFeatureCollection = ghJson.fromJson(new FileReader(new File(SpatialRuleLookupBuilder.class.getResource("countries.geo.json").getFile())), JsonFeatureCollection.class); | ||
|
|
||
| // TODO find outer Border of all used features and create SpatialRuleLookupArray onlyo for these Bounds |
There was a problem hiding this comment.
What do you mean here?
(+minor spelling issues)
There was a problem hiding this comment.
The idea was to calculate a BBox for all SpatialRules. Initializing an Array for world wide coverage makes no sense If we only have a rule for a subset of the world (e.g., Germany and Austria). Would be quite easy to do and would save quite some storage.
| } | ||
| } | ||
| } catch (FileNotFoundException e) { | ||
| e.printStackTrace(); |
There was a problem hiding this comment.
please never catch exceptions and continue silently. Instead rethrow e.g. via new RuntimeException(e)
… feeding the spatialID
|
Have now pushed a change suggestion. BTW: The same config is now ensured via the spatial_rules number. The height&width etc config are similarly ensured already. |
|
|
||
| public DataFlagEncoder setSpatialRuleLookup(SpatialRuleLookup spatialRuleLookup) { | ||
| if (spatialRuleLookup.size() != spatialRules) | ||
| throw new IllegalArgumentException("You have to configure the encoder to accept the identical amount of rules " + |
There was a problem hiding this comment.
Should we get the number of spatial rules from the lookup then? But the lookup isn't available when constructing the encoder?
There was a problem hiding this comment.
Exactly that can be the problem
| "|store_weight=" + storeWeight + | ||
| "|store_width=" + storeWidth; | ||
| "|store_width=" + storeWidth + | ||
| "|spatial_rules=" + spatialRules; |
There was a problem hiding this comment.
Should we safe the order and name of the countries? Or would this be too much overhead?
There was a problem hiding this comment.
For now lets keep it simple and a bit unsafer until we have something better :)
| double getMaxSpeed(String highway, double _default); | ||
|
|
||
| AccessValue isAccessible(String highwayTag, String transportationMode, AccessValue _default); | ||
| AccessValue getAccessible(String highwayTag, String transportationMode, AccessValue _default); |
There was a problem hiding this comment.
getAccessValue or getAccessibility?
boldtrn
left a comment
There was a problem hiding this comment.
In general your changes look good to me. The only thing that bothers me is that we don't evaluate the SpatialRules in the GenericWeighting (or I missed it). So right now we safe the country but don't use the information?
| * | ||
| * @author Robin Boldt | ||
| */ | ||
| public interface SpatialRule { |
There was a problem hiding this comment.
After thinking about the SpatialRule a bit more I think we should make it definitely more generic. But I think we should do this in a future improvement. First merge this one :)
|
|
||
| /** | ||
| * Contains SpatialRules | ||
| * This class contains a collection of SpatialRule and is used for the implementation SpatialRuleLookupArray. |
There was a problem hiding this comment.
This class contains a collection of SpatialRules. It is used to define multiple rules per tile in the SpatialRuleLookupArray.
Not sure if this is better?
There was a problem hiding this comment.
Yes that might be better 👍 I think this also follows more the standard behavior of Java Collections.
| * The covered area is indexed as tiles, with every tile being "quadratic" having the same degree length on every side. | ||
| * <p> | ||
| * Currently one one rule per Tile is allowed. The later added rule will be added to the tile. | ||
| * Currently only a limited amount of rule combinations per tile are allowed. |
There was a problem hiding this comment.
Is this true? Obviously limited to 255, but you could put Integer.MAX_VALUE amount of rules per container and therefore per tile.
There was a problem hiding this comment.
It is not limited to the number of rules per container but to the number of rule combinations. E.g. A,B is one combination then A,B,D is another and so on. So maybe not that clear description?
There was a problem hiding this comment.
But is it really limited per tile? It is limited for the whole lookup, but not per tile.
There was a problem hiding this comment.
Ah, yes, it is the whole lookup
| SpatialRuleLookupArray(BBox bounds, double resolution, boolean exact) { | ||
| if (bounds == null) | ||
| throw new IllegalArgumentException("BBox cannot be null"); | ||
| if (resolution < 1e-100) |
There was a problem hiding this comment.
Is there a reason for this beside just generically limiting the resoultion. BTW: I don't think anyone will need a finer resolution, just asking :).
There was a problem hiding this comment.
The problem is resolution<=0 is not what we want and maybe we can raise this limit here a bit but not lower it IMO
There was a problem hiding this comment.
I am perfectly fine with the limit, I was just wondering why it is that number :).
There was a problem hiding this comment.
I just picked a very small one - maybe we can increase it with some assumption
There was a problem hiding this comment.
No, that's fine. Was just wondering.
|
|
||
| protected int getRuleIndex(int xIndex, int yIndex) { | ||
| protected int getRuleContainerIndex(int xIndex, int yIndex) { | ||
| if (xIndex < 0 || xIndex > lookupArray.length) { |
There was a problem hiding this comment.
Should we remove the lat lon checks in the lookupRule method in favor of this check. This might safe some overhead? This should be also very fast. I think we should do some performance measures here? But that is something we can consider later anyway. Create an issue for this?
There was a problem hiding this comment.
Well, usually ifs for boundary checks are very fast. For now I wouldn't care that much. But we could check the result (in lookupRule) for EMPTY_RULE_INDEX and return EMPTY instead of the call to ruleContainers etc?
There was a problem hiding this comment.
Yeah, let's just leave it as is. If we see any performance issues we can still tweak?
| lookupArray[i][j] = (byte) ruleIndex; | ||
| lookupArray[i][j] = (byte) ruleContainerIndex; | ||
| } else { | ||
| // Merge Rules |
There was a problem hiding this comment.
Should we extract the merge rules logic into a new method?
There was a problem hiding this comment.
Extracted already a bit ... it is ok for now IMO
There was a problem hiding this comment.
Yes I saw that. Should be fine.
| } | ||
|
|
||
| public void visualize(int stepSize) { | ||
| void sout(int stepSize) { |
There was a problem hiding this comment.
We can delete that one. I used it in the beginning to debug the lookup.
| GHJson ghJson = new GHJsonBuilder().create(); | ||
| JsonFeatureCollection jsonFeatureCollection = ghJson.fromJson(reader, JsonFeatureCollection.class); | ||
| return new SpatialRuleLookupBuilder().build( | ||
| Arrays.asList(new GermanySpatialRule(), new AustriaSpatialRule()), |
There was a problem hiding this comment.
Should we make the Countries configureable?
There was a problem hiding this comment.
We could do it with reflection? Maybe later?
| if (time == Long.MAX_VALUE) | ||
| return Double.POSITIVE_INFINITY; | ||
|
|
||
| int spatialId = gEncoder.getSpatialId(edgeState.getFlags()); |
There was a problem hiding this comment.
Did I missed it or is the SpatialIndex actually unused now? We safe the CountryId but don't evaluate it.
There was a problem hiding this comment.
This is true, yes. But we could apply speed and access changes when feeding here: https://github.com/boldtrn/graphhopper/blob/59b7e435f11628507cdbc254d27f7ceb3b31501d/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java#L408
I would avoid putting the index in the GenericWeighting ... too many things already there and as we could change speed and accessibility when feeding this is also not necessary for now
There was a problem hiding this comment.
Would you mind to add applying the speed and accessibility changes and recover may some tests that I removed? This seems indeed important to me, now that you pointed me to it
There was a problem hiding this comment.
Would you mind to add applying the speed and accessibility changes and recover may some tests that I removed? This seems indeed important to me, now that you pointed me to it
So should I put it back into the GenericWeighting? Or should we keep it in the DataFlagEncoder?
There was a problem hiding this comment.
Please for now without GenericWeighting. So like I linked: determine the country and apply a speed reduction if necessary (similar with the access).
There was a problem hiding this comment.
Would you mind to add applying the speed and accessibility changes and recover may some tests that I removed? This seems indeed important to me, now that you pointed me to it
Added these back to the DataFlagEncoder.
commented
Feb 9, 2017
|
(BTW: also please have a look at some comments we made at 'outdated' commits here in this PR :)) |
| maxSpeed = getSpatialRule(way).getMaxSpeed(way.getTag("highway", ""), maxSpeed); | ||
| } | ||
| double fwdSpeed = parseSpeed(way.getTag("maxspeed:forward")); | ||
| if (fwdSpeed < 0 || maxSpeed > 0 && maxSpeed < fwdSpeed) |
There was a problem hiding this comment.
Is fwdSpeed > maxSpeed an iillegal/unwanted state? From a mapping perspective it shouldn't matter, right?
commented
Feb 14, 2017
|
Ok, so I updated this PR. Are we good to merge it or is there anything we should add/change? |
| return false; | ||
| } | ||
|
|
||
| private SpatialRule getSpatialRule(ReaderWay way) { |
| assertEquals(index.getSpatialId(SpatialRule.EMPTY), encoder.getSpatialId(e2.getFlags())); | ||
|
|
||
| assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, encoder.getAccessValue(e1.getFlags())); | ||
| assertEquals(AccessValue.ACCESSIBLE, encoder.getAccessValue(e2.getFlags())); |
There was a problem hiding this comment.
Would you mind to add a further test where the max_speed value is overwritten by the one stored in the index?
| private EncodedValue highwayEncoder; | ||
| private EncodedValue transportModeEncoder; | ||
| private EncodedValue accessEncoder; | ||
| private static int NOT_ACCESSIBLE_KEY; |
There was a problem hiding this comment.
Here the static has to be removed. static is almost always code smell and/or dangerous/buggy (at least without final)
There was a problem hiding this comment.
Somehow I cannot comment on your comment below.
Can it be removed?
Yes, but it is used two times in the code. This would most probably lead to duplicated code. So I wouldn't want to do this, only if we have to?
There was a problem hiding this comment.
Yes, but it is used two times in the code. This would most probably lead to duplicated code. So I wouldn't want to do this, only if we have to?
Ok
| maxSpeed = getSpatialRule(way).getMaxSpeed(way.getTag("highway", ""), maxSpeed); | ||
| } | ||
| double fwdSpeed = parseSpeed(way.getTag("maxspeed:forward")); | ||
| if (fwdSpeed < 0 || maxSpeed > 0 && maxSpeed < fwdSpeed) |
There was a problem hiding this comment.
Hmmh, sorry. When I think more about this: removing this is incorrect. Assume maxspeed=60 and maxspeed:forward=50 should result in fwdSpeed==50 so what we probably need is fwdSpeed < 0 && maxSpeed > 0 - would you add another test? (Similar fix needed for bwdSpeed)
There was a problem hiding this comment.
Yes you are right. Added another test.
| return false; | ||
| } | ||
|
|
||
| private SpatialRule getSpatialRule(ReaderWay way) { |
| /** | ||
| * Get the unique name for this rule, e.g. the ISO name of the country. | ||
| */ | ||
| String getUniqueName(); |
There was a problem hiding this comment.
Yes, at first I did not want to call it id, but this should be easier to understand and also imply that it should be unique. But I think it makes sense to rename it.
| } | ||
| } | ||
|
|
||
| if (polygonBounds.minLon == Double.MAX_VALUE) { |
There was a problem hiding this comment.
here it is safer to use !polygonBounds.isValid() instead
| } | ||
| System.out.println(); | ||
| } | ||
| } |
There was a problem hiding this comment.
As you said this is no longer used/needed: would you mind to remove it?
| case "motorway": | ||
| return Integer.MAX_VALUE; | ||
| case "trunk": | ||
| return Integer.MAX_VALUE; |
There was a problem hiding this comment.
This is fine but should be probably explicitly mentioned in the javadocs of the interface?
commented
Feb 20, 2017
|
I updated this PR and added JavaDoc to the Interfaces. I realized that passing a string as TransportationMode is not a good idea and changed it to be an enum (sorry for the last minute change). I feel like it makes more sense like that. This enum could be used in the DataFlagEncoder to differ between different TransportationModes as well. |
commented
Feb 20, 2017
|
Nice - Thanks! |
| * This class represents a polygon that is defined by a set of points. | ||
| * Every point i is connected to point i-1 and i+1. | ||
| * <p> | ||
| * TODO: Howto design inner rings in the polygon? |
This PR intents to solve #175.
The basic data structure is an array, that separates the world (or an area) into tiles, with a given resolution. Each tile contains a reference to a set of rules.
Rules contain a set of polygons in which their rules are valid. Using an array has to disadvantages: (1) covering unused space, e.g. water and (2) inexact resoultion. Currently the default resolution about 20*20km at the equator (0.1 degree). We can achieve exact results when doing a Polygon contains check, which can be configured.
Right now I prepared two set of rules, Austria and Germany, but rules can be written for every Country. All countries are available in the countries.geo.json file. This file is based on the admin levels from NaturalEarthData taken from this repo under public domain. As the file would be 25mb, is reduced it to 10% using DP. Therefore the borders are sometimes slightly inaccurate (should not be too bad, otherwise let me know).
Lookup speed is not too bad. 10 000 000 lookups in an area that is filled approximately half by a polyong, takes between 3-4 seconds on my machine.
TODOs:
Storable<LocationIndex>. That way we could load it, once it was created (even though creation is quite fast). Also I am not sure how to do this (especially with the rule reference array, the lookup array should be easy)?Let me know what you think. Currently this is still work in progress, so any comments are more than welcome.