Created
March 1, 2025 17:45
-
-
Save spinningcat/bb5b4f73736e835aa4ca2354a1eacede to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as ET | |
osm_file = 'outputfile2.osm' | |
root = tree.getroot() | |
target_relation_id = '18765511' | |
def find_relation_by_id(root, relation_id): | |
for relation in root.findall('relation'): | |
if relation.attrib.get('id') == relation_id: | |
return relation | |
return None | |
relation = find_relation_by_id(root, target_relation_id) | |
if relation is None: | |
print(f"Relation with ID {target_relation_id} not found.") | |
else: | |
tags = {} | |
for tag in relation.findall('tag'): | |
tags[tag.attrib['k']] = tag.attrib['v'] | |
members = [] | |
for member in relation.findall('member'): | |
members.append({ | |
'type': member.attrib['type'], | |
'ref': member.attrib['ref'], | |
'role': member.attrib.get('role', '') | |
}) | |
coordinates = [] | |
for member in members: | |
if member['type'] == 'node': | |
node_id = member['ref'] | |
node = root.find(f".//node[@id='{node_id}']") | |
if node is not None: | |
coordinates.append({ | |
'lon': float(node.attrib['lon']), | |
'lat': float(node.attrib['lat']) | |
}) | |
print("Tags:", tags) | |
print("Members:", members) | |
print("Coordinates:", coordinates) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment