forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagFix_Housenumber.py
254 lines (208 loc) · 10.7 KB
/
TagFix_Housenumber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Frédéric Rodrigo 2014-2016 ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
import re
from modules.OsmoseTranslation import T_
from plugins.Plugin import Plugin
class TagFix_Housenumber(Plugin):
not_for = ("RU", "BG")
def init(self, logger):
Plugin.init(self, logger)
self.errors[10] = self.def_class(item = 2060, level = 3, tags = ['addr', 'fix:survey'],
title = T_('Invalid addr:housenumber value'))
self.errors[14] = self.def_class(item = 2060, level = 3, tags = ['addr', 'fix:chair'],
title = T_('Invalid tag on interpolation way'),
detail = T_(
'''Interpolation way only valid with addr:interpolation=* and optional
addr:inclusion=*.'''))
self.errors[15] = self.def_class(item = 2060, level = 3, tags = ['addr', 'fix:chair'],
title = T_('Invalid addr:interpolation or addr:inclusion value'),
detail = T_(
'''* Tag `addr:interpolation=*` is only valid with values: `even`, `odd`,
`all`, `alphabetic` and Number.
* Tag `addr:inclusion=*` is only valid with values: `actual`, `estimate`
and `potential`.'''))
country = None
if self.father.config.options.get("country"):
country = self.father.config.options.get("country")[0:2]
# More specific rules by country
if country == 'CH':
# From open data from cantons Zurich and Bern. See also https://github.com/ltog/osmi-addresses/issues/93
# Plus allows commas with multiple numbers
ch_number = "[1-9][0-9]{0,3}( ?[a-zA-Z])?"
self.housenumber = re.compile(r"^(:?{0})(:?,{0})?$".format(ch_number))
elif country == 'CZ':
# https://wiki.openstreetmap.org/wiki/Cs:WikiProject_Czech_Republic/Address_system
self.housenumber = re.compile(r"^(ev\.)?[1-9]")
elif country == 'LU':
# From open data from CACLR, https://data.public.lu/en/datasets/registre-national-des-localites-et-des-rues/
self.housenumber = re.compile(r"^[1-9][0-9]{0,3}([A-Z]){0,3}(-[1-9][0-9]{0,3}([A-Z]){0,3})?$")
elif country == 'IT':
# Allow "snc" (Senza numero civico) in Italy
self.housenumber = re.compile(r"(:?^[1-9])|(^snc$)")
elif country == 'NL':
# Baseline:
# https://imbag.github.io/catalogus/hoofdstukken/attributen--relaties#734-huisnummertoevoeging
# (7.3.2 huisnummer, 7.3.3 huisletter and 7.3.4 huisnummertoevoeging)
# Exceptions to the rule:
# https://nl.wikipedia.org/wiki/Huisnummer
# This pattern isn't exhaustive, but it should catch most weirdness.
self.housenumber = re.compile(
# Houseboats, 't/o X' stands for 'opposite X', where 'X' is an address on shore
r"^(t/o )?"
# 'Pekela'-style exception, leading letter (e.g., 'C54')
"(([A-Z][1-9][0-9]{0,3})|"
# Normal base numbers, no leading zero, not exceeding 5 digits.
"([1-9][0-9]{0,4}))"
# Up to four optional extensions (can have leading zeroes in the extension part, e.g., '2K-008')
"([ -/]?(([0-9]{1,4})|([A-Za-z]{1,5}))){0,4}$")
else:
# By default, validate that house number starts with 1-9
self.housenumber = re.compile(r"^[1-9]")
def node(self, data, tags):
err = []
if "addr:housenumber" in tags and (len(tags["addr:housenumber"]) == 0 or not (self.housenumber.match(tags["addr:housenumber"]))):
err.append({"class": 10, "subclass": 1})
return err
def way(self, data, tags, nds):
err = self.node(data, tags)
interpolation = tags.get("addr:interpolation")
if interpolation:
if any(filter(lambda x: x.startswith("addr:") and x not in ('addr:interpolation', 'addr:inclusion'), tags.keys())):
err.append({"class": 14, "subclass": 1})
if interpolation not in ('even', 'odd', 'all', 'alphabetic') and not interpolation.isdigit():
err.append({"class": 15, "subclass": 1, "text": {'en': 'addr:interpolation={0}'.format(interpolation)}})
inclusion = tags.get("addr:inclusion")
if inclusion and inclusion not in ('actual', 'estimate', 'potential'):
err.append({"class": 15, "subclass": 2, "text": {'en': 'addr:inclusion={0}'.format(inclusion)}})
return err
def relation(self, data, tags, members):
return self.node(data, tags)
###########################################################################
from plugins.Plugin import TestPluginCommon
class Test(TestPluginCommon):
def test(self):
a = TagFix_Housenumber(None)
class _config:
options = {"country": "XY"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {})
assert not a.node(None, {"addr:housenumber": "33"})
assert not a.relation(None, {"addr:housenumber": "33"}, None)
assert a.node(None, {"addr:housenumber": ""})
assert a.node(None, {"addr:housenumber": "?"})
assert a.relation(None, {"addr:housenumber": "?"}, None)
assert a.way(None, {"addr:stret": "Lomlim", "addr:interpolation": "even"}, None)
assert not a.way(None, {"addr:interpolation": "even"}, None)
assert not a.way(None, {"addr:interpolation": "4"}, None)
assert not a.way(None, {"addr:interpolation": "4", "addr:inclusion": "actual"}, None)
assert a.way(None, {"addr:interpolation": "invalid"}, None)
assert a.way(None, {"addr:housenumber": "ev.387"}, None) # CZ housenumbers not valid outside CZ
assert a.node(None, {"addr:housenumber": "корпус"})
def test_CH(self):
a = TagFix_Housenumber(None)
class _config:
options = {"country": "CH"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {"addr:housenumber": "313A"})
assert not a.node(None, {"addr:housenumber": "1"})
assert not a.node(None, {"addr:housenumber": "1,2"})
assert a.node(None, {"addr:housenumber": "1;2"})
def test_CZ(self):
a = TagFix_Housenumber(None)
class _config:
options = {"country": "CZ"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.way(None, {"addr:housenumber": "ev.387"}, None) # In CZ
def test_LU(self):
a = TagFix_Housenumber(None)
class _config:
options = {"country": "LU"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {"addr:housenumber": "42A-44A"})
assert not a.node(None, {"addr:housenumber": "42BIS"})
def test_IT(self):
a = TagFix_Housenumber(None)
class _config:
options = {"country": "IT"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {"addr:housenumber": "42"})
assert not a.node(None, {"addr:housenumber": "snc"})
def test_NL(self):
a = TagFix_Housenumber(None)
class _config:
options = {"country": "NL"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {"addr:housenumber": "42"})
assert not a.node(None, {"addr:housenumber": "18A"})
assert not a.node(None, {"addr:housenumber": "123-24"})
assert not a.node(None, {"addr:housenumber": "2K-008"})
assert not a.node(None, {"addr:housenumber": "10t-13"})
assert not a.node(None, {"addr:housenumber": "13-bv"})
assert not a.node(None, {"addr:housenumber": "13 bv"})
assert not a.node(None, {"addr:housenumber": "1-TRAF"})
assert not a.node(None, {"addr:housenumber": "19p-8"})
assert not a.node(None, {"addr:housenumber": "44d-G"})
assert not a.node(None, {"addr:housenumber": "3-0072"})
# Groningen-style extension.
assert not a.node(None, {"addr:housenumber": "16/1"})
# Naval base in Den Helder
assert not a.node(None, {"addr:housenumber": "100D-G29B"})
# Pekela
assert not a.node(None, {"addr:housenumber": "B54"})
# Graan voor Visch, Hoofddorp
assert not a.node(None, {"addr:housenumber": "19601U"})
# Woonboot
assert not a.node(None, {"addr:housenumber": "t/o 56"})
# Utrecht
assert not a.node(None, {"addr:housenumber": "113B Bis A"})
assert not a.node(None, {"addr:housenumber": "7 bis"})
assert a.node(None, {"addr:housenumber": "1;2"})
assert a.node(None, {"addr:housenumber": " 1 "})
assert a.node(None, {"addr:housenumber": "1 "})
assert a.node(None, {"addr:housenumber": " 1"})
# Non-ASCII numerics.
assert a.node(None, {"addr:housenumber": "1"})
# Non-ASCII letters.
assert a.node(None, {"addr:housenumber": "1B"})
assert a.node(None, {"addr:housenumber": "123--A3"})
assert a.node(None, {"addr:housenumber": "123 -A3"})
assert a.node(None, {"addr:housenumber": "123 A3"})
# Mixed up tags.
assert a.node(None, {"addr:housenumber": "Dorpsstraat"})
# Trolls, bogus input.
assert a.node(None, {"addr:housenumber": "1234567890abcdefghijklmnopqrstuvwxyz"})