Skip to content

Commit

Permalink
DataCube.load_collection without conneciton: automatically avoid `f…
Browse files Browse the repository at this point in the history
…etch_metadata` #638
  • Loading branch information
soxofaan committed Oct 2, 2024
1 parent 33317b0 commit 3871077
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- When using `DataCube.load_collection()` without a connection, it is not necessary anymore to also explicitly set `fetch_metadata=False` ([#638](https://github.com/Open-EO/openeo-python-client/issues/638))


## [0.32.0] - 2024-09-27

Expand Down
7 changes: 4 additions & 3 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _assert_valid_dimension_name(self, name: str) -> str:
def load_collection(
cls,
collection_id: Union[str, Parameter],
connection: Connection = None,
connection: Optional[Connection] = None,
spatial_extent: Union[Dict[str, float], Parameter, None] = None,
temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None,
bands: Union[None, List[str], Parameter] = None,
Expand All @@ -151,7 +151,8 @@ def load_collection(
Create a new Raster Data cube.
:param collection_id: image collection identifier
:param connection: The connection to use to connect with the backend.
:param connection: The backend connection to use.
Can be ``None`` to work without connection and collection metadata.
:param spatial_extent: limit data to specified bounding box or polygons
:param temporal_extent: limit data to specified temporal interval.
Typically, just a two-item list or tuple containing start and end date.
Expand Down Expand Up @@ -190,7 +191,7 @@ def load_collection(
if isinstance(collection_id, Parameter):
fetch_metadata = False
metadata: Optional[CollectionMetadata] = (
connection.collection_metadata(collection_id) if fetch_metadata else None
connection.collection_metadata(collection_id) if connection and fetch_metadata else None
)
if bands:
if isinstance(bands, str):
Expand Down
55 changes: 55 additions & 0 deletions tests/rest/datacube/test_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import shapely
import shapely.geometry

from openeo import collection_property
from openeo.api.process import Parameter
from openeo.rest import BandMathException, OpenEoClientException
from openeo.rest._testing import build_capabilities
Expand Down Expand Up @@ -81,6 +82,60 @@ def _get_leaf_node(cube, force_flat=True) -> dict:
raise ValueError(repr(cube))


class TestDataCube:
def test_load_collection_connectionless_basic(self):
cube = DataCube.load_collection("T3")
assert cube.flat_graph() == {
"loadcollection1": {
"arguments": {"id": "T3", "spatial_extent": None, "temporal_extent": None},
"process_id": "load_collection",
"result": True,
}
}

def test_load_collection_connectionless_full(self):
cube = DataCube.load_collection(
"T3",
spatial_extent={"west": 1, "east": 2, "north": 3, "south": 4},
temporal_extent=["2019-01-01", "2019-02-01"],
bands=["RED", "GREEN"],
properties=[collection_property("orbit") == "low"],
)
assert cube.flat_graph() == {
"loadcollection1": {
"process_id": "load_collection",
"arguments": {
"id": "T3",
"spatial_extent": {"east": 2, "north": 3, "south": 4, "west": 1},
"temporal_extent": ["2019-01-01", "2019-02-01"],
"bands": ["RED", "GREEN"],
"properties": {
"orbit": {
"process_graph": {
"eq1": {
"process_id": "eq",
"arguments": {"x": {"from_parameter": "value"}, "y": "low"},
"result": True,
}
}
}
},
},
"result": True,
}
}

def test_load_collection_connectionless_temporal_extent_shortcut(self):
cube = DataCube.load_collection("T3", temporal_extent="2024-09")
assert cube.flat_graph() == {
"loadcollection1": {
"arguments": {"id": "T3", "spatial_extent": None, "temporal_extent": ["2024-09-01", "2024-10-01"]},
"process_id": "load_collection",
"result": True,
}
}


def test_filter_temporal_basic_positional_args(s2cube):
im = s2cube.filter_temporal("2016-01-01", "2016-03-10")
graph = _get_leaf_node(im)
Expand Down

0 comments on commit 3871077

Please sign in to comment.