|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
try: |
|
4
|
|
|
import logging |
|
5
|
|
|
import sys |
|
6
|
|
|
import math |
|
7
|
|
|
import os |
|
8
|
|
|
import datetime |
|
9
|
|
|
from urllib.parse import quote |
|
10
|
|
|
from osm_poi_matchmaker.dao.data_structure import OSM_object_type |
|
11
|
|
|
from osm_poi_matchmaker.utils import config |
|
12
|
|
|
from osm_poi_matchmaker.libs.address import clean_url |
|
13
|
|
|
from osm_poi_matchmaker.libs.osm import relationer, timestamp_now |
|
|
|
|
|
|
14
|
|
|
from osm_poi_matchmaker.libs.compare_strings import compare_strings |
|
15
|
|
|
from sqlalchemy.orm import scoped_session, sessionmaker |
|
16
|
|
|
from osm_poi_matchmaker.dao.poi_base import POIBase |
|
|
|
|
|
|
17
|
|
|
from lxml import etree |
|
18
|
|
|
import lxml |
|
19
|
|
|
except ImportError as err: |
|
20
|
|
|
logging.error('Error %s import module: %s', __name__, err) |
|
21
|
|
|
logging.exception('Exception occurred') |
|
22
|
|
|
|
|
23
|
|
|
sys.exit(128) |
|
24
|
|
|
|
|
25
|
|
|
POI_TAGS = {'poi_name': 'name', 'poi_city': 'addr:city', 'poi_postcode': 'addr:postcode', |
|
26
|
|
|
'poi_addr_street': 'addr:street', 'poi_addr_housenumber': 'addr:housenumber', |
|
27
|
|
|
'poi_conscriptionnumber': 'addr:conscriptionnumber', 'poi_branch': 'branch', 'poi_email': 'email'} |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
POI_YESNO_TAGS = {'poi_fuel_adblue': 'fuel:adblue', 'poi_fuel_octane_100': 'fuel:octane_100', |
|
30
|
|
|
'poi_fuel_octane_98': 'fuel:octane_98', 'poi_fuel_octane_95': 'fuel:octane_95', |
|
31
|
|
|
'poi_fuel_diesel_gtl': 'fuel:GTL_diesel', 'poi_fuel_diesel': 'fuel:diesel', |
|
32
|
|
|
'poi_fuel_lpg': 'fuel:lpg', 'poi_fuel_e85': 'fuel:e85', 'poi_rent_lpg_bottles': 'rent:lpg_bottles', |
|
|
|
|
|
|
33
|
|
|
'poi_compressed_air': 'compressed_air', 'poi_restaurant': 'restaurant', 'poi_food': 'food', |
|
|
|
|
|
|
34
|
|
|
'poi_truck': 'truck', 'poi_authentication_app': 'authentication:app', |
|
35
|
|
|
'poi_authentication_membership_card': 'authentication:membership_card', 'poi_fee': 'fee', |
|
|
|
|
|
|
36
|
|
|
'poi_parking_fee': 'parking_fee', 'poi_motorcar': 'motorcar'} |
|
37
|
|
|
|
|
38
|
|
|
POI_EV_TAGS = {'poi_capacity': 'capacity', |
|
39
|
|
|
'poi_socket_chademo': 'socket:chademo', 'poi_socket_chademo_output': 'socket:chademo:output', |
|
|
|
|
|
|
40
|
|
|
'poi_socket_type2_combo': 'socket:type2_combo', |
|
41
|
|
|
'poi_socket_type2_combo_output': 'socket:type2_combo:output', |
|
42
|
|
|
'poi_socket_type2_cable': 'socket:type2_cable', |
|
43
|
|
|
'poi_socket_type2_cable_output': 'socket:type2_cable:output', |
|
44
|
|
|
'poi_socket_type2': 'socket:type2', 'poi_socket_type2_output': 'socket:type2:output', |
|
45
|
|
|
'poi_manufacturer': 'manufacturer', 'poi_model': 'model'} |
|
46
|
|
|
|
|
47
|
|
|
TESTCASE_GEN_KEYS = ('original', 'poi_postcode', 'poi_city', 'poi_addr_street', 'poi_addr_housenumber', 'poi_conscriptionnumber') |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
TIMESTAMP_FORMAT = '{:{dfmt}T{tfmt}Z}' |
|
50
|
|
|
DATE_FOTMAT = '%Y-%m-%d' |
|
51
|
|
|
TIME_FORMAT = '%H:%M:%S' |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def ascii_numcoder(text): |
|
|
|
|
|
|
55
|
|
|
output = '' |
|
56
|
|
|
for i in text: |
|
57
|
|
|
if i in range(0, 10, 1): |
|
58
|
|
|
output += i |
|
59
|
|
|
else: |
|
60
|
|
|
output += str(ord(i)) |
|
61
|
|
|
return output |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def save_csv_file(path: str, file: str, data, message: str): |
|
65
|
|
|
"""Save Pandas dataframe to a CSV file |
|
66
|
|
|
|
|
67
|
|
|
Args: |
|
68
|
|
|
path (str): Path of newly created CVS file |
|
69
|
|
|
file (str): Filename of newly created CVS file |
|
70
|
|
|
data (pd.DataFrame): Pandas dataframe to write |
|
71
|
|
|
message (str): Addtion information to display |
|
72
|
|
|
""" |
|
73
|
|
|
try: |
|
74
|
|
|
# Save file to CSV file |
|
75
|
|
|
logging.info('Saving {%s to file: %s', message, file) |
|
76
|
|
|
res = data.to_csv(os.path.join(path, file)) |
|
|
|
|
|
|
77
|
|
|
logging.info('The %s was sucessfully saved', file) |
|
78
|
|
|
except Exception as e: |
|
|
|
|
|
|
79
|
|
|
logging.error(e) |
|
80
|
|
|
logging.exception('Exception occurred') |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
def add_osm_node(osm_id: int, node_data: dict, prefix: str = 'poi') -> dict: |
|
84
|
|
|
"""Generate OpenStreetMap node header information as string |
|
85
|
|
|
|
|
86
|
|
|
Args: |
|
87
|
|
|
osm_id (int): OpenStreetMap ID |
|
88
|
|
|
node_data (dict): [description] |
|
89
|
|
|
prefix (str): Prefix for field names in database |
|
90
|
|
|
|
|
91
|
|
|
Returns: |
|
92
|
|
|
str: [description] |
|
93
|
|
|
""" |
|
94
|
|
|
logging.info(node_data.to_string()) |
|
95
|
|
|
if node_data.get('osm_timestamp') is None: |
|
96
|
|
|
osm_timestamp = datetime.datetime.now() |
|
97
|
|
|
else: |
|
98
|
|
|
osm_timestamp = node_data.get('osm_timestamp') |
|
99
|
|
|
osm_version = '99999' if node_data.get('osm_version') is None else node_data.get('osm_version') |
|
100
|
|
|
osm_data = {'action': 'modify', 'id': str(osm_id), |
|
101
|
|
|
'lat': '{}'.format(node_data.get('{}_lat'.format(prefix))), |
|
102
|
|
|
'lon': '{}'.format(node_data.get('{}_lon'.format(prefix))), |
|
103
|
|
|
'user': '{}'.format('osm_poi_matchmaker'), 'uid': '{}'.format('8635934'), 'version': '{}'.format(osm_version), |
|
|
|
|
|
|
104
|
|
|
'timestamp': TIMESTAMP_FORMAT.format(osm_timestamp, dfmt=DATE_FOTMAT, tfmt=TIME_FORMAT)} |
|
|
|
|
|
|
105
|
|
|
logging.info(osm_data) |
|
106
|
|
|
return osm_data |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
def list_osm_node(osm_id: int, node_data: dict, prefix='poi') -> dict: |
|
110
|
|
|
"""Generate OpenStreetMap node header information as string |
|
111
|
|
|
|
|
112
|
|
|
Args: |
|
113
|
|
|
osm_id (int): OpenStreetMap ID |
|
114
|
|
|
node_data (dict): [description] |
|
115
|
|
|
prefix (str): Prefix for field names in database |
|
116
|
|
|
|
|
117
|
|
|
Returns: |
|
118
|
|
|
str: [description] |
|
119
|
|
|
""" |
|
120
|
|
|
logging.debug(node_data) |
|
121
|
|
|
osm_user = 'osm_poi_matchmaker' if node_data.get('osm_user') is None else node_data.get('osm_user') |
|
|
|
|
|
|
122
|
|
|
osm_user_id = '8635934' if node_data.get('osm_user_id') is None else node_data.get('osm_user_id') |
|
|
|
|
|
|
123
|
|
|
if node_data.get('osm_timestamp') is None: |
|
124
|
|
|
osm_timestamp = datetime.datetime.now() |
|
125
|
|
|
else: |
|
126
|
|
|
osm_timestamp = node_data.get('osm_timestamp') |
|
127
|
|
|
osm_version = '99999' if node_data.get('osm_version') is None else node_data.get('osm_version') |
|
128
|
|
|
osm_data = {'id': str(osm_id), |
|
129
|
|
|
'lat': '{}'.format(node_data.get('{}_lat'.format(prefix))), |
|
130
|
|
|
'lon': '{}'.format(node_data.get('{}_lon'.format(prefix))), |
|
131
|
|
|
'user': '{}'.format(osm_user), 'uid': '{}'.format(osm_user_id), 'version': '{}'.format(osm_version), |
|
|
|
|
|
|
132
|
|
|
'timestamp': TIMESTAMP_FORMAT.format(osm_timestamp, dfmt=DATE_FOTMAT, tfmt=TIME_FORMAT)} |
|
|
|
|
|
|
133
|
|
|
logging.debug(osm_data) |
|
134
|
|
|
return osm_data |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
def add_osm_way(osm_id: int, node_data: dict) -> dict: |
|
138
|
|
|
"""Generate OpenStreetMap way header information as dictionary |
|
139
|
|
|
|
|
140
|
|
|
Args: |
|
141
|
|
|
osm_id (int): [description] |
|
142
|
|
|
node_data (dict): [description] |
|
143
|
|
|
|
|
144
|
|
|
Returns: |
|
145
|
|
|
str: [description] |
|
146
|
|
|
""" |
|
147
|
|
|
if node_data.get('osm_timestamp') is None: |
|
148
|
|
|
osm_timestamp = datetime.datetime.now() |
|
149
|
|
|
else: |
|
150
|
|
|
osm_timestamp = node_data.get('osm_timestamp') |
|
151
|
|
|
osm_version = '99999' if node_data.get('osm_version') is None else node_data.get('osm_version') |
|
152
|
|
|
osm_data = {'action': 'modify', 'id': str(osm_id), |
|
153
|
|
|
'user': '{}'.format('osm_poi_matchmaker'), 'uid': '{}'.format('8635934'), |
|
154
|
|
|
'version': '{}'.format(osm_version), |
|
155
|
|
|
'timestamp': TIMESTAMP_FORMAT.format(osm_timestamp, dfmt=DATE_FOTMAT, tfmt=TIME_FORMAT)} |
|
|
|
|
|
|
156
|
|
|
return osm_data |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
def add_osm_link_comment(osm_id: int, osm_type) -> str: |
|
160
|
|
|
"""Create OpenStreetMap osm.org link from OSM object |
|
161
|
|
|
|
|
162
|
|
|
Args: |
|
163
|
|
|
osm_id (int): [description] |
|
164
|
|
|
osm_type ([type]): [description] |
|
165
|
|
|
|
|
166
|
|
|
Returns: |
|
167
|
|
|
str: [description] |
|
168
|
|
|
""" |
|
169
|
|
|
osm_comment = ' OSM link: https://osm.org/{}/{} '.format(osm_type.name, str(osm_id)) |
|
170
|
|
|
return osm_comment |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
def generate_osm_xml(df, session=None): |
|
|
|
|
|
|
174
|
|
|
"""Crete OpenStreetMap (OSM XML) file from passed Panda Dataframe |
|
175
|
|
|
|
|
176
|
|
|
Args: |
|
177
|
|
|
df ([type]): [description] |
|
178
|
|
|
session ([type], optional): [description]. Defaults to None. |
|
179
|
|
|
|
|
180
|
|
|
Returns: |
|
181
|
|
|
[type]: [description] |
|
182
|
|
|
""" |
|
183
|
|
|
db = POIBase('{}://{}:{}@{}:{}/{}'.format(config.get_database_type(), config.get_database_writer_username(), |
|
|
|
|
|
|
184
|
|
|
config.get_database_writer_password(), |
|
185
|
|
|
config.get_database_writer_host(), |
|
186
|
|
|
config.get_database_writer_port(), |
|
187
|
|
|
config.get_database_poi_database())) |
|
188
|
|
|
pgsql_pool = db.pool |
|
189
|
|
|
session_factory = sessionmaker(pgsql_pool) |
|
190
|
|
|
Session = scoped_session(session_factory) |
|
|
|
|
|
|
191
|
|
|
session = Session() |
|
192
|
|
|
osm_xml_data = etree.Element('osm', version='0.6', generator='JOSM') |
|
|
|
|
|
|
193
|
|
|
default_osm_id = -1 |
|
194
|
|
|
current_osm_id = default_osm_id |
|
195
|
|
|
added_nodes = [] |
|
196
|
|
|
try: |
|
|
|
|
|
|
197
|
|
|
for index, row in df.iterrows(): |
|
|
|
|
|
|
198
|
|
|
tags = {} |
|
199
|
|
|
osm_live_tags = {} |
|
200
|
|
|
main_data = {} |
|
201
|
|
|
current_osm_id = default_osm_id if row.get('osm_id') is None else row.get('osm_id') |
|
202
|
|
|
osm_version = '99999' if row.get('osm_version') is None else row.get('osm_version') |
|
|
|
|
|
|
203
|
|
|
if row.get('osm_node') is None or row.get('osm_node') == OSM_object_type.node: |
|
204
|
|
|
try: |
|
205
|
|
|
josm_object = 'n{}'.format(current_osm_id) |
|
206
|
|
|
main_data = etree.SubElement(osm_xml_data, 'node', add_osm_node(current_osm_id, row)) |
|
|
|
|
|
|
207
|
|
|
except Exception as e: |
|
|
|
|
|
|
208
|
|
|
logging.exception('Exception occurred') |
|
209
|
|
|
elif row.get('osm_node') is not None and row.get('osm_node') == OSM_object_type.way: |
|
210
|
|
|
try: |
|
211
|
|
|
main_data = etree.SubElement(osm_xml_data, 'way', add_osm_way(current_osm_id, row)) |
|
|
|
|
|
|
212
|
|
|
josm_object = 'w{}'.format(current_osm_id) |
|
213
|
|
|
# Add way nodes without any modification) |
|
214
|
|
|
node_data = [] |
|
215
|
|
|
for n in row.get('osm_nodes'): |
|
|
|
|
|
|
216
|
|
|
data = etree.SubElement(main_data, 'nd', ref=str(n)) |
|
|
|
|
|
|
217
|
|
|
if session is not None: |
|
218
|
|
|
# Go through the list except the last value (which is same as the first) |
|
219
|
|
|
for n in row.get('osm_nodes'): |
|
|
|
|
|
|
220
|
|
|
# Add nodes only when it is not already added. |
|
221
|
|
|
if n not in added_nodes: |
|
222
|
|
|
added_nodes.append(n) |
|
223
|
|
|
way_node = db.query_from_cache(n, OSM_object_type.node) |
|
224
|
|
|
if way_node is not None: |
|
225
|
|
|
node_data = etree.SubElement(osm_xml_data, 'node', |
|
|
|
|
|
|
226
|
|
|
list_osm_node(n, way_node, 'osm')) |
|
227
|
|
|
if node_data.get('osm_live_tags') is not None and \ |
|
228
|
|
|
node_data.get('osm_live_tags') != '': |
|
229
|
|
|
node_osm_live_tags = node_data.get('osm_live_tags') |
|
230
|
|
|
for k, v in sorted(node_osm_live_tags).items(): |
|
|
|
|
|
|
231
|
|
|
xml_node_tags = etree.SubElement(node_data, 'tag', k=k, v='{}'.format(v)) |
|
|
|
|
|
|
232
|
|
|
except TypeError as e: |
|
|
|
|
|
|
233
|
|
|
logging.warning('Missing nodes on this way: %s.', row.get('osm_id')) |
|
234
|
|
|
logging.exception('Exception occurred') |
|
235
|
|
|
elif row.get('osm_node') is not None and row.get('osm_node') == OSM_object_type.relation: |
|
|
|
|
|
|
236
|
|
|
try: |
|
237
|
|
|
main_data = etree.SubElement(osm_xml_data, 'relation', add_osm_way(current_osm_id, row)) |
|
|
|
|
|
|
238
|
|
|
josm_object = 'r{}'.format(current_osm_id) |
|
239
|
|
|
relations = relationer(row.get('osm_nodes')) |
|
240
|
|
|
for i in relations: |
|
241
|
|
|
data = etree.SubElement(main_data, 'member', type=i.get('type'), ref=i.get('ref'), |
|
|
|
|
|
|
242
|
|
|
role=i.get('role')) |
|
243
|
|
|
except TypeError as e: |
|
|
|
|
|
|
244
|
|
|
logging.warning('Missing nodes on this relation: %s.', row['osm_id']) |
|
245
|
|
|
logging.exception('Exception occurred') |
|
246
|
|
|
# Add already existing node, way, relation OpenStreetMap reference as comment |
|
247
|
|
|
try: |
|
248
|
|
|
if current_osm_id > 0: |
|
249
|
|
|
osm_xml_data.append(etree.Comment(add_osm_link_comment(current_osm_id, row.get('osm_node')))) |
|
|
|
|
|
|
250
|
|
|
# Add original POI coordinates as comment |
|
251
|
|
|
comment = etree.Comment(' Original coordinates: {} '.format(row.get('poi_geom'))) |
|
|
|
|
|
|
252
|
|
|
osm_xml_data.append(comment) |
|
253
|
|
|
if 'poi_distance' in row: |
|
254
|
|
|
if row.get('poi_distance') is not None: |
|
255
|
|
|
comment = etree.Comment(' OSM <-> POI distance: {} m'.format(row.get('poi_distance'))) |
|
|
|
|
|
|
256
|
|
|
else: |
|
257
|
|
|
comment = etree.Comment(' OSM <-> POI distance: Non exist') |
|
|
|
|
|
|
258
|
|
|
osm_xml_data.append(comment) |
|
259
|
|
|
if 'poi_good' in row and 'poi_bad' in row: |
|
260
|
|
|
comment = etree.Comment(' Checker good: {}; bad {}'.format(row.get('poi_good'), row.get('poi_bad'))) |
|
|
|
|
|
|
261
|
|
|
osm_xml_data.append(comment) |
|
262
|
|
|
# Using already definied OSM tags if exists |
|
263
|
|
|
if row.get('osm_live_tags') is not None: |
|
264
|
|
|
tags.update(row.get('osm_live_tags').copy()) |
|
265
|
|
|
logging.critical(row.get('osm_live_tags')) |
|
266
|
|
|
osm_live_tags.update(row.get('osm_live_tags').copy()) |
|
267
|
|
|
# Adding POI common tags |
|
268
|
|
|
if row.get('poi_tags') is not None: |
|
269
|
|
|
tags.update(row.get('poi_tags')) |
|
270
|
|
|
# Save live name tags if preserve name is enabled |
|
271
|
|
|
except Exception as e: |
|
|
|
|
|
|
272
|
|
|
logging.exception('Exception occurred') |
|
273
|
|
|
try: |
|
274
|
|
|
if row.get('preserve_original_name') is True: |
|
275
|
|
|
preserved_name = tags.get('name') |
|
276
|
|
|
except KeyError as e: |
|
|
|
|
|
|
277
|
|
|
logging.debug('No name tag is specified to save in original OpenStreetMap data.') |
|
278
|
|
|
try: |
|
279
|
|
|
# Overwriting with data from data providers |
|
280
|
|
|
for k, v in POI_TAGS.items(): |
|
|
|
|
|
|
281
|
|
|
if row.get(k) is not None: |
|
282
|
|
|
tags[v] = row.get(k) |
|
283
|
|
|
except Exception as e: |
|
|
|
|
|
|
284
|
|
|
logging.exception('Exception occurred') |
|
285
|
|
|
try: |
|
286
|
|
|
if config.get_geo_alternative_opening_hours(): |
|
287
|
|
|
alternative_oh_tag = config.get_geo_alternative_opening_hours_tag() |
|
288
|
|
|
# Alternative opening_hours handling for COVID-19 code path |
|
289
|
|
|
if tags.get('opening_hours') is not None and tags.get('opening_hours') != '': |
|
290
|
|
|
if row.get('poi_opening_hours') is not None and row.get('poi_opening_hours') != '': |
|
|
|
|
|
|
291
|
|
|
if tags.get('opening_hours') == row.get('poi_opening_hours'): |
|
292
|
|
|
tags[alternative_oh_tag] = 'same' |
|
293
|
|
|
else: |
|
294
|
|
|
tags[alternative_oh_tag] = row.get('poi_opening_hours') |
|
295
|
|
|
else: |
|
296
|
|
|
if row.get('poi_opening_hours') is not None and row.get('poi_opening_hours') != '': |
|
|
|
|
|
|
297
|
|
|
tags['opening_hours'] = row.get('poi_opening_hours') |
|
298
|
|
|
tags[alternative_oh_tag] = 'same' |
|
299
|
|
|
else: |
|
300
|
|
|
# Alternative opening_hours handling for NON COVID-19 code path: just simply add opening_hours to tags |
|
|
|
|
|
|
301
|
|
|
if row.get('poi_opening_hours') is not None and row.get('poi_opening_hours') != '': |
|
|
|
|
|
|
302
|
|
|
tags['opening_hours'] = row.get('poi_opening_hours') |
|
303
|
|
|
except Exception as e: |
|
|
|
|
|
|
304
|
|
|
logging.exception('Exception occurred') |
|
305
|
|
|
try: |
|
306
|
|
|
# If we got POI phone tag use it as OSM contact:phone tag |
|
307
|
|
|
if row.get('poi_phone') is not None and row.get('poi_phone') != '': |
|
308
|
|
|
tags['contact:phone'] = row.get('poi_phone') |
|
309
|
|
|
except Exception as e: |
|
|
|
|
|
|
310
|
|
|
logging.exception('Exception occurred') |
|
311
|
|
|
try: |
|
312
|
|
|
# If we got POI website tag use it as OSM contact:website tag |
|
313
|
|
|
if row.get('poi_url_base') is not None and row.get('poi_website') is not None: |
|
314
|
|
|
if row['poi_url_base'] in row.get('poi_website'): |
|
315
|
|
|
# The POI website contains the base URL use the POI website field only |
|
316
|
|
|
tags['contact:website'] = clean_url('{}'.format((row.get('poi_website')))) |
|
317
|
|
|
else: |
|
318
|
|
|
# The POI website does not contain the base URL use the merged base URL and POI website field |
|
|
|
|
|
|
319
|
|
|
tags['contact:website'] = clean_url('{}/{}'.format(row.get('poi_url_base'), row.get('poi_website'))) |
|
|
|
|
|
|
320
|
|
|
# If only the base URL is available |
|
321
|
|
|
elif row.get('poi_url_base') is not None: |
|
322
|
|
|
tags['contact:website'] = row.get('poi_url_base') |
|
323
|
|
|
except Exception as e: |
|
|
|
|
|
|
324
|
|
|
logging.exception('Exception occurred') |
|
325
|
|
|
try: |
|
326
|
|
|
# Short URL for source |
|
327
|
|
|
if row['poi_url_base'] is not None: |
|
328
|
|
|
source_url = 'source:{}:date'.format(row.get('poi_url_base').split('/')[2]) |
|
329
|
|
|
else: |
|
330
|
|
|
source_url = 'source:website:date' |
|
331
|
|
|
tags[source_url] = '{:{dfmt}}'.format(datetime.datetime.now(), dfmt=DATE_FOTMAT) |
|
332
|
|
|
except Exception as e: |
|
|
|
|
|
|
333
|
|
|
logging.exception('Exception occurred') |
|
334
|
|
|
try: |
|
335
|
|
|
# Write back the saved name tag |
|
336
|
|
|
if 'preserved_name' in locals(): |
|
337
|
|
|
tags['name'] = preserved_name |
|
338
|
|
|
# Rewrite old contact tags to contact:* tag form |
|
339
|
|
|
tags_rewrite = ['website', 'phone', 'email', 'facebook', 'instagram', 'youtube', 'pinterest', 'fax'] |
|
|
|
|
|
|
340
|
|
|
for tr in tags_rewrite: |
|
|
|
|
|
|
341
|
|
|
if tr in tags: |
|
342
|
|
|
# Never overwrite already existing contact:* tags |
|
343
|
|
|
if 'contact:' + tr in tags: |
|
344
|
|
|
# We already have this contact:* tag so remove the simple contact tag |
|
345
|
|
|
tags.pop(tr, None) |
|
346
|
|
|
else: |
|
347
|
|
|
# Rewrite simple contact tag to contact:* tag |
|
348
|
|
|
tags['contact:' + tr] = tags.pop(tr, None) |
|
349
|
|
|
except Exception as e: |
|
|
|
|
|
|
350
|
|
|
logging.exception('Exception occurred') |
|
351
|
|
|
try: |
|
352
|
|
|
if row.get('poi_description') is not None and row.get('poi_description') != '': |
|
353
|
|
|
tags['description'] = row.get('poi_description') |
|
354
|
|
|
except Exception as e: |
|
|
|
|
|
|
355
|
|
|
logging.exception('Exception occurred') |
|
356
|
|
|
try: |
|
357
|
|
|
# Write tags with yes/no value |
|
358
|
|
|
for k, v in POI_YESNO_TAGS.items(): |
|
|
|
|
|
|
359
|
|
|
if row.get(k) is not None and row.get(k) != '': |
|
360
|
|
|
tags[v] = 'yes' if row.get(k) is True else 'no' |
|
361
|
|
|
for k, v in POI_EV_TAGS.items(): |
|
|
|
|
|
|
362
|
|
|
if row.get(k) is not None and row.get(k) != '': |
|
363
|
|
|
if isinstance(row.get(k), float): |
|
364
|
|
|
if not math.isnan(row.get(k)): |
|
365
|
|
|
tags[v] = int(row.get(k)) |
|
366
|
|
|
else: |
|
367
|
|
|
tags[v] = row.get(k) |
|
368
|
|
|
except Exception as e: |
|
|
|
|
|
|
369
|
|
|
logging.exception('Exception occurred') |
|
370
|
|
|
try: |
|
371
|
|
|
# This is a new POI - will add fix me tag to the new items. |
|
372
|
|
|
if row.get('poi_new') is not None and row.get('poi_new') is True: |
|
373
|
|
|
tags['fixme'] = 'verify import' |
|
374
|
|
|
# Remove unwanted addr:country from file output as we discussed in Issue #33 |
|
375
|
|
|
tags.pop('addr:country', None) |
|
376
|
|
|
# tags['import'] = 'osm_poi_matchmaker' |
|
377
|
|
|
# Rendering tags to the XML file and JOSM magic link |
|
378
|
|
|
except Exception as e: |
|
|
|
|
|
|
379
|
|
|
logging.exception('Exception occurred') |
|
380
|
|
|
try: |
|
381
|
|
|
josm_link = '' |
|
382
|
|
|
comment = '\nKey\t\t\t\tStatus\t\tNew value\t\tOSM value\n' |
|
383
|
|
|
for k, v in sorted(tags.items()): |
|
|
|
|
|
|
384
|
|
|
xml_tags = etree.SubElement(main_data, 'tag', k=k, v='{}'.format(v)) |
|
|
|
|
|
|
385
|
|
|
josm_link = '{}|{}={}'.format(josm_link, k, v) |
|
386
|
|
|
# Add original POI tags as comment |
|
387
|
|
|
try: |
|
388
|
|
|
if isinstance(v, str): |
|
389
|
|
|
v = v.replace('-', '\-').replace('\n', '') |
|
|
|
|
|
|
390
|
|
|
w = osm_live_tags[k] |
|
|
|
|
|
|
391
|
|
|
except KeyError: |
|
392
|
|
|
comment += "{:32} N\t\t'{}'\n".format(k, v) |
|
393
|
|
|
else: |
|
394
|
|
|
if isinstance(w, str): |
|
395
|
|
|
w = w.replace('-', '\-').replace('\n', '') |
|
|
|
|
|
|
396
|
|
|
comment += "{:32} {}\t\t'{}'\t\t\t'{}'\n".format(k, compare_strings(v, w), v, w) |
|
|
|
|
|
|
397
|
|
|
comment = etree.Comment(comment) |
|
|
|
|
|
|
398
|
|
|
except Exception as e: |
|
|
|
|
|
|
399
|
|
|
logging.exception('Exception occurred') |
|
400
|
|
|
try: |
|
401
|
|
|
osm_xml_data.append(comment) |
|
402
|
|
|
except Exception as e: |
|
|
|
|
|
|
403
|
|
|
logging.exception('Exception occurred') |
|
404
|
|
|
try: |
|
405
|
|
|
# URL encode link and '--' in comment |
|
406
|
|
|
josm_link = quote(josm_link) |
|
407
|
|
|
josm_link = josm_link.replace('--', '%2D%2D') |
|
408
|
|
|
comment = etree.Comment(' JOSM magic link: {}?new_layer=false&objects={}&addtags={} '.format |
|
|
|
|
|
|
409
|
|
|
('http://localhost:8111/load_object', josm_object, josm_link)) |
|
|
|
|
|
|
410
|
|
|
osm_xml_data.append(comment) |
|
411
|
|
|
except Exception as e: |
|
|
|
|
|
|
412
|
|
|
logging.exception('Exception occurred') |
|
413
|
|
|
try: |
|
414
|
|
|
test_case = {k: row.get(k, None) for k in TESTCASE_GEN_KEYS} |
|
415
|
|
|
comment = etree.Comment("ˇ'original': '{t[original]}', 'postcode': '{t[poi_postcode]}', 'city': '{t[poi_city]}', 'street': '{t[poi_addr_street]}', 'housenumber': '{t[poi_addr_housenumber]}', 'conscriptionnumber': '{t[poi_conscriptionnumber]}'°".format(t=test_case)) |
|
|
|
|
|
|
416
|
|
|
osm_xml_data.append(comment) |
|
417
|
|
|
except Exception as e: |
|
|
|
|
|
|
418
|
|
|
logging.exception('Exception occurred') |
|
419
|
|
|
try: |
|
420
|
|
|
osm_xml_data.append(xml_node_tags) |
|
|
|
|
|
|
421
|
|
|
except UnboundLocalError as e: |
|
|
|
|
|
|
422
|
|
|
logging.debug('Unbound local error extra node tags') |
|
423
|
|
|
try: |
|
424
|
|
|
osm_xml_data.append(main_data) |
|
425
|
|
|
# Next deafult OSM id is one more less for non existing objects |
|
426
|
|
|
default_osm_id -= 1 |
|
427
|
|
|
except Exception as e: |
|
|
|
|
|
|
428
|
|
|
logging.exception('Exception occurred') |
|
429
|
|
|
except ValueError as e: |
|
|
|
|
|
|
430
|
|
|
logging.error(e) |
|
431
|
|
|
logging.exception('Exception occurred') |
|
432
|
|
|
|
|
433
|
|
|
except Exception as e: |
|
|
|
|
|
|
434
|
|
|
logging.error(e) |
|
435
|
|
|
logging.exception('Exception occurred') |
|
436
|
|
|
|
|
437
|
|
|
return lxml.etree.tostring(osm_xml_data, pretty_print=True, xml_declaration=True, encoding="UTF-8") |
|
|
|
|
|
|
438
|
|
|
|