diff --git a/openeo/rest/_datacube.py b/openeo/rest/_datacube.py index fce6f717c..b63672d97 100644 --- a/openeo/rest/_datacube.py +++ b/openeo/rest/_datacube.py @@ -113,7 +113,13 @@ def from_node(self): # _FromNodeMixin API return self._pg - def _build_pgnode(self, process_id: str, arguments: dict, namespace: Optional[str], **kwargs) -> PGNode: + def _build_pgnode( + self, + process_id: str, + arguments: Optional[dict] = None, + namespace: Optional[str] = None, + **kwargs + ) -> PGNode: """ Helper to build a PGNode from given argument dict and/or kwargs, and possibly resolving the `THIS` reference. diff --git a/openeo/rest/datacube.py b/openeo/rest/datacube.py index 462a92fc6..6a9242f3a 100644 --- a/openeo/rest/datacube.py +++ b/openeo/rest/datacube.py @@ -196,13 +196,13 @@ def __init__(self, graph: PGNode, connection: 'openeo.Connection', metadata: Col self.metadata = CollectionMetadata.get_or_create(metadata) def process( - self, - process_id: str, - arguments: dict = None, - metadata: Optional[CollectionMetadata] = None, - namespace: Optional[str] = None, - **kwargs - ) -> 'DataCube': + self, + process_id: str, + arguments: Optional[dict] = None, + metadata: Optional[CollectionMetadata] = None, + namespace: Optional[str] = None, + **kwargs, + ) -> "DataCube": """ Generic helper to create a new DataCube by applying a process. @@ -961,14 +961,21 @@ def _get_geometry_argument( @openeo_process def aggregate_spatial( - self, - geometries: Union[shapely.geometry.base.BaseGeometry, dict, str, pathlib.Path, Parameter, "VectorCube"], - reducer: Union[str, PGNode, typing.Callable], - target_dimension: Optional[str] = None, - crs: str = None, - context: Optional[dict] = None, - # TODO arguments: target dimension, context - ) -> 'DataCube': + self, + geometries: Union[ + shapely.geometry.base.BaseGeometry, + dict, + str, + pathlib.Path, + Parameter, + VectorCube, + ], + reducer: Union[str, PGNode, typing.Callable], + target_dimension: Optional[str] = None, + crs: str = None, + context: Optional[dict] = None, + # TODO arguments: target dimension, context + ) -> VectorCube: """ Aggregates statistics for one or more geometries (e.g. zonal statistics for polygons) over the spatial dimensions. @@ -984,16 +991,24 @@ def aggregate_spatial( .. note:: this ``crs`` argument is a non-standard/experimental feature, only supported by specific back-ends. See https://github.com/Open-EO/openeo-processes/issues/235 for details. """ - # TODO #279 aggregate_spatial should return a VectorCube, not a DataCube valid_geojson_types = [ "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", "GeometryCollection", "Feature", "FeatureCollection" ] geometries = self._get_geometry_argument(geometries, valid_geojson_types=valid_geojson_types, crs=crs) reducer = self._get_callback(reducer, parent_parameters=["data"]) - return self.process( - process_id="aggregate_spatial", data=THIS, geometries=geometries, reducer=reducer, - **dict_no_none(target_dimension=target_dimension, context=context) + return VectorCube( + graph=self._build_pgnode( + process_id="aggregate_spatial", + data=THIS, + geometries=geometries, + reducer=reducer, + arguments=dict_no_none( + target_dimension=target_dimension, context=context + ), + ), + connection=self._connection, + # TODO: metadata? ) @staticmethod @@ -1970,7 +1985,7 @@ def save_user_defined_process( returns=returns, categories=categories, examples=examples, links=links, ) - def execute(self) -> Dict: + def execute(self) -> dict: """Executes the process graph of the imagery. """ return self._connection.execute(self.flat_graph()) diff --git a/openeo/rest/vectorcube.py b/openeo/rest/vectorcube.py index 9b7c195e3..275381d0c 100644 --- a/openeo/rest/vectorcube.py +++ b/openeo/rest/vectorcube.py @@ -70,7 +70,12 @@ def save_result(self, format: str = "GeoJson", options: dict = None): } ) + def execute(self) -> dict: + """Executes the process graph of the imagery.""" + return self._connection.execute(self.flat_graph()) + def download(self, outputfile: str, format: str = "GeoJSON", options: dict = None): + # TODO: only add save_result, when not already present (see DataCube.download) cube = self.save_result(format=format, options=options) return self._connection.download(cube.flat_graph(), outputfile)