3939
4040import numpy
4141import shapely .geometry .base
42+ from pyproj import CRS
4243from shapely .geometry import Point , Polygon , MultiPolygon , GeometryCollection
4344from shapely .geometry .base import BaseGeometry
4445
5354 AggregatePolygonResult ,
5455 AggregatePolygonSpatialResult ,
5556)
56- from openeo_driver .util .geometry import geojson_to_geometry , GeometryBufferer
57+ from openeo_driver .util .geometry import geojson_to_geometry , GeometryBufferer , BoundingBox
5758from openeo_driver .utils import to_hashable , EvalEnv
5859
5960_log = logging .getLogger (__name__ )
@@ -519,7 +520,11 @@ def filter_bbox(
519520 "crs" : (crs or "EPSG:4326" )})
520521
521522 def filter_spatial (self , geometries ):
522- geometries , bbox = self ._normalize_geometry (geometries )
523+ crs = None
524+ if (len (self .metadata .spatial_dimensions ) > 0 ):
525+ spatial_dim = self .metadata .spatial_dimensions [0 ]
526+ crs = spatial_dim .crs
527+ geometries , bbox = self ._normalize_geometry (geometries ,target_crs = crs )
523528 cube = self .filter_bbox (** bbox , operation = "weak_spatial_extent" )
524529 return cube ._process (operation = "filter_spatial" , arguments = {"geometries" : geometries })
525530
@@ -570,11 +575,15 @@ def aggregate_spatial(
570575 geoms_is_empty = isinstance (geometries , DriverVectorCube ) and len (geometries .get_geometries ()) == 0
571576 cube = self
572577 if not geoms_is_empty :
573- geometries , bbox = self ._normalize_geometry (geometries )
578+ crs = None
579+ if (len (self .metadata .spatial_dimensions )> 0 ):
580+ spatial_dim = self .metadata .spatial_dimensions [0 ]
581+ crs = spatial_dim .crs
582+ geometries , bbox = self ._normalize_geometry (geometries ,target_crs = crs )
574583 cube = self .filter_bbox (** bbox , operation = "weak_spatial_extent" )
575584 return cube ._process (operation = "aggregate_spatial" , arguments = {"geometries" : geometries })
576585
577- def _normalize_geometry (self , geometries ) -> Tuple [Union [DriverVectorCube , DelayedVector , BaseGeometry ], dict ]:
586+ def _normalize_geometry (self , geometries , target_crs = None ) -> Tuple [Union [DriverVectorCube , DelayedVector , BaseGeometry ], dict ]:
578587 """
579588 Helper to preprocess geometries (as used in aggregate_spatial and mask_polygon)
580589 and extract bbox (e.g. for filter_bbox)
@@ -586,8 +595,17 @@ def _normalize_geometry(self, geometries) -> Tuple[Union[DriverVectorCube, Delay
586595 # TODO: buffer distance of 10m assumes certain resolution (e.g. sentinel2 pixels)
587596 # TODO: use proper distance for collection resolution instead of using a default distance?
588597 # TODO: or eliminate need for buffering in the first place? https://github.com/Open-EO/openeo-python-driver/issues/148
589- bbox = geometries .buffer_points (distance = 10 ).get_bounding_box ()
590- crs = geometries .get_crs_str ()
598+ if target_crs is not None :
599+ is_utm = crs == "AUTO:42001" or "Auto42001" in str (target_crs )
600+ if is_utm :
601+ target_crs = BoundingBox .from_wsen_tuple (geometries .get_bounding_box (),crs = geometries .get_crs ()).best_utm ()
602+ else :
603+ target_crs = BoundingBox .normalize_crs (target_crs )
604+ bbox = geometries .buffer_points (distance = 10 ).reproject (CRS .from_user_input ( target_crs )).get_bounding_box ()
605+ crs = target_crs
606+ else :
607+ bbox = geometries .buffer_points (distance = 10 ).get_bounding_box ()
608+ crs = geometries .get_crs_str ()
591609 elif isinstance (geometries , dict ):
592610 return self ._normalize_geometry (geojson_to_geometry (geometries ))
593611 elif isinstance (geometries , str ):
0 commit comments