Skip to content

Commit

Permalink
Issue #425 load_stac: support lambda based property filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Aug 9, 2023
1 parent febdeca commit 6409f0a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Processes that take a CRS as argument now try harder to convert your input into a proper EPSG code, to avoid unexpected results when an invalid argument gets sent to the backend.
- Initial `load_geojson` support with `Connection.load_geojson()` ([#424](https://github.com/Open-EO/openeo-python-client/issues/424))
- Initial `load_url` (for vector cubes) support with `Connection.load_url()` ([#424](https://github.com/Open-EO/openeo-python-client/issues/424))
- Support lambda based property filtering in `Connection.load_stac()` ([#425](https://github.com/Open-EO/openeo-python-client/issues/425))


### Changed
Expand Down Expand Up @@ -105,7 +106,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
including special mode for in Jupyter notebooks.
([#237](https://github.com/Open-EO/openeo-python-client/issues/237))
- Basic support for `load_stac` process with `Connection.load_stac()`
([#425](https://github.com/Open-EO/openeo-python-client/issues/))
([#425](https://github.com/Open-EO/openeo-python-client/issues/425))
- Add `DataCube.aggregate_spatial_window()`

### Fixed
Expand Down
1 change: 1 addition & 0 deletions openeo/rest/_datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def build_child_callback(
:param parent_parameters: list of parameter names defined for child process
:return:
"""
# TODO: move this to more generic process graph building utility module
# TODO: autodetect the parameters defined by parent process?
if isinstance(process, PGNode):
# Assume this is already a valid callback process
Expand Down
10 changes: 7 additions & 3 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from openeo.internal.warnings import legacy_alias, deprecated
from openeo.metadata import CollectionMetadata, SpatialDimension, TemporalDimension, BandDimension, Band
from openeo.rest import OpenEoClientException, OpenEoApiError, OpenEoRestError
from openeo.rest._datacube import build_child_callback
from openeo.rest.auth.auth import NullAuth, BearerAuth, BasicBearerAuth, OidcBearerAuth
from openeo.rest.auth.config import RefreshTokenStore, AuthConfig
from openeo.rest.auth.oidc import OidcClientCredentialsAuthenticator, OidcAuthCodePkceAuthenticator, \
Expand Down Expand Up @@ -1280,7 +1281,8 @@ def load_stac(
.. versionadded:: 0.17.0
"""
# TODO: detect actual metadata from URL
# TODO #425 move this implementation to `DataCube` and just forward here (like with `load_collection`)
# TODO #425 detect actual metadata from URL
metadata = CollectionMetadata(
{},
dimensions=[
Expand All @@ -1291,15 +1293,17 @@ def load_stac(
],
)
arguments = {"url": url}
# TODO: more normalization/validation of extent/band parameters and `properties`
# TODO #425 more normalization/validation of extent/band parameters
if spatial_extent:
arguments["spatial_extent"] = spatial_extent
if temporal_extent:
arguments["temporal_extent"] = DataCube._get_temporal_extent(temporal_extent)
if bands:
arguments["bands"] = bands
if properties:
arguments["properties"] = properties
arguments["properties"] = {
prop: build_child_callback(pred, parent_parameters=["value"]) for prop, pred in properties.items()
}
cube = self.datacube_from_process(process_id="load_stac", **arguments)
cube.metadata = metadata
return cube
Expand Down
31 changes: 27 additions & 4 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2331,18 +2331,18 @@ def test_load_result_filters(requests_mock):

class TestLoadStac:
def test_basic(self, con120):
cube = con120.load_stac("https://provide.test/dataset")
cube = con120.load_stac("https://provider.test/dataset")
assert cube.flat_graph() == {
"loadstac1": {
"process_id": "load_stac",
"arguments": {"url": "https://provide.test/dataset"},
"arguments": {"url": "https://provider.test/dataset"},
"result": True,
}
}

def test_extents(self, con120):
cube = con120.load_stac(
"https://provide.test/dataset",
"https://provider.test/dataset",
spatial_extent={"west": 1, "south": 2, "east": 3, "north": 4},
temporal_extent=["2023-05-10", "2023-06-01"],
bands=["B02", "B03"],
Expand All @@ -2351,7 +2351,7 @@ def test_extents(self, con120):
"loadstac1": {
"process_id": "load_stac",
"arguments": {
"url": "https://provide.test/dataset",
"url": "https://provider.test/dataset",
"spatial_extent": {"east": 3, "north": 4, "south": 2, "west": 1},
"temporal_extent": ["2023-05-10", "2023-06-01"],
"bands": ["B02", "B03"],
Expand All @@ -2360,6 +2360,29 @@ def test_extents(self, con120):
}
}

def test_properties(self, con120):
cube = con120.load_stac("https://provider.test/dataset", properties={"platform": lambda p: p == "S2A"})
assert cube.flat_graph() == {
"loadstac1": {
"process_id": "load_stac",
"arguments": {
"url": "https://provider.test/dataset",
"properties": {
"platform": {
"process_graph": {
"eq1": {
"arguments": {"x": {"from_parameter": "value"}, "y": "S2A"},
"process_id": "eq",
"result": True,
}
}
}
},
},
"result": True,
}
}


@pytest.mark.parametrize(
"data",
Expand Down

0 comments on commit 6409f0a

Please sign in to comment.