Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xml.etree.ElementTree as ET
- from xml.etree.ElementTree import Element
- def get_node_name(tags_elems, find_tag):
- # f.e. <tag k="information" v="guidepost"/>
- for t in tags_elems:
- if t.get('k') == find_tag:
- return t.get('v')
- def parse_map_points(root):
- f = open("../data-insert-points.sql", "a", encoding="utf-8")
- f.write("set define off;")
- inserted_places = []
- skip_places_tags = ['hamlet', 'town', 'village', 'suburb', 'quarter', 'traffic_sign']
- unique_tags = []
- for elem in root.findall('node'):
- skip_node = False
- tags = elem.findall('tag')
- name = ''
- place_type = ''
- for t in tags:
- k_tag = t.get('k')
- v_tag = t.get('v')
- if k_tag == 'place' and v_tag in skip_places_tags: # skip some places
- skip_node = True
- break
- elif k_tag == 'place':
- place_type = v_tag
- elif k_tag == 'tourism':
- place_type = get_node_name(tags, 'information')
- place_type = v_tag if place_type is None else place_type
- elif k_tag in ['amenity', 'natural', 'highway', 'barrier', 'shop', 'railway']:
- place_type = v_tag
- if k_tag == 'name':
- name = v_tag
- if skip_node or not tags or name == '' or place_type == '':
- continue
- lon = elem.get('lon')
- lat = elem.get('lat')
- if [place_type, name] not in inserted_places:
- inserted_places.append([place_type, name])
- insert = f"INSERT INTO MAP_OBJECT(type, NAME, geometry) VALUES(\'{place_type}\', \'{name}\', "\
- f"SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE({lon}, {lat}, NULL), NULL, NULL));\n"
- f.write(insert)
- f.close()
- def get_coords_str(coords):
- str = ''
- i = 0
- for c in coords:
- i+=1
- str += f" {c['lon']},{c['lat']} ,"
- if i % 3 == 0:
- str += "\n"
- # print(str[:-1])
- return str[:-2]
- def print_insert_polygon(f, coords, type, name):
- insert = f"declare\n"\
- f"GEOMETRY mdsys.sdo_geometry :=\n" \
- f"mdsys.sdo_geometry(2003, 8307, null,\n" \
- f"mdsys.sdo_elem_info_array(1, 1003, 1),\n" \
- f"mdsys.sdo_ordinate_array(\n" \
- f"{get_coords_str(coords)}"\
- f"));\n" \
- f"BEGIN\n" \
- f"INSERT_SUBAREA(\n" \
- f" 'subarea',\n" \
- f" \'{name}\',\n" \
- f" GEOMETRY\n" \
- f");\n" \
- f"COMMIT;\n" \
- f"END;\n" \
- f"/\n\n\n"
- print(insert)
- f.write(insert)
- def print_insert_lines(f, coords, type, name):
- insert = f"INSERT INTO MAP_OBJECT (type, name, geometry) VALUES (\'{type}\', \'{name}\', " \
- f"SDO_GEOMETRY(2002, 8307, NULL," \
- f"SDO_ELEM_INFO_ARRAY(1,2,1)," \
- f"SDO_ORDINATE_ARRAY({get_coords_str(coords)})));\n"
- f.write(insert)
- def parse_map_lines(root):
- f_poly = open("../data-insert-polygons.sql", "a", encoding="utf-8")
- f_lines = open("../data-insert-lines.sql", "a", encoding="utf-8")
- inserted_places = []
- unique_tags = []
- nodes = root.findall('node')
- way_elems = root.findall('way')
- for idx, elem in enumerate(way_elems, start=1):
- skip_elem = False
- name = ''
- type = ''
- way_tags = ''
- tags = elem.findall('tag')
- is_polygon = True
- if not tags:
- skip_elem = True
- for tag in tags:
- k_tag = tag.get('k')
- v_tag = tag.get('v')
- if k_tag in ['water', 'amenity', 'waterway', 'pitch', 'leisure', 'historic']: #landuse
- type = v_tag
- if v_tag == 'river':
- is_polygon = False
- elif k_tag == 'name':
- name = v_tag
- elif k_tag == 'highway' or k_tag == 'boundary':
- if not v_tag == 'administrative':
- type = f"{v_tag}_{k_tag}"
- if k_tag == 'highway':
- is_polygon = False
- elif k_tag == 'railway':
- type = k_tag
- is_polygon = False
- elif k_tag == 'building':
- skip_elem = True
- # break
- # name = v_tag + get_node_name(tags, '')
- elif k_tag == 'suburb':
- print('suburb')
- way_tags += f" {tag.get('k')}:{tag.get('v')}\t"
- # if 'building' not in way_tags and 'highway' not in way_tags and 'landuse' not in way_tags and 'water' not in way_tags\
- # and 'barrier' not in way_tags and 'boundary' not in way_tags and 'historic' not in way_tags\
- # and 'leisure' not in way_tags and 'amenity' not in way_tags and 'power' not in way_tags\
- # and 'natural' not in way_tags:
- # print(way_tags)
- if not skip_elem: # get way coords
- coords = []
- way_nodes_ids = [n.get('ref') for n in elem.findall('nd')]
- for node_id in way_nodes_ids:
- node = root.find(f'./node[@id="{node_id}"]')
- coords.append({'lon': node.get('lon'), 'lat': node.get('lat')})
- else: # is way of lines
- print_insert_lines(f_lines, coords, type, name)
- print(f'Insert printed ... {idx}/{len(way_elems)}')
- def parse_map_polygons(root):
- f_poly = open("../data-insert-polygons.sql", "a", encoding="utf-8")
- procedure = f"CREATE OR REPLACE PROCEDURE\n" \
- f"\tINSERT_SUBAREA(TYPE VARCHAR2, NAME VARCHAR2, GEOMETRY MDSYS.SDO_GEOMETRY) IS\n" \
- f"BEGIN\n" \
- f"\tINSERT INTO MAP_OBJECT (TYPE, NAME, GEOMETRY) VALUES (TYPE, NAME, GEOMETRY);\n" \
- f"COMMIT;\n" \
- f"END;\n" \
- f"/\n\n"
- f_poly.write(procedure)
- relations = root.findall('relation')
- ids = [str(i) for i in [1551612, 1557453, 1557451, 1557454]]
- other_ids = [str(i) for i in [2189518, 2189519, 2189521, 2189555, 2189522, 2189523, 2189524, 2189525,
- 2189526, 2189520, 2189527, 2189558, 2189528, 2189550, 2189529, 2189530, 1557452,
- 2189531, 2189532, 2189533, 2189534, 2189536, 2189535, 2189537, 2189538, 2189539,
- 2189540, 2189560, 2189551, 2189541, 2189543, 2189556, 2189557,
- 2189559, 2189545, 2189544, 2189546, ]]
- r: Element
- # for r_id in ids+other_ids:
- for r_id in [423676,]:
- # for r_id in [1551612, ]:
- # if r_id in done:
- # continue
- print(f"relation_id {r_id}")
- r = root.find(f'./relation[@id="{r_id}"]')
- coords = []
- name = ''
- members = r.findall("member")
- tags = r.findall("tag")
- way_ids = []
- ways = []
- BEGINNING = None
- for m in members:
- if m.attrib['type'] == 'way':
- ways.append(m)
- for i, m in enumerate(ways):
- way = root.find(f'./way[@id="{m.attrib["ref"]}"]')
- print(f"way_id {m.attrib['ref']}")
- way_nodes_ids = [n.attrib.get('ref') for n in way.findall('nd')]
- way_coords = []
- if i + 1 < len(ways):
- next_way = root.find(f'./way[@id="{ways[i + 1].attrib["ref"]}"]')
- next_ids = [n.attrib.get('ref') for n in next_way.findall('nd')]
- next_beginning = next_ids[0]
- next_finish = next_ids[len(next_ids) - 1]
- print("------------------------------")
- print(f"NEXT B node_id {next_beginning}")
- print(f"NEXT F node_id {next_finish}")
- actual_beginning = way_nodes_ids[0]
- actual_finish = way_nodes_ids[len(way_nodes_ids) - 1]
- print(f"ACT B node_id {actual_beginning}")
- print(f"ACT F node_id {actual_finish}")
- else:
- next_beginning = BEGINNING
- actual_finish = way_nodes_ids[len(way_nodes_ids) - 1]
- actual_beginning = way_nodes_ids[0]
- print(f"NEXT B node_id {next_beginning}")
- print(f"ACT F node_id {actual_finish}")
- if next_beginning == actual_beginning:
- way_nodes_ids.reverse()
- if actual_finish == next_finish:
- pass
- elif actual_finish != next_beginning:
- print("REVERSEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
- way_nodes_ids.reverse()
- if not BEGINNING:
- BEGINNING = way_nodes_ids[0]
- for node_id in way_nodes_ids:
- # print(f"node_id {node_id}")
- node = root.find(f'./node[@id="{node_id}"]')
- way_coords.append({'lon': node.get('lon'), 'lat': node.get('lat')})
- # way_coords.reverse()
- coords += way_coords
- for t in tags:
- if t.attrib['k'] == 'name':
- name = t.attrib['v']
- print(name)
- break
- if name != '' and coords:
- print_insert_polygon(f_poly, coords, "subarea", name)
- if __name__ == "__main__":
- root = ET.parse('brnoTraining.xml').getroot()
- # parse_map_points(root)
- # parse_map_lines(root)
- parse_map_polygons(root) #suburbs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement