Skip to content

Commit b853f22

Browse files
committed
Issue #690/#712 best effort resample_spatial on cube without metadata
1 parent 42c31b4 commit b853f22

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

openeo/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def drop_dimension(self, name: str = None) -> CubeMetadata:
422422

423423
def resample_spatial(
424424
self,
425-
resolution: Union[int, float, Tuple[float, float], Tuple[int, int]] = 0.0,
425+
resolution: Union[float, Tuple[float, float], List[float]] = 0.0,
426426
projection: Union[int, str, None] = None,
427427
) -> CubeMetadata:
428428
resolution = normalize_resample_resolution(resolution)

openeo/rest/datacube.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
Band,
4141
BandDimension,
4242
CollectionMetadata,
43+
CubeMetadata,
4344
SpatialDimension,
4445
TemporalDimension,
4546
metadata_from_stac,
@@ -69,6 +70,10 @@
6970
from openeo.udf import XarrayDataCube
7071

7172

73+
# Sentinel value for arguments that are unset (when `None` has a different meaning)
74+
_UNSET = object()
75+
76+
7277
log = logging.getLogger(__name__)
7378

7479

@@ -97,7 +102,7 @@ def process(
97102
self,
98103
process_id: str,
99104
arguments: Optional[dict] = None,
100-
metadata: Optional[CollectionMetadata] = None,
105+
metadata: Optional[CubeMetadata] = _UNSET,
101106
namespace: Optional[str] = None,
102107
**kwargs,
103108
) -> DataCube:
@@ -111,7 +116,11 @@ def process(
111116
:return: new DataCube instance
112117
"""
113118
pg = self._build_pgnode(process_id=process_id, arguments=arguments, namespace=namespace, **kwargs)
114-
return DataCube(graph=pg, connection=self._connection, metadata=metadata or self.metadata)
119+
return DataCube(
120+
graph=pg,
121+
connection=self._connection,
122+
metadata=self.metadata if metadata is _UNSET else metadata,
123+
)
115124

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

@@ -750,14 +759,12 @@ def band(self, band: Union[str, int]) -> DataCube:
750759
@openeo_process
751760
def resample_spatial(
752761
self,
753-
resolution: Union[int, float, Tuple[float, float], Tuple[int, int]] = 0.0,
762+
resolution: Union[float, Tuple[float, float], List[float]] = 0.0,
754763
projection: Union[int, str, None] = None,
755764
method: str = "near",
756765
align: str = "upper-left",
757766
) -> DataCube:
758-
metadata = (
759-
self.metadata.resample_spatial(resolution=resolution, projection=projection) if self.metadata else None
760-
)
767+
metadata = (self.metadata or CubeMetadata()).resample_spatial(resolution=resolution, projection=projection)
761768
return self.process(
762769
process_id="resample_spatial",
763770
arguments={
@@ -783,8 +790,8 @@ def resample_cube_spatial(self, target: DataCube, method: str = "near") -> DataC
783790
:param method: Resampling method to use.
784791
:return:
785792
"""
786-
if self.metadata and target.metadata:
787-
metadata = self.metadata.resample_cube_spatial(target=target.metadata)
793+
if target.metadata:
794+
metadata = (self.metadata or CubeMetadata()).resample_cube_spatial(target=target.metadata)
788795
else:
789796
metadata = None
790797
return self.process(

tests/rest/datacube/test_datacube.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,26 @@ def test_resample_spatial(s2cube):
744744
]
745745

746746

747+
def test_resample_spatial_no_metadata(s2cube_without_metadata):
748+
cube = s2cube_without_metadata.resample_spatial(resolution=(3, 5), projection=4578)
749+
assert get_download_graph(cube, drop_load_collection=True, drop_save_result=True) == {
750+
"resamplespatial1": {
751+
"process_id": "resample_spatial",
752+
"arguments": {
753+
"data": {"from_node": "loadcollection1"},
754+
"resolution": [3, 5],
755+
"projection": 4578,
756+
"method": "near",
757+
"align": "upper-left",
758+
},
759+
}
760+
}
761+
assert cube.metadata.spatial_dimensions == [
762+
SpatialDimension(name="x", extent=[None, None], crs=4578, step=3.0),
763+
SpatialDimension(name="y", extent=[None, None], crs=4578, step=5.0),
764+
]
765+
766+
747767
def test_resample_cube_spatial(s2cube):
748768
cube1 = s2cube.resample_spatial(resolution=[2.0, 3.0], projection=4578)
749769
cube2 = s2cube.resample_spatial(resolution=10, projection=32631)
@@ -821,16 +841,27 @@ def test_resample_cube_spatial(s2cube):
821841
]
822842

823843

824-
def test_resample_cube_spatial_no_metadata(s2cube_without_metadata):
825-
cube1 = s2cube_without_metadata.resample_spatial(resolution=[2.0, 3.0], projection=4578)
826-
cube2 = s2cube_without_metadata.resample_spatial(resolution=10, projection=32631)
827-
assert cube1.metadata is None
828-
assert cube2.metadata is None
844+
def test_resample_cube_spatial_no_source_metadata(s2cube, s2cube_without_metadata):
845+
cube = s2cube_without_metadata
846+
target = s2cube.resample_spatial(resolution=10, projection=32631)
847+
assert cube.metadata is None
848+
assert target.metadata is not None
849+
850+
result = cube.resample_cube_spatial(target=target)
851+
assert result.metadata.spatial_dimensions == [
852+
SpatialDimension(name="x", extent=None, crs=32631, step=10),
853+
SpatialDimension(name="y", extent=None, crs=32631, step=10),
854+
]
829855

830-
cube12 = cube1.resample_cube_spatial(target=cube2)
831-
assert cube12.metadata is None
832856

857+
def test_resample_cube_spatial_no_target_metadata(s2cube, s2cube_without_metadata):
858+
cube = s2cube.resample_spatial(resolution=10, projection=32631)
859+
target = s2cube_without_metadata
860+
assert cube.metadata is not None
861+
assert target.metadata is None
833862

863+
result = cube.resample_cube_spatial(target=target)
864+
assert result.metadata is None
834865

835866

836867
def test_merge(s2cube, api_version, test_data):

0 commit comments

Comments
 (0)