Skip to content

Commit

Permalink
Preserve non-spatial metadata in aggregate_spatial #612
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Sep 3, 2024
1 parent abd9650 commit 2bac719
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions openeo/rest/vectorcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
20 changes: 20 additions & 0 deletions tests/rest/datacube/test_datacube100.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 2bac719

Please sign in to comment.