-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
389 lines (315 loc) · 15 KB
/
data.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
After auditing is complete the next step is to prepare the data to be inserted into a SQL database.
To do so you will parse the elements in the OSM XML file, transforming them from document format to
tabular format, thus making it possible to write to .csv files. These csv files can then easily be
imported to a SQL database as tables.
The process for this transformation is as follows:
- Use iterparse to iteratively step through each top level element in the XML
- Shape each element into several data structures using a custom function
- Utilize a schema and validation library to ensure the transformed data is in the correct format
- Write each data structure to the appropriate .csv files
We've already provided the code needed to load the data, perform iterative parsing and write the
output to csv files. Your task is to complete the shape_element function that will transform each
element into the correct format. To make this process easier we've already defined a schema (see
the schema.py file in the last code tab) for the .csv files and the eventual tables. Using the
cerberus library we can validate the output against this schema to ensure it is correct.
## Shape Element Function
The function should take as input an iterparse Element object and return a dictionary.
### If the element top level tag is "node":
The dictionary returned should have the format {"node": .., "node_tags": ...}
The "node" field should hold a dictionary of the following top level node attributes:
- id
- user
- uid
- version
- lat
- lon
- timestamp
- changeset
All other attributes can be ignored
The "node_tags" field should hold a list of dictionaries, one per secondary tag. Secondary tags are
child tags of node which have the tag name/type: "tag". Each dictionary should have the following
fields from the secondary tag attributes:
- id: the top level node id attribute value
- key: the full tag "k" attribute value if no colon is present or the characters after the colon if one is.
- value: the tag "v" attribute value
- type: either the characters before the colon in the tag "k" value or "regular" if a colon
is not present.
Additionally,
- if the tag "k" value contains problematic characters, the tag should be ignored
- if the tag "k" value contains a ":" the characters before the ":" should be set as the tag type
and characters after the ":" should be set as the tag key
- if there are additional ":" in the "k" value they and they should be ignored and kept as part of
the tag key. For example:
<tag k="addr:street:name" v="Lincoln"/>
should be turned into
{'id': 12345, 'key': 'street:name', 'value': 'Lincoln', 'type': 'addr'}
- If a node has no secondary tags then the "node_tags" field should just contain an empty list.
The final return value for a "node" element should look something like:
{'node': {'id': 757860928,
'user': 'uboot',
'uid': 26299,
'version': '2',
'lat': 41.9747374,
'lon': -87.6920102,
'timestamp': '2010-07-22T16:16:51Z',
'changeset': 5288876},
'node_tags': [{'id': 757860928,
'key': 'amenity',
'value': 'fast_food',
'type': 'regular'},
{'id': 757860928,
'key': 'cuisine',
'value': 'sausage',
'type': 'regular'},
{'id': 757860928,
'key': 'name',
'value': "Shelly's Tasty Freeze",
'type': 'regular'}]}
### If the element top level tag is "way":
The dictionary should have the format {"way": ..., "way_tags": ..., "way_nodes": ...}
The "way" field should hold a dictionary of the following top level way attributes:
- id
- user
- uid
- version
- timestamp
- changeset
All other attributes can be ignored
The "way_tags" field should again hold a list of dictionaries, following the exact same rules as
for "node_tags".
Additionally, the dictionary should have a field "way_nodes". "way_nodes" should hold a list of
dictionaries, one for each nd child tag. Each dictionary should have the fields:
- id: the top level element (way) id
- node_id: the ref attribute value of the nd tag
- position: the index starting at 0 of the nd tag i.e. what order the nd tag appears within
the way element
The final return value for a "way" element should look something like:
{'way': {'id': 209809850,
'user': 'chicago-buildings',
'uid': 674454,
'version': '1',
'timestamp': '2013-03-13T15:58:04Z',
'changeset': 15353317},
'way_nodes': [{'id': 209809850, 'node_id': 2199822281, 'position': 0},
{'id': 209809850, 'node_id': 2199822390, 'position': 1},
{'id': 209809850, 'node_id': 2199822392, 'position': 2},
{'id': 209809850, 'node_id': 2199822369, 'position': 3},
{'id': 209809850, 'node_id': 2199822370, 'position': 4},
{'id': 209809850, 'node_id': 2199822284, 'position': 5},
{'id': 209809850, 'node_id': 2199822281, 'position': 6}],
'way_tags': [{'id': 209809850,
'key': 'housenumber',
'type': 'addr',
'value': '1412'},
{'id': 209809850,
'key': 'street',
'type': 'addr',
'value': 'West Lexington St.'},
{'id': 209809850,
'key': 'street:name',
'type': 'addr',
'value': 'Lexington'},
{'id': '209809850',
'key': 'street:prefix',
'type': 'addr',
'value': 'West'},
{'id': 209809850,
'key': 'street:type',
'type': 'addr',
'value': 'Street'},
{'id': 209809850,
'key': 'building',
'type': 'regular',
'value': 'yes'},
{'id': 209809850,
'key': 'levels',
'type': 'building',
'value': '1'},
{'id': 209809850,
'key': 'building_id',
'type': 'chicago',
'value': '366409'}]}
"""
import csv
import codecs
import pprint
import re
import xml.etree.cElementTree as ET
import cerberus
import schema
# Importing the update functions and dictionaries from the audit scripts
# that we created
from audit_street_type import mapping, update_street_type
from audit_bus_stations import mapping_street_buses, update_bus_street_names
from audit_amenities import amenity_mapping, update_amenity
OSM_PATH = "liverpool_england.osm"
NODES_PATH = "nodes.csv"
NODE_TAGS_PATH = "nodes_tags.csv"
WAYS_PATH = "ways.csv"
WAY_NODES_PATH = "ways_nodes.csv"
WAY_TAGS_PATH = "ways_tags.csv"
LOWER_COLON = re.compile(r'^([A-Za-z]|_)+:([A-Za-z]|_)+')
PROBLEMCHARS = re.compile(r'[=\+/&<>;\'"\?%#$@\,\. \t\r\n]')
DOUBLE_COLON = re.compile(r'^([A-Za-z]|_)+:([A-Za-z]|_)+:([A-Za-z]|_)+')
SCHEMA = schema.schema
NODE_FIELDS = ['id', 'lat', 'lon', 'user', 'uid', 'version', 'changeset', 'timestamp']
NODE_TAGS_FIELDS = ['id', 'key', 'value', 'type']
WAY_FIELDS = ['id', 'user', 'uid', 'version', 'changeset', 'timestamp']
WAY_TAGS_FIELDS = ['id', 'key', 'value', 'type']
WAY_NODES_FIELDS = ['id', 'node_id', 'position']
def shape_element(element, node_attr_fields=NODE_FIELDS, way_attr_fields=WAY_FIELDS,
problem_chars=PROBLEMCHARS, default_tag_type='regular'):
"""Clean and shape node or way XML element to Python dict"""
node_attribs = {}
way_attribs = {}
way_nodes = []
node_tags = []
way_tags = []
# We are going to use the update functions wherever is needed according to the audits
# we completed
if element.tag == 'node':
for attrib, value in element.attrib.iteritems():
if attrib in node_attr_fields:
node_attribs[attrib] = value
for child in element.iter():
if child.tag == 'tag':
tag = {}
problem = PROBLEMCHARS.search(child.attrib['k'])
match_two_colons = DOUBLE_COLON.search(child.attrib['k'])
match_one_colon = LOWER_COLON.search(child.attrib['k'])
if problem:
continue
elif match_two_colons:
tag ['id'] = element.attrib['id']
tag ['type'] = child.attrib['k'].split(':')[0]
tag ['key'] = child.attrib['k'].split(':')[1] + ':' + child.attrib['k'].split(':')[2]
tag ['value'] = child.attrib['v']
elif match_one_colon:
tag ['id'] = element.attrib['id']
tag ['type'] = child.attrib['k'].split(':')[0]
tag ['key'] = child.attrib['k'].split(':')[1]
if child.attrib['k'] == "naptan:Street":
tag ['value'] = update_bus_street_names(child.attrib['v'], mapping_street_buses)
else:
tag ['value'] = child.attrib['v']
elif not match_one_colon:
tag ['id'] = element.attrib['id']
tag['type'] = 'regular'
tag ['key'] = child.attrib['k']
if child.attrib['k'] == "amenity":
tag ['value'] = update_amenity(child.attrib['v'], amenity_mapping)
else:
tag ['value'] = child.attrib['v']
node_tags.append(tag)
return {'node': node_attribs, 'node_tags': node_tags}
elif element.tag == 'way':
for attrib, value in element.attrib.iteritems():
if attrib in way_attr_fields:
way_attribs[attrib] = value
for child in element.iter():
if child.tag == 'tag':
way_tag = {}
problem = PROBLEMCHARS.search(child.attrib['k'])
match_two_colons = DOUBLE_COLON.search(child.attrib['k'])
match_one_colon = LOWER_COLON.search(child.attrib['k'])
if problem:
continue
elif match_two_colons:
way_tag ['id'] = element.attrib['id']
way_tag ['type'] = child.attrib['k'].split(':')[0]
way_tag ['key'] = child.attrib['k'].split(':')[1] + ':' + child.attrib['k'].split(':')[2]
way_tag ['value'] = child.attrib['v']
elif match_one_colon:
way_tag ['id'] = element.attrib['id']
way_tag ['type'] = child.attrib['k'].split(':')[0]
way_tag ['key'] = child.attrib['k'].split(':')[1]
if child.attrib['k'] == "addr:street":
way_tag ['value'] = update_street_type(child.attrib['v'], mapping)
else:
way_tag ['value'] = child.attrib['v']
elif not match_one_colon:
way_tag ['id'] = element.attrib['id']
way_tag['type'] = 'regular'
way_tag ['key'] = child.attrib['k']
if child.attrib['k'] == "amenity":
way_tag ['value'] = update_amenity(child.attrib['v'], amenity_mapping)
else:
way_tag ['value'] = child.attrib['v']
way_tags.append(way_tag)
counter = 0
for child in element.iter():
if child.tag == 'nd':
newnd = {}
newnd['id'] = element.attrib['id']
newnd['node_id'] = child.attrib['ref']
newnd['position'] = counter
counter += 1
way_nodes.append(newnd)
return {'way': way_attribs, 'way_nodes': way_nodes, 'way_tags': way_tags}
# ================================================== #
# Helper Functions #
# ================================================== #
def get_element(osm_file, tags=('node', 'way', 'relation')):
"""Yield element if it is the right type of tag"""
context = ET.iterparse(osm_file, events=('start', 'end'))
_, root = next(context)
for event, elem in context:
if event == 'end' and elem.tag in tags:
yield elem
root.clear()
def validate_element(element, validator, schema=SCHEMA):
"""Raise ValidationError if element does not match schema"""
if validator.validate(element, schema) is not True:
field, errors = next(validator.errors.iteritems())
message_string = "\nElement of type '{0}' has the following errors:\n{1}"
error_string = pprint.pformat(errors)
raise Exception(message_string.format(field, error_string))
class UnicodeDictWriter(csv.DictWriter, object):
"""Extend csv.DictWriter to handle Unicode input"""
def writerow(self, row):
super(UnicodeDictWriter, self).writerow({
k: (v.encode('utf-8') if isinstance(v, unicode) else v) for k, v in row.iteritems()
})
def writerows(self, rows):
for row in rows:
self.writerow(row)
# ================================================== #
# Main Function #
# ================================================== #
def process_map(file_in, validate):
"""Iteratively process each XML element and write to csv(s)"""
with codecs.open(NODES_PATH, 'w') as nodes_file, \
codecs.open(NODE_TAGS_PATH, 'w') as nodes_tags_file, \
codecs.open(WAYS_PATH, 'w') as ways_file, \
codecs.open(WAY_NODES_PATH, 'w') as way_nodes_file, \
codecs.open(WAY_TAGS_PATH, 'w') as way_tags_file:
nodes_writer = UnicodeDictWriter(nodes_file, NODE_FIELDS)
node_tags_writer = UnicodeDictWriter(nodes_tags_file, NODE_TAGS_FIELDS)
ways_writer = UnicodeDictWriter(ways_file, WAY_FIELDS)
way_nodes_writer = UnicodeDictWriter(way_nodes_file, WAY_NODES_FIELDS)
way_tags_writer = UnicodeDictWriter(way_tags_file, WAY_TAGS_FIELDS)
nodes_writer.writeheader()
node_tags_writer.writeheader()
ways_writer.writeheader()
way_nodes_writer.writeheader()
way_tags_writer.writeheader()
validator = cerberus.Validator()
for element in get_element(file_in, tags=('node', 'way')):
el = shape_element(element)
if el:
if validate is True:
validate_element(el, validator)
if element.tag == 'node':
nodes_writer.writerow(el['node'])
node_tags_writer.writerows(el['node_tags'])
elif element.tag == 'way':
ways_writer.writerow(el['way'])
way_nodes_writer.writerows(el['way_nodes'])
way_tags_writer.writerows(el['way_tags'])
if __name__ == '__main__':
# Note: Validation is ~ 10X slower. For the project consider using a small
# sample of the map when validating.
process_map(OSM_PATH, validate=False)