]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Introduce DeRefError exception when unable to de-reference elements.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 16 Aug 2022 18:47:21 +0000 (20:47 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 16 Aug 2022 18:47:21 +0000 (20:47 +0200)
scripts/osm_to_wrmap.py
wrpylib/wrosm.py

index db235b1f951156e1dcb0cb07b34d7658b49977f9..40e84ff34cb2e7ff5f4d09746a3b35e16e76caba 100644 (file)
@@ -28,7 +28,7 @@ import wrpylib.wrmwmarkup
 
 from wrpylib.argparse_tools import bool_type
 from wrpylib.wrgeojson import join_sledrun_map_ways, simplify_ways
-from wrpylib.wrosm import find_sledrun_relations, tags, convert_osm_to_geojson
+from wrpylib.wrosm import find_sledrun_relations, tags, convert_osm_to_geojson, DeRefError
 
 
 def process_osm_file(filename: str, single: Optional[int], join_ways: bool, simplify_max_dist: Optional[float] = 15.):
@@ -56,7 +56,11 @@ def process_osm_file(filename: str, single: Optional[int], join_ways: bool, simp
         print("-" * len(title))
 
         # Convert to geojson
-        sledrun_geojson = convert_osm_to_geojson(osm_tree, sledrun_relation)
+        try:
+            sledrun_geojson = convert_osm_to_geojson(osm_tree, sledrun_relation)
+        except DeRefError:
+            print('Error: Incomplete XML - please load larger region.')
+            continue
 
         # Merge sledrun ways of the same type
         if join_ways:
index 15b4ef02799dc3060c8cccf46806eabe061cf46e..7b056150f75d0b5614c12096e9b3a60b03fa7b68 100644 (file)
@@ -18,14 +18,22 @@ def tags(element: Element) -> Dict:
     return {tag.attrib['k']: tag.attrib['v'] for tag in tag_list}
 
 
+class DeRefError(ValueError):
+    pass
+
+
 def de_ref(osm_tree: ElementTree, element: Element) -> Element:
     """De-references an element (relation member or nd)."""
     ref = element.get('ref')
     if element.tag == 'member':
-        return osm_tree.find(f"./{element.get('type')}[@id='{ref}']")
-    if element.tag == 'nd':
-        return osm_tree.find(f"./node[@id='{ref}']")
-    raise ValueError(f'Unsupported element type: {element.tag}')
+        target = osm_tree.find(f"./{element.get('type')}[@id='{ref}']")
+    elif element.tag == 'nd':
+        target = osm_tree.find(f"./node[@id='{ref}']")
+    else:
+        raise ValueError(f'Unsupported element type: {element.tag}')
+    if target is None:
+        raise DeRefError(f'Element {element} not found in tree.')
+    return target
 
 
 def node_coordinates(element: Element) -> Point: