]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Fix calculating the center coordinates when directly producing geojson.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 25 Jun 2023 19:30:10 +0000 (21:30 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 25 Jun 2023 19:30:10 +0000 (21:30 +0200)
wrpylib/wrgeojson.py
wrpylib/wrosm.py

index ffd846b14477cf876194ce60bf6af78d7fe40c11..83a6bb151c46e19bc0fc708d1e7fc81b5b21946e 100644 (file)
@@ -117,7 +117,7 @@ from enum import Enum
 from typing import Iterable, Tuple, List
 
 import geojson.utils
-from geojson import FeatureCollection, Feature
+from geojson import FeatureCollection, Feature, Point
 
 
 class WrPointFeatureType(Enum):
@@ -207,17 +207,17 @@ def simplify_ways(wrgeojson: FeatureCollection, max_dist: float = DEFAULT_MAX_DI
         simplify_way(way['geometry']['coordinates'], max_dist)
 
 
-def find_center(coordinates: Iterable[Tuple[float, ...]], fallback: Tuple[float, float]) -> Tuple[float, float]:
+def find_center(coordinates: Iterable[Tuple[float, ...]], fallback: Tuple[float, float]) -> Point:
     """Returns the center of the given coordinates. If the lists are empty, fallback_lon is returned."""
     coordinates = list(coordinates)
     min_lon = min(coordinates, key=lambda c: c[0], default=fallback[0])[0]
     max_lon = max(coordinates, key=lambda c: c[0], default=fallback[0])[0]
     min_lat = min(coordinates, key=lambda c: c[1], default=fallback[1])[1]
     max_lat = max(coordinates, key=lambda c: c[1], default=fallback[1])[1]
-    return (min_lon + max_lon) / 2., (min_lat + max_lat) / 2.
+    return Point(((min_lon + max_lon) / 2., (min_lat + max_lat) / 2.))
 
 
-def find_map_center(wrgeojson: FeatureCollection, fallback: Tuple[float, float]) -> Tuple[float, float]:
+def find_map_center(wrgeojson: FeatureCollection, fallback: Tuple[float, float]) -> Point:
     # Calculate center of the map
     coordinates = geojson.utils.coords(wrgeojson)
     return find_center(coordinates, fallback)
index 7b056150f75d0b5614c12096e9b3a60b03fa7b68..e87644d5c871e8c59da0f672d81dd15892a01de8 100644 (file)
@@ -59,7 +59,7 @@ def point_feature(osm_tree: ElementTree, point_type: WrPointFeatureType, name: O
         point = node_coordinates(element)
     elif element.tag == 'way':
         line_string = way_coordinates(osm_tree, element)
-        point = find_center(line_string, fallback=(0., 0.))
+        point = find_center(line_string["coordinates"], fallback=(0., 0.))
     else:
         raise ValueError(f'Unsupported element: {element.tag}')
     return Feature(geometry=point, properties=properties)
@@ -137,8 +137,8 @@ def convert_osm_to_geojson(osm_tree: ElementTree, sledrun_relation: Element) ->
     center = find_map_center(feature_collection, (0., 0.))
     feature_collection = sort_features(feature_collection)
     feature_collection['properties'] = {
-        'lon': center[0],
-        'lat': center[1],
+        'lon': center['coordinates'][0],
+        'lat': center['coordinates'][1],
         'zoom': 14
     }
     return feature_collection