Repository navigation
Replace spatial rules by custom areas, include country borders in bundle and add Country EncodedValue - #2353
Conversation
|
Here: f5748e0 I added a proof of concept how we can transfer the current spatial rules to country rules. There could be one file per country and the rules could be used in the various tag parsers (like in the example OSMMaxSpeed, but of course also others. If someone wanted to do something similar for custom areas this could also be the way to go, up to the point that the equivalent to these |
| // todo: SpatialRuleLookupJTS uses SpatialRuleContainer#covers, but why? | ||
| .anyMatch(point::within)).collect(Collectors.toList()); |
There was a problem hiding this comment.
Maybe @otbutz can comment on this as there is some optimization going on there. The documentation says:
This predicate is similar to within(org.locationtech.jts.geom.Geometry), but is more inclusive (i.e. returns true for more cases).
There was a problem hiding this comment.
PreparedPolygon.intersects() should be preferred
There was a problem hiding this comment.
See my comment above. PreparedPolygon should be used instead of a regular JTS Polygon here as it's optimized for PIP checks.
There was a problem hiding this comment.
Ok, so I should just create a prepared polygon using the borders of each custom area? And what about this 'filled lines' code in SpatialRuleContainer? Is this another optimization?
There was a problem hiding this comment.
PreparedPolygon is the fastest method for full point-in-polygon checks. These are still costly though and the "filled lines" logic helps to avoid them. We basically rasterize the polygon and mark those rectangles which are completely within the polygon so we can skip PIP checks for them.
| for (CustomArea customArea : customAreas) { | ||
| Object countryCode = customArea.getProperties().get("ISO3166-1:alpha3"); | ||
| if (countryCode != null) { | ||
| NewCountry country = NewCountry.valueOf(countryCode.toString()); |
There was a problem hiding this comment.
Doesn't Enum.valueOf() yield an IllegalArgumentException if it can't find the value?
| NewCountry country = NewCountry.valueOf(countryCode.toString()); | |
| NewCountry country = NewCountry.find(countryCode.toString()); |
There was a problem hiding this comment.
Yes, it does. For country this should be ok, because I will move the countries.geojson into the source bundle and the NewCountry enum should then always be in sync with the country codes used in this file. But otherwise I agree it would be more useful to return null or something like a 'missing' value in case some value is missing (like road_class=my_404_class).
|
|
||
| private final String name; | ||
|
|
||
| NewCountry(String name) { |
There was a problem hiding this comment.
Can we also include the two letter country code?
There was a problem hiding this comment.
So far I used ISO3166-1:alpha3 as country code, but no idea honestly. What are the pros and cons of the different codes? If there is some use-case for it we can of course also include other fields.
There was a problem hiding this comment.
Depends on the data. Our geojson uses alpha3 and some tags in OSM like e.g maxspeed=RU:urban reference the alpha2 code. I doesn't really hurt to have both representations.
There was a problem hiding this comment.
Ok I see. So should we use alpha3 for the enum constants and add alpha2 as additional field, just like name? Or maybe alpha3 as enum constant, but also as additional field?
Btw the countries properties currently contain all these fields:
"id": -2186665,
"admin_level": "2",
"name": "Tonga",
"local_name": "Tonga",
"name_en": "Tonga",
"flag": "http://upload.wikimedia.org/wikipedia/commons/9/9a/Flag_of_Tonga.svg",
"name:af": "Tonga",
"name:am": "ቶንጋ",
"name:an": "Tonga",
"name:ar": "تونجا",
"name:az": "Tonqa",
"name:ba": "Тонга",
"name:be": "Тонга",
"name:bg": "Тонга",
"name:bn": "টোঙ্গা",
"name:bo": "ཊོང་ག",
"name:br": "Tonga",
"name:bs": "Tonga",
"name:ca": "Tonga",
"name:ce": "Тонга",
"name:cs": "Tonga",
"name:cu": "Тонга",
"name:cy": "Tonga",
"name:da": "Tonga",
"name:de": "Tonga",
"name:dv": "ޓޮންގާ",
"name:el": "Τόνγκα",
"name:en": "Tonga",
"name:eo": "Tongo",
"name:es": "Tonga",
"name:et": "Tonga",
"name:eu": "Tonga",
"name:fa": "تونگا",
"name:fi": "Tonga",
"name:fo": "Tonga",
"name:fr": "Tonga",
"name:fy": "Tonga",
"name:ga": "Tonga",
"name:gd": "Tonga",
"name:gl": "Tonga",
"name:gv": "Yn Tongey",
"name:he": "טונגה",
"name:hi": "टोंगा",
"name:hr": "Tonga",
"name:ht": "Tonga",
"name:hu": "Tonga",
"name:hy": "Տոնգա",
"name:ia": "Tonga",
"name:id": "Tonga",
"name:io": "Tonga",
"name:is": "Tonga",
"name:it": "Tonga",
"name:ja": "トンガ",
"name:jv": "Tonga",
"name:ka": "ტონგა",
"name:kk": "Тонга",
"name:kn": "ಟೋಂಗಾ",
"name:ko": "통가",
"name:ku": "Tonga",
"name:kw": "Tonga",
"name:la": "Tonga",
"name:lb": "Tonga",
"name:li": "Tonga",
"name:lt": "Tonga",
"name:lv": "Tonga",
"name:mk": "Тонга",
"name:ml": "ടോങ്ക",
"name:mn": "Тонга",
"name:mr": "टोंगा",
"name:ms": "Tonga",
"name:mt": "Tonga",
"name:my": "တုံဂါနိုင်ငံ",
"name:nl": "Tonga",
"name:nn": "Tonga",
"name:no": "Tonga",
"name:nv": "Tʼónga",
"name:oc": "Tònga",
"name:or": "ଟୋଙ୍ଗା",
"name:os": "Тонгæ",
"name:pa": "ਟੋਂਗਾ",
"name:pl": "Tonga",
"name:pt": "Tonga",
"name:qu": "Tunqa",
"name:rm": "Tonga",
"name:ro": "Tonga",
"name:ru": "Тонга",
"name:rw": "Tonga",
"name:sa": "टोंगा",
"name:se": "Tonga",
"name:sh": "Tonga",
"name:sk": "Tonga",
"name:sl": "Tonga",
"name:sm": "Toga",
"name:sq": "Tonga",
"name:sr": "Тонга",
"name:su": "Tonga",
"name:sv": "Tonga",
"name:sw": "Tonga",
"name:ta": "தொங்கா",
"name:tg": "Тонга",
"name:th": "ประเทศตองกา",
"name:tl": "Tonga",
"name:to": "Tonga",
"name:tr": "Tonga",
"name:ts": "Tonga",
"name:tw": "Tonga",
"name:ty": "To’a",
"name:ug": "Ton’ga",
"name:uk": "Тонга",
"name:ur": "ٹونگا",
"name:uz": "Tonga",
"name:vi": "Tonga",
"name:vo": "Tonguäns",
"name:wo": "Tonga",
"name:yo": "Tóngà",
"name:zh": "東加",
"boundary": "administrative",
"maritime": "yes",
"name:ace": "Tonga",
"name:als": "Tonga",
"name:arz": "تونجا",
"name:ast": "Tonga",
"name:bcl": "Tonga",
"name:bjn": "Tonga",
"name:bpy": "টঙ্গা",
"name:bxr": "Тонга",
"name:ceb": "Tonga",
"name:chy": "Tonga",
"name:ckb": "تۆنگا",
"name:crh": "Tonga",
"name:diq": "Tonga",
"name:ext": "Tonga",
"name:frp": "Tonga",
"name:hak": "Tûng-kâ",
"name:hif": "Tonga",
"name:ilo": "Tonga",
"name:lij": "Tonga",
"name:lmo": "Tonga",
"name:mrj": "Тонга",
"name:nah": "Tonga",
"name:nan": "Tonga",
"name:nds": "Tonga",
"name:pam": "Tonga",
"name:pih": "Tonga",
"name:pms": "Tònga",
"name:pnb": "ٹونگا",
"name:sah": "Тонга",
"name:scn": "Tonga",
"name:sco": "Tonga",
"name:smn": "Tonga",
"name:sms": "Tonga",
"name:szl": "Tůnga",
"name:tpi": "Tonga",
"name:tzl": "Tonga",
"name:war": "Tonga",
"name:xal": "Тонһлмудин Нутг",
"name:yue": "湯加",
"timezone": "Pacific/Tongatapu",
"way_area": "3.34815",
"wikidata": "Q678",
"ISO3166-1": "TO",
"wikipedia": "en:Tonga",
"name:simple": "Tonga",
"driving_side": "left",
"name:bat-smg": "Tuonga",
"name:fiu-vro": "Tonga",
"name:be-tarask": "Тонга",
"is_in:continent": "Oceania",
"ISO3166-1:alpha2": "TO",
"ISO3166-1:alpha3": "TON",
"official_name:cs": "Království Tonga",
"official_name:de": "Königreich Tonga",
"official_name:en": "Kingdom of Tonga",
"official_name:es": "Reino de Tonga",
"official_name:ru": "Королевство Тонга",
"ISO3166-1:numeric": "776"
}
Since we want to move this into the bundle we should probably get rid of what we do not use (just in case this actually reduces the filesize).
There was a problem hiding this comment.
nv-i18n handles this in a similar way: https://github.com/TakahikoKawasaki/nv-i18n/blob/master/src/main/java/com/neovisionaries/i18n/CountryCode.java
One code representation as the name of the constant and the other one as a regular value.
There was a problem hiding this comment.
No, it's in core/files/spatialrules currently.
Ah, ok. Thanks. Strange, always thought we have it in the bundle already.
So should we use alpha3 for the enum constants and add alpha2 as additional field, just like name?
This sounds good to me 👍
driving_side
This is indeed interesting but not sure if we should attach it to the enum or if it belongs to the country rule.
There was a problem hiding this comment.
Ok, sure. Russia actually does not even have a timezone field (probably for this reason).
There was a problem hiding this comment.
Should we keep alpha2, alpha3 name:en, ISO3166-1:numeric, flag and wikidata in countries.geojson and remove everything else? The last three wouldn't be used by default but I thought they might be useful to somebody?
| RoadClass roadClass = RoadClass.find(way.getTag("highway", "")); | ||
| CountryRule countryRule = way.getTag("country_rule", null); | ||
| if (countryRule != null) | ||
| maxSpeed = countryRule.getMaxSpeed(roadClass, TransportationMode.CAR, maxSpeed); |
There was a problem hiding this comment.
As noted in #2246 (comment) we should probably pass this down to the value extractor.
| gf = new GeometryFactory(); | ||
| index = new STRtree(); | ||
| for (CustomArea customArea : customAreas) { | ||
| for (Polygon border : customArea.getBorders()) { |
There was a problem hiding this comment.
We should use PreparedPolygon which is a lot faster for point-in-polygon checks
There was a problem hiding this comment.
I tried this and you are right it is much faster. I did a test with 100k random queries in Europe against an area index that uses the countries file and this took around 18s, but only like 0.2s when using prepared geometry 👍
However, the filled lines optimization does not seem to make the queries faster and sometimes it is even slower. Therefore I would rather not include this, or is this especially useful for some corner cases (maybe smaller areas?)
There was a problem hiding this comment.
My test code is here if you want to try yourself: 436821e
There was a problem hiding this comment.
However, the filled lines optimization does not seem to make the queries faster and sometimes it is even slower. Therefore I would rather not include this, or is this especially useful for some corner cases (maybe smaller areas?)
Interesting. If it's not really faster, i'd agree that we should remove it.
There was a problem hiding this comment.
In my tests it did not seem to be significant, maybe like 10%, but not sure to be honest. I'll remove it for now. The custom area index should not be slow, but I'm also not sure if it has to be as fast as possible, because it will only be used during import where other things might be the bottleneck anyway.
| // todo: SpatialRuleLookupJTS uses SpatialRuleContainer#covers, but why? | ||
| .anyMatch(point::within)).collect(Collectors.toList()); |
There was a problem hiding this comment.
PreparedPolygon.intersects() should be preferred
|
@otbutz Thanks for your review, I'll take a closer look at your suggestions. But from what I understand you like/agree with the general idea? |
|
Absolutely 😉 |
It does not seem to be significantly faster
The STRtree is usually doing a good job keeping the number of PIP checks low. I'd expected performance only to degrade if a lot of overlapping areas are involved and/or if the polygons have a high resolution.
We need to take a closer look on our rules and their impact on routing in general. See #2266 One of the problems is that we would need to differentiate between optimistic(upper bound) and pessimistic(lower bound) fallback values. IMHO this is in the same vein as distinguishing the TransportationMode. We need to find a balance between too much detail and oversimplification. Talking about simplfication: did you remove the other properties from the countries.geojson? Could you take a look on memory/storage consumption? I fear that the large enum comes with a price tag. |
Yes, sure, and hopefully this PR will be the foundation for this. I disabled the country rules by default in this PR and we can improve the rules, fix CH preparation performance and enable the rules by default afterwards.
Yes, see here: #2353 (comment)
In which context? Do you mean the Country EV? The Country enum has 218 entries, so it should take one byte per edge (8bits, 2^8=256). For a global import with currently around 250mio edges this would mean |
|
250MB sounds very reasonable |
Have you tested why this happens? I haven't had a chance to look at the code changes yet (sorry :( - summer is always pretty busy). My understanding from reading your proposal a few weeks ago:
Is the current approach different from this? |
Yes, it is because witness path searches that include destination edges take very long because of the large weight of these edges.
Yes, for every way we find the matching country rule (and all other custom areas) and this information is forwarded to all tag parsers. Currently only the max speed and road access parsers (and
After the import (when the encoded values are set) the country rules are never applied again. Like I said, CH preparation is still affected, because of the increased weights of the access=destination edges. For Germany I measured a x3 slow down for both node- and edge-based preparation. I am working on a fix for this. |
|
Is everybody happy? I will merge this then. |
|
Just one last thought: do we want a default country rule? I'd really like to get rid of hardcoded fallback values in FlagEncoders in the long run: I'd avoid any logic for now and just go for a NOOP implementation first. The reason why i bring this up now is that this would allow us to remove a bit of null handling. We would always have a CountryRule which should be applied. |
Yes, the thing we need to do here is that instead of setting average speed and access in
I really don't mind the null handling here. For some countries there is no rule, and the most direct way to state this in code is that the rule is null (=missing) in this case. For country rule it could kind of work, because so far all |
Btw in principle with the current code we can already make use of the country information in |
|
LGTM then :) |
Cool, and thanks a lot for your initial work on this and all your help with this PR. |
Ah thanks yes that makes sense. I thought there would be direct slow down because of too many lookups. |
|
@easbar any updates on the CH slowdown? Could we enable countryrules by default now? |
|
No, but I hopefully will fix this soon and I would rather wait for this before we enable country rules by default. And maybe when we enable it it would be nice to have a few more (maybe using the approach we discussed in #2392 ?) |

Here I tried to demonstrate some of the ideas described in this comment: #2246 (comment)
I haven't really removed any code yet and rather added custom areas the way I think they should be handled. Especially this commit: d3b037b shows how we can use custom areas. I have added a country encoded value that only requires a few changes and can immediately be used with custom models / auto-complete in the custom model box and path details. To try this yourself just add
to
config.ymland addnew_countrytograph.encoded_values.Like I said, I would rather include
countries.geojsonin the main bundle, but this just demonstrates how we could use arbitrary geojson files. I also think we can use the current spatial rules the same way: We just add geojson to thecustom_areas_folderand adjust the maxspeed parser or whatever parser we want to make area specific.Update:
This PR replaces spatial rules with 'custom areas'. This means that we can read arbitrary geojson polygons during import which are then made available to the tag parsers and flag encoders. This allows setting location-dependent encoded values, for example we could draw a Polygon around Paris and do special handling of the max speed encoded value just for this area.
This PR also adds the
countries.geojsonfile to the core bundle and uses above mechanism to provide the country for each edge. There is a newCountryParserwhich sets the country encoded value for every edge and the country is also passed to so calledCountryRules.CountryRulesprovide an easy way to apply country-specific rules for e.g.RoadAccessorMaxSpeed.