Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.32 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2. from xml.etree.ElementTree import Element
  3.  
  4.  
  5. def get_node_name(tags_elems, find_tag):
  6.     # f.e. <tag k="information" v="guidepost"/>
  7.     for t in tags_elems:
  8.         if t.get('k') == find_tag:
  9.             return t.get('v')
  10.  
  11.  
  12. def parse_map_points(root):
  13.     f = open("../data-insert-points.sql", "a", encoding="utf-8")
  14.     f.write("set define off;")
  15.  
  16.     inserted_places = []
  17.     skip_places_tags = ['hamlet', 'town', 'village', 'suburb', 'quarter', 'traffic_sign']
  18.     unique_tags = []
  19.  
  20.     for elem in root.findall('node'):
  21.         skip_node = False
  22.         tags = elem.findall('tag')
  23.         name = ''
  24.         place_type = ''
  25.  
  26.         for t in tags:
  27.             k_tag = t.get('k')
  28.             v_tag = t.get('v')
  29.  
  30.             if k_tag == 'place' and v_tag in skip_places_tags:  # skip some places
  31.                 skip_node = True
  32.                 break
  33.             elif k_tag == 'place':
  34.                 place_type = v_tag
  35.             elif k_tag == 'tourism':
  36.                 place_type = get_node_name(tags, 'information')
  37.                 place_type = v_tag if place_type is None else place_type
  38.             elif k_tag in ['amenity', 'natural', 'highway', 'barrier', 'shop', 'railway']:
  39.                 place_type = v_tag
  40.  
  41.             if k_tag == 'name':
  42.                 name = v_tag
  43.  
  44.         if skip_node or not tags or name == '' or place_type == '':
  45.             continue
  46.  
  47.         lon = elem.get('lon')
  48.         lat = elem.get('lat')
  49.         if [place_type, name] not in inserted_places:
  50.             inserted_places.append([place_type, name])
  51.             insert = f"INSERT INTO MAP_OBJECT(type, NAME, geometry) VALUES(\'{place_type}\', \'{name}\', "\
  52.                      f"SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE({lon}, {lat}, NULL), NULL, NULL));\n"
  53.             f.write(insert)
  54.  
  55.     f.close()
  56.  
  57.  
  58. def get_coords_str(coords):
  59.     str = ''
  60.     for c in coords:
  61.         str += f" {c['lon']},{c['lat']} ,"
  62.     # print(str[:-1])
  63.     return str[:-1]
  64.  
  65.  
  66. def print_insert_polygon(f, coords, type, name):
  67.     insert = f"INSERT INTO MAP_OBJECT (type, name, geometry) VALUES (\'{type}\', \'{name}\', " \
  68.              f"SDO_GEOMETRY(2003, 8307, NULL," \
  69.              f"SDO_ELEM_INFO_ARRAY(1,1003, 1)," \
  70.              f"SDO_ORDINATE_ARRAY({get_coords_str(coords)})));\n"
  71.     f.write(insert)
  72.  
  73.  
  74. def print_insert_lines(f, coords, type, name):
  75.     insert = f"INSERT INTO MAP_OBJECT (type, name, geometry) VALUES (\'{type}\', \'{name}\', " \
  76.              f"SDO_GEOMETRY(2002, 8307, NULL," \
  77.              f"SDO_ELEM_INFO_ARRAY(1,2,1)," \
  78.              f"SDO_ORDINATE_ARRAY({get_coords_str(coords)})));\n"
  79.     f.write(insert)
  80.  
  81.  
  82. def parse_map_lines(root):
  83.     f_poly = open("../data-insert-polygons.sql", "a", encoding="utf-8")
  84.     f_lines = open("../data-insert-lines.sql", "a", encoding="utf-8")
  85.     inserted_places = []
  86.     unique_tags = []
  87.     nodes = root.findall('node')
  88.  
  89.     way_elems = root.findall('way')
  90.     for idx, elem in enumerate(way_elems, start=1):
  91.         skip_elem = False
  92.         name = ''
  93.         type = ''
  94.         way_tags = ''
  95.         tags = elem.findall('tag')
  96.         is_polygon = True
  97.         if not tags:
  98.             skip_elem = True
  99.         for tag in tags:
  100.             k_tag = tag.get('k')
  101.             v_tag = tag.get('v')
  102.             if k_tag in ['water', 'amenity', 'waterway', 'pitch', 'leisure', 'historic']:   #landuse
  103.                 type = v_tag
  104.                 if v_tag == 'river':
  105.                     is_polygon = False
  106.             elif k_tag == 'name':
  107.                 name = v_tag
  108.             elif k_tag == 'highway' or k_tag == 'boundary':
  109.                 if not v_tag == 'administrative':
  110.                     type = f"{v_tag}_{k_tag}"
  111.                 if k_tag == 'highway':
  112.                     is_polygon = False
  113.             elif k_tag == 'railway':
  114.                 type = k_tag
  115.                 is_polygon = False
  116.             elif k_tag == 'building':
  117.                 skip_elem = True
  118.                 # break
  119.                 # name = v_tag + get_node_name(tags, '')
  120.             elif k_tag == 'suburb':
  121.                 print('suburb')
  122.             way_tags += f" {tag.get('k')}:{tag.get('v')}\t"
  123.  
  124.         # if 'building' not in way_tags and 'highway' not in way_tags and 'landuse' not in way_tags and 'water' not in way_tags\
  125.         #         and 'barrier' not in way_tags and 'boundary' not in way_tags and 'historic' not in way_tags\
  126.         #         and 'leisure' not in way_tags and 'amenity' not in way_tags and 'power' not in way_tags\
  127.         #         and 'natural' not in way_tags:
  128.         #     print(way_tags)
  129.  
  130.         if not skip_elem:  # get way coords
  131.             coords = []
  132.             way_nodes_ids = [n.get('ref') for n in elem.findall('nd')]
  133.             way_nodes = []
  134.             for node_id in way_nodes_ids:
  135.                 node = root.find(f'./node[@id="{node_id}"]')
  136.                 coords.append({'lon': node.get('lon'), 'lat': node.get('lat')})
  137.  
  138.             else:   # is way of lines
  139.                 print_insert_lines(f_lines, coords, type, name)
  140.             print(f'Insert printed ... {idx}/{len(way_elems)}')
  141.  
  142.  
  143. def parse_map_polygons(root):
  144.     f_poly = open("../data-insert-polygons.sql", "a", encoding="utf-8")
  145.  
  146.     relations = root.findall('relation')
  147.     ids = [str(i) for i in [1551612, 1557453, 1557451, 1557454]]
  148.     other_ids = [str(i) for i in [2189518, 2189519, 2189521, 2189549, 2189555, 2189522, 2189523, 2189524, 2189525,
  149.                                   2189526, 2189520, 2189527, 2189558, 2189528, 2189550, 2189529, 2189530, 1557452,
  150.                                   2189531, 2189532, 2189533, 2189534, 2189536, 2189535, 2189537, 2189538, 2189539,
  151.                                   2189540, 2189560, 2220715, 2189553, 2189551, 2189541, 2189543, 2189556, 2189557,
  152.                                   2189559, 2189545, 2189544, 2189546, 2189548, 2189554]]
  153.     r: Element
  154.     for r in relations:
  155.         if r.attrib['id'] in ids+other_ids:
  156.             members = r.findall("member")
  157.             tags = []
  158.             for elem in r:
  159.  
  160.             if not type and not coords:
  161.                 print_insert_polygon(f_poly, coords, "subarea", name)
  162.  
  163.  
  164. if __name__ == "__main__":
  165.     root = ET.parse('map').getroot()
  166.     # parse_map_points(root)
  167.     # parse_map_lines(root)
  168.     parse_map_polygons(root)  #suburbs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement