Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.51 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.     i = 0
  61.     for c in coords:
  62.         i+=1
  63.         str += f" {c['lon']},{c['lat']} ,"
  64.         if i % 3 == 0:
  65.             str += "\n"
  66.     # print(str[:-1])
  67.     return str[:-2]
  68.  
  69.  
  70. def print_insert_polygon(f, coords, type, name):
  71.     insert = f"declare\n"\
  72.              f"GEOMETRY mdsys.sdo_geometry :=\n" \
  73.              f"mdsys.sdo_geometry(2003, 8307, null,\n" \
  74.              f"mdsys.sdo_elem_info_array(1, 1003, 1),\n" \
  75.              f"mdsys.sdo_ordinate_array(\n" \
  76.              f"{get_coords_str(coords)}"\
  77.              f"));\n" \
  78.              f"BEGIN\n" \
  79.              f"INSERT_SUBAREA(\n" \
  80.              f"    'subarea',\n" \
  81.              f"    \'{name}\',\n" \
  82.              f"    GEOMETRY\n" \
  83.              f");\n" \
  84.              f"COMMIT;\n" \
  85.              f"END;\n" \
  86.              f"/\n\n\n"
  87.     print(insert)
  88.     f.write(insert)
  89.  
  90.  
  91. def print_insert_lines(f, coords, type, name):
  92.     insert = f"INSERT INTO MAP_OBJECT (type, name, geometry) VALUES (\'{type}\', \'{name}\', " \
  93.              f"SDO_GEOMETRY(2002, 8307, NULL," \
  94.              f"SDO_ELEM_INFO_ARRAY(1,2,1)," \
  95.              f"SDO_ORDINATE_ARRAY({get_coords_str(coords)})));\n"
  96.     f.write(insert)
  97.  
  98.  
  99. def parse_map_lines(root):
  100.     f_poly = open("../data-insert-polygons.sql", "a", encoding="utf-8")
  101.     f_lines = open("../data-insert-lines.sql", "a", encoding="utf-8")
  102.     inserted_places = []
  103.     unique_tags = []
  104.     nodes = root.findall('node')
  105.  
  106.     way_elems = root.findall('way')
  107.     for idx, elem in enumerate(way_elems, start=1):
  108.         skip_elem = False
  109.         name = ''
  110.         type = ''
  111.         way_tags = ''
  112.         tags = elem.findall('tag')
  113.         is_polygon = True
  114.         if not tags:
  115.             skip_elem = True
  116.         for tag in tags:
  117.             k_tag = tag.get('k')
  118.             v_tag = tag.get('v')
  119.             if k_tag in ['water', 'amenity', 'waterway', 'pitch', 'leisure', 'historic']:   #landuse
  120.                 type = v_tag
  121.                 if v_tag == 'river':
  122.                     is_polygon = False
  123.             elif k_tag == 'name':
  124.                 name = v_tag
  125.             elif k_tag == 'highway' or k_tag == 'boundary':
  126.                 if not v_tag == 'administrative':
  127.                     type = f"{v_tag}_{k_tag}"
  128.                 if k_tag == 'highway':
  129.                     is_polygon = False
  130.             elif k_tag == 'railway':
  131.                 type = k_tag
  132.                 is_polygon = False
  133.             elif k_tag == 'building':
  134.                 skip_elem = True
  135.                 # break
  136.                 # name = v_tag + get_node_name(tags, '')
  137.             elif k_tag == 'suburb':
  138.                 print('suburb')
  139.             way_tags += f" {tag.get('k')}:{tag.get('v')}\t"
  140.  
  141.         # if 'building' not in way_tags and 'highway' not in way_tags and 'landuse' not in way_tags and 'water' not in way_tags\
  142.         #         and 'barrier' not in way_tags and 'boundary' not in way_tags and 'historic' not in way_tags\
  143.         #         and 'leisure' not in way_tags and 'amenity' not in way_tags and 'power' not in way_tags\
  144.         #         and 'natural' not in way_tags:
  145.         #     print(way_tags)
  146.  
  147.         if not skip_elem:  # get way coords
  148.             coords = []
  149.             way_nodes_ids = [n.get('ref') for n in elem.findall('nd')]
  150.             for node_id in way_nodes_ids:
  151.                 node = root.find(f'./node[@id="{node_id}"]')
  152.                 coords.append({'lon': node.get('lon'), 'lat': node.get('lat')})
  153.  
  154.             else:   # is way of lines
  155.                 print_insert_lines(f_lines, coords, type, name)
  156.             print(f'Insert printed ... {idx}/{len(way_elems)}')
  157.  
  158.  
  159. def parse_map_polygons(root):
  160.     f_poly = open("../data-insert-polygons.sql", "a", encoding="utf-8")
  161.     procedure = f"CREATE OR REPLACE PROCEDURE\n" \
  162.                 f"\tINSERT_SUBAREA(TYPE VARCHAR2, NAME VARCHAR2, GEOMETRY MDSYS.SDO_GEOMETRY) IS\n" \
  163.                 f"BEGIN\n" \
  164.                 f"\tINSERT INTO MAP_OBJECT (TYPE, NAME, GEOMETRY) VALUES (TYPE, NAME, GEOMETRY);\n" \
  165.                 f"COMMIT;\n" \
  166.                 f"END;\n" \
  167.                 f"/\n\n"
  168.     f_poly.write(procedure)
  169.  
  170.     relations = root.findall('relation')
  171.     ids = [str(i) for i in [1551612, 1557453, 1557451, 1557454]]
  172.     other_ids = [str(i) for i in [2189518, 2189519, 2189521, 2189555, 2189522, 2189523, 2189524, 2189525,
  173.                                   2189526, 2189520, 2189527, 2189558, 2189528, 2189550, 2189529, 2189530, 1557452,
  174.                                   2189531, 2189532, 2189533, 2189534, 2189536, 2189535, 2189537, 2189538, 2189539,
  175.                                   2189540, 2189560, 2189551, 2189541, 2189543, 2189556, 2189557,
  176.                                   2189559, 2189545, 2189544, 2189546, ]]
  177.  
  178.     r: Element
  179.     # for r_id in ids+other_ids:
  180.     for r_id in [423676,]:
  181.     # for r_id in [1551612, ]:
  182.         # if r_id in done:
  183.         #     continue
  184.         print(f"relation_id {r_id}")
  185.         r = root.find(f'./relation[@id="{r_id}"]')
  186.         coords = []
  187.         name = ''
  188.         members = r.findall("member")
  189.         tags = r.findall("tag")
  190.         way_ids = []
  191.         ways = []
  192.         BEGINNING = None
  193.         for m in members:
  194.             if m.attrib['type'] == 'way':
  195.                 ways.append(m)
  196.         for i, m in enumerate(ways):
  197.             way = root.find(f'./way[@id="{m.attrib["ref"]}"]')
  198.  
  199.  
  200.             print(f"way_id {m.attrib['ref']}")
  201.             way_nodes_ids = [n.attrib.get('ref') for n in way.findall('nd')]
  202.             way_coords = []
  203.  
  204.             if i + 1 < len(ways):
  205.                 next_way = root.find(f'./way[@id="{ways[i + 1].attrib["ref"]}"]')
  206.                 next_ids = [n.attrib.get('ref') for n in next_way.findall('nd')]
  207.                 next_beginning = next_ids[0]
  208.                 next_finish = next_ids[len(next_ids) - 1]
  209.                 print("------------------------------")
  210.                 print(f"NEXT B node_id {next_beginning}")
  211.                 print(f"NEXT F node_id {next_finish}")
  212.  
  213.                 actual_beginning = way_nodes_ids[0]
  214.                 actual_finish = way_nodes_ids[len(way_nodes_ids) - 1]
  215.                 print(f"ACT B node_id {actual_beginning}")
  216.                 print(f"ACT F node_id {actual_finish}")
  217.  
  218.             else:
  219.                 next_beginning = BEGINNING
  220.                 actual_finish = way_nodes_ids[len(way_nodes_ids) - 1]
  221.                 actual_beginning = way_nodes_ids[0]
  222.                 print(f"NEXT B node_id {next_beginning}")
  223.                 print(f"ACT F node_id {actual_finish}")
  224.                 if next_beginning == actual_beginning:
  225.                     way_nodes_ids.reverse()
  226.  
  227.             if actual_finish == next_finish:
  228.                 pass
  229.             elif actual_finish != next_beginning:
  230.                 print("REVERSEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
  231.                 way_nodes_ids.reverse()
  232.  
  233.             if not BEGINNING:
  234.                 BEGINNING = way_nodes_ids[0]
  235.  
  236.             for node_id in way_nodes_ids:
  237.                 # print(f"node_id {node_id}")
  238.                 node = root.find(f'./node[@id="{node_id}"]')
  239.                 way_coords.append({'lon': node.get('lon'), 'lat': node.get('lat')})
  240.             # way_coords.reverse()
  241.             coords += way_coords
  242.         for t in tags:
  243.             if t.attrib['k'] == 'name':
  244.                 name = t.attrib['v']
  245.                 print(name)
  246.                 break
  247.  
  248.         if name != '' and coords:
  249.             print_insert_polygon(f_poly, coords, "subarea", name)
  250.  
  251.  
  252.  
  253.  
  254. if __name__ == "__main__":
  255.     root = ET.parse('brnoTraining.xml').getroot()
  256.     # parse_map_points(root)
  257.     # parse_map_lines(root)
  258.     parse_map_polygons(root)  #suburbs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement