forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstruction.py
202 lines (174 loc) · 7.07 KB
/
Construction.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
# -*- coding: utf-8 -*-
###########################################################################
# ##
# Copyrights Frédéric Rodrigo 2011-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 datetime
import dateutil.parser
from modules.OsmoseTranslation import T_
from plugins.Plugin import Plugin
class Construction(Plugin):
def init(self, logger):
Plugin.init(self, logger)
if self.father.config.options.get("project") != "openstreetmap":
return False
self.errors[4070] = self.def_class(
item=4070,
level=2,
tags=["tag", "fix:survey"],
title=T_("Finished construction"),
detail=T_(
"""There is no tag `opening_date`, `check_date`, `open_date`,
`construction:date`, `temporary:date_on`, `date_on` and the object has
been in construction for more than two years or opening data is
exceeded."""
),
)
self.tag_construction = ["highway", "landuse", "building"]
self.tag_date = [
"opening_date",
"open_date",
"construction:date",
"temporary:date_on",
"date_on",
]
self.default_date = datetime.datetime(9999, 12, 1)
self.today = datetime.datetime.today()
self.date_limit = datetime.timedelta(days=2 * 365)
self.recall = int(self.total_seconds(datetime.timedelta(days=6 * 30)))
def getTagDate(self, tags):
for i in self.tag_date:
if i in tags:
return tags[i]
def convert2date(self, string):
try:
date = dateutil.parser.parse(string, default=self.default_date)
if date.tzinfo and abs(date.tzinfo.utcoffset()) > 1439:
# Ignore tzinfo if not in valid range
date.tzinfo = None
if date.year != 9999:
return date
except ValueError:
pass
except TypeError:
# triggered by python-dateutil 2.2, on an incorrect string
pass
def node(self, data, tags):
construction_found = False
for t in tags:
if t == "construction" or (
t.startswith("construction:") and t != "construction:date"
):
construction_found = True
break
if not construction_found:
for t in self.tag_construction:
if tags.get(t) == "construction":
construction_found = True
break
if not construction_found:
return
date = None
tagDate = self.getTagDate(tags)
if tagDate:
date = self.convert2date(tagDate)
end_date = False
if date:
end_date = date
elif "timestamp" in data:
end_date = (
datetime.datetime.strptime(data["timestamp"][0:10], "%Y-%m-%d")
+ self.date_limit
)
else:
return
delta = int(self.total_seconds(self.today - end_date))
if delta > 0:
# Change the subclass every 6 months after expiration, re-popup the marker in frontend event if set as false-positive
return {"class": 4070, "subclass": delta // self.recall}
def way(self, data, tags, nds):
return self.node(data, tags)
def relation(self, data, tags, members):
return self.node(data, tags)
def total_seconds(self, td):
# Note: compared to timedelta.total_seconds(), this function doesn't use microseconds
return td.seconds + td.days * 24 * 3600
###########################################################################
from plugins.Plugin import TestPluginCommon
class Test(TestPluginCommon):
def setUp(self):
TestPluginCommon.setUp(self)
self.p = Construction(None)
self.set_default_config(self.p)
self.p.init(None)
def test(self):
constr_tags = [
{"construction": "yes"},
{"highway": "construction"},
{"landuse": "construction"},
{"building": "construction"},
{"construction:man_made": "water_works"},
]
other_tags = [
{"highway": "primary"},
{"landuse": "farm"},
{"building": "yes"},
]
correct_dates = [
"2010-02-03",
"January 3rd, 2012",
"02/01/1987",
"12/21/1993",
"22/01/2012",
]
not_correct_dates = [
"22/01/2123",
"2142-10-01",
"monday",
"yes",
"2018-11-11--2018-12-31",
]
for tags in constr_tags:
for tag_d in self.p.tag_date:
for val_d in correct_dates:
t = tags.copy()
t.update({tag_d: val_d})
self.check_err(self.p.node({}, t), t)
for val_d in not_correct_dates:
t = tags.copy()
t.update({tag_d: val_d})
assert not self.p.way({}, t, None), t
for tags in other_tags:
for tag_d in self.p.tag_date:
for val_d in correct_dates:
t = tags.copy()
t.update({tag_d: val_d})
assert not self.p.relation({}, t, None), t
def test_timestamp(self):
tags = {"construction": "yes"}
for ts in ["2003-01-04", "1989-03-10"]:
self.check_err(self.p.node({"timestamp": ts}, tags), ts)
for ts in ["2078-01-04"]:
assert not self.p.node({"timestamp": ts}, tags), ts
def test_recall(self):
tags = {"construction": "yes"}
today = datetime.datetime.today()
td = datetime.timedelta(days=6 * 30)
for i in range(5, 10, 1):
e = self.p.node({"timestamp": (today - i * td).strftime("%Y-%m-%d")}, tags)
self.check_err(e, i)
assert e["subclass"] == i - 5