Skip to content

Commit

Permalink
Issue #690/#712 best effort resample_spatial on cube without metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jan 23, 2025
1 parent 8af769c commit 064c688
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
2 changes: 1 addition & 1 deletion openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def drop_dimension(self, name: str = None) -> CubeMetadata:

def resample_spatial(
self,
resolution: Union[int, float, Tuple[float, float], Tuple[int, int]] = 0.0,
resolution: Union[float, Tuple[float, float], List[float]] = 0.0,
projection: Union[int, str, None] = None,
) -> CubeMetadata:
resolution = normalize_resample_resolution(resolution)
Expand Down
23 changes: 15 additions & 8 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Band,
BandDimension,
CollectionMetadata,
CubeMetadata,
SpatialDimension,
TemporalDimension,
metadata_from_stac,
Expand Down Expand Up @@ -69,6 +70,10 @@
from openeo.udf import XarrayDataCube


# Sentinel value for arguments that are unset (when `None` has a different meaning)
_UNSET = object()


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -97,7 +102,7 @@ def process(
self,
process_id: str,
arguments: Optional[dict] = None,
metadata: Optional[CollectionMetadata] = None,
metadata: Optional[CubeMetadata] = _UNSET,
namespace: Optional[str] = None,
**kwargs,
) -> DataCube:
Expand All @@ -111,7 +116,11 @@ def process(
:return: new DataCube instance
"""
pg = self._build_pgnode(process_id=process_id, arguments=arguments, namespace=namespace, **kwargs)
return DataCube(graph=pg, connection=self._connection, metadata=metadata or self.metadata)
return DataCube(
graph=pg,
connection=self._connection,
metadata=self.metadata if metadata is _UNSET else metadata,
)

graph_add_node = legacy_alias(process, "graph_add_node", since="0.1.1")

Expand Down Expand Up @@ -750,14 +759,12 @@ def band(self, band: Union[str, int]) -> DataCube:
@openeo_process
def resample_spatial(
self,
resolution: Union[int, float, Tuple[float, float], Tuple[int, int]] = 0.0,
resolution: Union[float, Tuple[float, float], List[float]] = 0.0,
projection: Union[int, str, None] = None,
method: str = "near",
align: str = "upper-left",
) -> DataCube:
metadata = (
self.metadata.resample_spatial(resolution=resolution, projection=projection) if self.metadata else None
)
metadata = (self.metadata or CubeMetadata()).resample_spatial(resolution=resolution, projection=projection)
return self.process(
process_id="resample_spatial",
arguments={
Expand All @@ -783,8 +790,8 @@ def resample_cube_spatial(self, target: DataCube, method: str = "near") -> DataC
:param method: Resampling method to use.
:return:
"""
if self.metadata and target.metadata:
metadata = self.metadata.resample_cube_spatial(target=target.metadata)
if target.metadata:
metadata = (self.metadata or CubeMetadata()).resample_cube_spatial(target=target.metadata)
else:
metadata = None
return self.process(
Expand Down
45 changes: 38 additions & 7 deletions tests/rest/datacube/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,26 @@ def test_resample_spatial(s2cube):
]


def test_resample_spatial_no_metadata(s2cube_without_metadata):
cube = s2cube_without_metadata.resample_spatial(resolution=(3, 5), projection=4578)
assert get_download_graph(cube, drop_load_collection=True, drop_save_result=True) == {
"resamplespatial1": {
"process_id": "resample_spatial",
"arguments": {
"data": {"from_node": "loadcollection1"},
"resolution": [3, 5],
"projection": 4578,
"method": "near",
"align": "upper-left",
},
}
}
assert cube.metadata.spatial_dimensions == [
SpatialDimension(name="x", extent=[None, None], crs=4578, step=3.0),
SpatialDimension(name="y", extent=[None, None], crs=4578, step=5.0),
]


def test_resample_cube_spatial(s2cube):
cube1 = s2cube.resample_spatial(resolution=[2.0, 3.0], projection=4578)
cube2 = s2cube.resample_spatial(resolution=10, projection=32631)
Expand Down Expand Up @@ -828,16 +848,27 @@ def test_resample_cube_spatial(s2cube):
]


def test_resample_cube_spatial_no_metadata(s2cube_without_metadata):
cube1 = s2cube_without_metadata.resample_spatial(resolution=[2.0, 3.0], projection=4578)
cube2 = s2cube_without_metadata.resample_spatial(resolution=10, projection=32631)
assert cube1.metadata is None
assert cube2.metadata is None
def test_resample_cube_spatial_no_source_metadata(s2cube, s2cube_without_metadata):
cube = s2cube_without_metadata
target = s2cube.resample_spatial(resolution=10, projection=32631)
assert cube.metadata is None
assert target.metadata is not None

result = cube.resample_cube_spatial(target=target)
assert result.metadata.spatial_dimensions == [
SpatialDimension(name="x", extent=None, crs=32631, step=10),
SpatialDimension(name="y", extent=None, crs=32631, step=10),
]

cube12 = cube1.resample_cube_spatial(target=cube2)
assert cube12.metadata is None

def test_resample_cube_spatial_no_target_metadata(s2cube, s2cube_without_metadata):
cube = s2cube.resample_spatial(resolution=10, projection=32631)
target = s2cube_without_metadata
assert cube.metadata is not None
assert target.metadata is None

result = cube.resample_cube_spatial(target=target)
assert result.metadata is None


def test_merge(s2cube, api_version, test_data):
Expand Down

0 comments on commit 064c688

Please sign in to comment.