Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1.  
  2. # Make sure the fields order in the csvs matches the column order in the sql table schema
  3. NODE_FIELDS = ['id', 'lat', 'lon', 'user', 'uid', 'version', 'changeset', 'timestamp']
  4. NODE_TAGS_FIELDS = ['id', 'key', 'value', 'type']
  5. WAY_FIELDS = ['id', 'user', 'uid', 'version', 'changeset', 'timestamp']
  6. WAY_TAGS_FIELDS = ['id', 'key', 'value', 'type']
  7. WAY_NODES_FIELDS = ['id', 'node_id', 'position']
  8.  
  9.  
  10. def shape_element(element, node_attr_fields=NODE_FIELDS, way_attr_fields=WAY_FIELDS,
  11.                   problem_chars=PROBLEMCHARS, default_tag_type='regular'):
  12.     """Clean and shape node or way XML element to Python dict"""
  13.  
  14.     node_attribs = {}
  15.     way_attribs = {}
  16.     way_nodes = []
  17.     tags = []  # Handle secondary tags the same way for both node and way elements
  18.  
  19.     # YOUR CODE HERE
  20.     if element.tag == 'node':
  21.         node_atts = element.attrib
  22.         node_children = element.getchildren()
  23.         for field in node_attr_fields:
  24.             node_attribs[field] = node_atts[field]
  25.         if len(node_children) == 0:
  26.             tags = []
  27.         else:
  28.             for child in node_children:
  29.                 child_dict = {}
  30.                 child_atts = child.attrib
  31.                 for field in NODE_TAGS_FIELDS:
  32.                     if field == 'id':
  33.                         child_dict[field] = node_atts[field]
  34.                     elif field == 'key':
  35.                         if ':' in child_atts['k']:
  36.                             pattern1 = re.compile("(.*):")
  37.                             pattern2 = re.compile(":(.*)")
  38.                             search1 = pattern1.search(child_atts['k'])
  39.                             search2 = pattern2.search(child_atts['k'])
  40.                             child_dict[field] = search2.group(1)
  41.                             child_dict['type'] = search1.group(1)
  42.                         else:
  43.                             child_dict[field] = child_atts['k']
  44.                             child_dict['type'] = 'regular'
  45.                     elif field == 'value':
  46.                         child_dict[field] = child_atts['v']
  47.                 tags.append(child_dict)
  48.         return {'node': node_attribs, 'node_tags': tags}
  49.     elif element.tag == 'way':
  50.         way_atts = element.attrib
  51.         way_children = element.getchildren()
  52.         #print 'These be way children: ' + str(way_children)
  53.         for field in way_attr_fields:
  54.             way_attribs[field] = way_atts[field]
  55.         if len(way_children) == 0:
  56.             tags = []
  57.             way_nodes = []
  58.         else:
  59.             counter = 0
  60.             for child in way_children:
  61.                 if child.tag == 'tag':
  62.                     child_dict = {}
  63.                     child_atts = child.attrib
  64.                    
  65.                     for field in WAY_TAGS_FIELDS:
  66.                         if field == 'id':
  67.                             child_dict[field] = way_atts[field]
  68.                         elif field == 'key':
  69.                             if ':' in child_atts['k']:
  70.                                 pattern1 = re.compile("(.*):")
  71.                                 pattern2 = re.compile(":(.*)")
  72.                                 search1 = pattern1.search(child_atts['k'])
  73.                                 search2 = pattern2.search(child_atts['k'])
  74.                                 child_dict[field] = search2.group(1)
  75.                                 child_dict['type'] = search1.group(1)
  76.                             else:
  77.                                 child_dict[field] = child_atts['k']
  78.                                 child_dict['type'] = 'regular'
  79.                         elif field == 'value':
  80.                             child_dict[field] = child_atts['v']
  81.                     tags.append(child_dict)
  82.                 elif child.tag == 'nd':
  83.                     child_dict = {}
  84.                     child_atts = child.attrib
  85.                     for field in WAY_NODES_FIELDS:
  86.                         if field == 'id':
  87.                             child_dict[field] = way_atts[field]
  88.                         elif field == 'node_id':
  89.                             child_dict[field] = child_atts['ref']
  90.                         else:
  91.                             child_dict[field] = counter
  92.                             counter += 1
  93.                     way_nodes.append(child_dict)
  94.         #print 'THIS BE WAY TAGS: ' + str(tags) + 'with length ' + str(len(tags))
  95.         #print 'THIS BE WAY nodes: ' + str(way_nodes) + 'with length ' + str(len(way_nodes))
  96.         #print 'NUmeber of children: ' + str(len(way_children)) + 'sum of tags and nodes is: ' + str(len(tags)+len(way_nodes))
  97.         return {'way': way_attribs, 'way_nodes': way_nodes, 'way_tags': tags}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement