Skip to content

Commit 80fae44

Browse files
Famlamfrodrigo
authored andcommitted
Make NSI country filter uniform
- Let all plugins use `nsi_rule_applies` to evaluate the locationSet - Allow country codes with subcodes (like NL-GE)
1 parent 63d85f5 commit 80fae44

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

plugins/Name_Script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def init(self, logger):
120120

121121
self.whitelist_names = set()
122122
if country:
123-
self.whitelist_names = whitelist_from_nsi(country[:2].lower())
123+
self.whitelist_names = whitelist_from_nsi(country)
124124

125125
def node(self, data, tags):
126126
err = []

plugins/Name_UpperCase.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ def init(self, logger):
4949
self.country = None
5050

5151
if "country" in self.father.config.options:
52-
self.country = self.father.config.options.get("country")[:2]
53-
self.whitelist = set(UpperCase_WhiteList.get(self.country, []))
52+
self.country = self.father.config.options.get("country")
53+
self.whitelist = set(UpperCase_WhiteList.get(self.country.split("-")[0], []))
5454
nsi_whitelist = set(filter(lambda name: self.UpperTitleCase.match(name) and not self.RomanNumber.match(name),
55-
whitelist_from_nsi(self.country.lower())))
55+
whitelist_from_nsi(self.country)))
5656
self.whitelist.update(nsi_whitelist)
5757
else:
5858
self.whitelist = set()
@@ -61,7 +61,7 @@ def node(self, data, tags):
6161
err = []
6262
if "name" in tags:
6363
# Whitelist bus stops in Greece, see #2368
64-
if self.country and self.country == "GR" and "public_transport" in tags and tags["public_transport"] in ("stop_position", "platform", "station"):
64+
if self.country and self.country.split("-")[0] == "GR" and "public_transport" in tags and tags["public_transport"] in ("stop_position", "platform", "station"):
6565
return err
6666

6767
# first check if the name *might* match

plugins/TagFix_Brand.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from modules.OsmoseTranslation import T_
2323
from plugins.Plugin import TestPluginCommon
2424
from plugins.Plugin import Plugin
25-
from plugins.modules.name_suggestion_index import download_nsi
25+
from plugins.modules.name_suggestion_index import download_nsi, nsi_rule_applies
2626

2727
class TagFix_Brand(Plugin):
2828

@@ -48,7 +48,7 @@ def init(self, logger):
4848

4949
if not self.father.config.options.get("country"):
5050
return False
51-
self.country_code = self.father.config.options.get("country").split("-")[0].lower()
51+
self.country_code = self.father.config.options.get("country")
5252

5353
nsi = download_nsi()
5454
self.brands_from_nsi = self._parse_category_from_nsi(nsi, "brands/", "brand")
@@ -60,13 +60,8 @@ def _parse_category_from_nsi(self, nsi, nsiprefix, key):
6060
if tag.startswith(nsiprefix) and "items" in details:
6161
nsi_name = tag[len(nsiprefix):]
6262
for preset in details["items"]:
63-
if "locationSet" in preset:
64-
if ("include" in preset["locationSet"] and
65-
self.country_code not in preset["locationSet"]["include"] and
66-
"001" not in preset["locationSet"]["include"]):
67-
continue
68-
if "exclude" in preset["locationSet"] and self.country_code in preset["locationSet"]["exclude"]:
69-
continue
63+
if "locationSet" in preset and not nsi_rule_applies(preset["locationSet"], self.country_code):
64+
continue
7065
if "matchTags" in preset:
7166
for additional_tag in preset["matchTags"]:
7267
nsi_key = "{}|{}".format(additional_tag, preset["tags"][key])

plugins/modules/name_suggestion_index.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ def download_nsi():
3535
results = json.loads(json_str)
3636
return results['nsi']
3737

38+
39+
def nsi_rule_applies(locationSet, country):
40+
if not "include" in locationSet and not "exclude" in locationSet:
41+
return True
42+
# For extract with country="AB-CD-EF", check "AB-CD-EF", then "AB-CD", then "AB", then worldwide ("001")
43+
for c in ['-'.join(country.lower().split("-")[:i]) for i in range(country.count("-")+1, 0, -1)]:
44+
if "exclude" in locationSet and c in locationSet["exclude"]:
45+
return False
46+
if "include" in locationSet and c in locationSet["include"]:
47+
return True
48+
return not "include" in locationSet or "001" in locationSet["include"]
49+
50+
3851
# Gets all valid (shop, amenity, ...) names that exist within a certain country
3952
# country: the lowercase 2-letter country code of the country of interest
4053
# nsi: the parsed NSI database obtained from download_nsi()
@@ -44,13 +57,8 @@ def whitelist_from_nsi(country, nsi = download_nsi(), nsiprefix = 'brands/'):
4457
for tag, details in nsi.items():
4558
if tag.startswith(nsiprefix) and "items" in details:
4659
for preset in details["items"]:
47-
if "locationSet" in preset:
48-
if ("include" in preset["locationSet"] and
49-
country not in preset["locationSet"]["include"] and
50-
"001" not in preset["locationSet"]["include"]): # 001 = worldwide
51-
continue
52-
if "exclude" in preset["locationSet"] and country in preset["locationSet"]["exclude"]:
53-
continue
60+
if "locationSet" in preset and not nsi_rule_applies(preset["locationSet"], country):
61+
continue
5462
if "name" in preset["tags"]:
5563
whitelist.add(preset["tags"]["name"])
5664
whitelist.add(preset["displayName"])

0 commit comments

Comments
 (0)