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 = ''
- for c in coords:
- str += f" {c['lon']},{c['lat']} ,"
- # print(str[:-1])
- return str[:-1]
- def print_insert_polygon(f, coords, type, name):
- insert = f"INSERT INTO MAP_OBJECT (type, name, geometry) VALUES (\'{type}\', \'{name}\', " \
- f"SDO_GEOMETRY(2003, 8307, NULL," \
- f"SDO_ELEM_INFO_ARRAY(1,1003, 1)," \
- f"SDO_ORDINATE_ARRAY({get_coords_str(coords)})));\n"
- 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')]
- way_nodes = []
- 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")
- relations = root.findall('relation')
- ids = [str(i) for i in [1551612, 1557453, 1557451, 1557454]]
- other_ids = [str(i) for i in [2189518, 2189519, 2189521, 2189549, 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, 2220715, 2189553, 2189551, 2189541, 2189543, 2189556, 2189557,
- 2189559, 2189545, 2189544, 2189546, 2189548, 2189554]]
- r: Element
- for r in relations:
- if r.attrib['id'] in ids+other_ids:
- members = r.findall("member")
- tags = []
- for elem in r:
- if not type and not coords:
- print_insert_polygon(f_poly, coords, "subarea", name)
- if __name__ == "__main__":
- root = ET.parse('map').getroot()
- # parse_map_points(root)
- # parse_map_lines(root)
- parse_map_polygons(root) #suburbs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement