Skip to content

Commit

Permalink
Issue #690/#712 also cover resample_cube_spatial
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jan 22, 2025
1 parent ab651ce commit 450b789
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def resample_spatial(
self,
resolution: Union[int, float, Tuple[float, float], Tuple[int, int]] = 0.0,
projection: Union[int, str, None] = None,
):
) -> CubeMetadata:
resolution = normalize_resample_resolution(resolution)
if self._dimensions is None:
# Best-effort fallback to work with
Expand All @@ -451,6 +451,8 @@ def resample_spatial(

return self._clone_and_update(dimensions=dimensions)

def resample_cube_spatial(self, target: CubeMetadata) -> CubeMetadata:
return self._clone_and_update(dimensions=list(target._dimensions))

class CollectionMetadata(CubeMetadata):
"""
Expand Down
10 changes: 9 additions & 1 deletion openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,15 @@ def resample_cube_spatial(self, target: DataCube, method: str = "near") -> DataC
:param method: Resampling method to use.
:return:
"""
return self.process("resample_cube_spatial", {"data": self, "target": target, "method": method})
if self.metadata and target.metadata:
metadata = self.metadata.resample_cube_spatial(target=target.metadata)
else:
metadata = None
return self.process(
process_id="resample_cube_spatial",
arguments={"data": self, "target": target, "method": method},
metadata=metadata,
)

@openeo_process
def resample_cube_temporal(
Expand Down
2 changes: 2 additions & 0 deletions tests/rest/datacube/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@ def test_resample_spatial(s2cube):
]




def test_merge(s2cube, api_version, test_data):
merged = s2cube.ndvi().merge(s2cube)
expected_graph = test_data.load_json("{v}/merge_ndvi_self.json".format(v=api_version))
Expand Down
26 changes: 26 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,3 +950,29 @@ def test_metadata_resample_spatial(xytb_cube_metadata, kwargs, expected_x, expec
]
assert metadata.temporal_dimension == xytb_cube_metadata.temporal_dimension
assert metadata.band_dimension == xytb_cube_metadata.band_dimension


def test_metadata_resample_cube_spatial(xytb_cube_metadata):
metadata1 = xytb_cube_metadata.resample_spatial(resolution=(11, 22), projection=32631)
metadata2 = xytb_cube_metadata.resample_spatial(resolution=0.5)

assert metadata1.spatial_dimensions == [
SpatialDimension(name="x", extent=[2, 7], crs=32631, step=11),
SpatialDimension(name="y", extent=[49, 52], crs=32631, step=22),
]
assert metadata2.spatial_dimensions == [
SpatialDimension(name="x", extent=[2, 7], crs=4326, step=0.5),
SpatialDimension(name="y", extent=[49, 52], crs=4326, step=0.5),
]

metadata12 = metadata1.resample_cube_spatial(target=metadata2)
assert metadata12.spatial_dimensions == [
SpatialDimension(name="x", extent=[2, 7], crs=4326, step=0.5),
SpatialDimension(name="y", extent=[49, 52], crs=4326, step=0.5),
]

metadata21 = metadata2.resample_cube_spatial(target=metadata1)
assert metadata21.spatial_dimensions == [
SpatialDimension(name="x", extent=[2, 7], crs=32631, step=11),
SpatialDimension(name="y", extent=[49, 52], crs=32631, step=22),
]

0 comments on commit 450b789

Please sign in to comment.