From 2bac71979d0fdef87c959fdc2f32f78be5005759 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Tue, 3 Sep 2024 08:14:54 +0200 Subject: [PATCH] Preserve non-spatial metadata in aggregate_spatial #612 --- CHANGELOG.md | 1 + openeo/rest/datacube.py | 3 ++- openeo/rest/vectorcube.py | 4 ++-- tests/rest/datacube/test_datacube100.py | 20 ++++++++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fd4ca464..7317db866 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - `apply_dimension` with a `target_dimension` argument was not correctly adjusting datacube metadata on the client side, causing a mismatch. +- Preserve non-spatial dimension metadata in `aggregate_spatial` ([#612](https://github.com/Open-EO/openeo-python-client/issues/612)) ## [0.31.0] - 2024-07-26 diff --git a/openeo/rest/datacube.py b/openeo/rest/datacube.py index c0a05236e..f79331772 100644 --- a/openeo/rest/datacube.py +++ b/openeo/rest/datacube.py @@ -1008,7 +1008,8 @@ def aggregate_spatial( ), ), connection=self._connection, - # TODO: metadata? And correct dimension of created vector cube? #457 + # TODO: also add new "geometry" dimension #457 + metadata=self.metadata.reduce_spatial(), ) @openeo_process diff --git a/openeo/rest/vectorcube.py b/openeo/rest/vectorcube.py index 80fe7ed86..206130bfc 100644 --- a/openeo/rest/vectorcube.py +++ b/openeo/rest/vectorcube.py @@ -12,7 +12,7 @@ from openeo.internal.documentation import openeo_process from openeo.internal.graph_building import PGNode from openeo.internal.warnings import legacy_alias -from openeo.metadata import CollectionMetadata, Dimension +from openeo.metadata import CollectionMetadata, CubeMetadata, Dimension from openeo.rest._datacube import ( THIS, UDF, @@ -37,7 +37,7 @@ class VectorCube(_ProcessGraphAbstraction): A geometry is specified in a 'coordinate reference system'. https://www.w3.org/TR/sdw-bp/#dfn-coordinate-reference-system-(crs) """ - def __init__(self, graph: PGNode, connection: Connection, metadata: Optional[CollectionMetadata] = None): + def __init__(self, graph: PGNode, connection: Connection, metadata: Optional[CubeMetadata] = None): super().__init__(pgnode=graph, connection=connection) self.metadata = metadata diff --git a/tests/rest/datacube/test_datacube100.py b/tests/rest/datacube/test_datacube100.py index 106698f03..e5a889822 100644 --- a/tests/rest/datacube/test_datacube100.py +++ b/tests/rest/datacube/test_datacube100.py @@ -3670,3 +3670,23 @@ def test_to_json_with_if_and_udf(con100): } assert 'process_id": "run_udf"' in cube.to_json() + + +def test_aggregate_spatial_band_metadata(con100): + """https://github.com/Open-EO/openeo-python-client/issues/612""" + cube = con100.load_collection("S2", bands=["B02", "B03"]) + geometry = shapely.geometry.box(0, 0, 1, 1) + aggregated = cube.aggregate_spatial(geometries=geometry, reducer="mean") + assert aggregated.metadata.band_names == ["B02", "B03"] + + +def test_aggregate_spatial_and_merge_again(con100): + """https://github.com/Open-EO/openeo-python-client/issues/612""" + cube = con100.load_collection("S2", bands=["B02", "B03"]) + geometry = shapely.geometry.box(0, 0, 1, 1) + aggregated = cube.aggregate_spatial(geometries=geometry, reducer="mean") + rasterized = aggregated.vector_to_raster(target=cube).rename_labels( + dimension="bands", target=["B02-mean", "B03-mean"] + ) + merged = cube.merge_cubes(rasterized) + assert merged.metadata.band_names == ["B02", "B03", "B02-mean", "B03-mean"]