Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Make sure the fields order in the csvs matches the column order in the sql table 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 = []
- tags = [] # Handle secondary tags the same way for both node and way elements
- # YOUR CODE HERE
- if element.tag == 'node':
- node_atts = element.attrib
- node_children = element.getchildren()
- for field in node_attr_fields:
- node_attribs[field] = node_atts[field]
- if len(node_children) == 0:
- tags = []
- else:
- for child in node_children:
- child_dict = {}
- child_atts = child.attrib
- for field in NODE_TAGS_FIELDS:
- if field == 'id':
- child_dict[field] = node_atts[field]
- elif field == 'key':
- if ':' in child_atts['k']:
- pattern1 = re.compile("(.*):")
- pattern2 = re.compile(":(.*)")
- search1 = pattern1.search(child_atts['k'])
- search2 = pattern2.search(child_atts['k'])
- child_dict[field] = search2.group(1)
- child_dict['type'] = search1.group(1)
- else:
- child_dict[field] = child_atts['k']
- child_dict['type'] = 'regular'
- elif field == 'value':
- child_dict[field] = child_atts['v']
- tags.append(child_dict)
- return {'node': node_attribs, 'node_tags': tags}
- elif element.tag == 'way':
- way_atts = element.attrib
- way_children = element.getchildren()
- #print 'These be way children: ' + str(way_children)
- for field in way_attr_fields:
- way_attribs[field] = way_atts[field]
- if len(way_children) == 0:
- tags = []
- way_nodes = []
- else:
- counter = 0
- for child in way_children:
- if child.tag == 'tag':
- child_dict = {}
- child_atts = child.attrib
- for field in WAY_TAGS_FIELDS:
- if field == 'id':
- child_dict[field] = way_atts[field]
- elif field == 'key':
- if ':' in child_atts['k']:
- pattern1 = re.compile("(.*):")
- pattern2 = re.compile(":(.*)")
- search1 = pattern1.search(child_atts['k'])
- search2 = pattern2.search(child_atts['k'])
- child_dict[field] = search2.group(1)
- child_dict['type'] = search1.group(1)
- else:
- child_dict[field] = child_atts['k']
- child_dict['type'] = 'regular'
- elif field == 'value':
- child_dict[field] = child_atts['v']
- tags.append(child_dict)
- elif child.tag == 'nd':
- child_dict = {}
- child_atts = child.attrib
- for field in WAY_NODES_FIELDS:
- if field == 'id':
- child_dict[field] = way_atts[field]
- elif field == 'node_id':
- child_dict[field] = child_atts['ref']
- else:
- child_dict[field] = counter
- counter += 1
- way_nodes.append(child_dict)
- #print 'THIS BE WAY TAGS: ' + str(tags) + 'with length ' + str(len(tags))
- #print 'THIS BE WAY nodes: ' + str(way_nodes) + 'with length ' + str(len(way_nodes))
- #print 'NUmeber of children: ' + str(len(way_children)) + 'sum of tags and nodes is: ' + str(len(tags)+len(way_nodes))
- return {'way': way_attribs, 'way_nodes': way_nodes, 'way_tags': tags}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement