diff --git a/openeo/rest/datacube.py b/openeo/rest/datacube.py index ec3df056b..4cbae0db1 100644 --- a/openeo/rest/datacube.py +++ b/openeo/rest/datacube.py @@ -282,7 +282,7 @@ def load_disk_collection(cls, connection: Connection, file_format: str, glob_pat def load_stac( cls, url: str, - spatial_extent: Union[Dict[str, float], Parameter, None] = None, + spatial_extent: Union[dict, Parameter, shapely.geometry.base.BaseGeometry, str, pathlib.Path, None] = None, temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None, bands: Union[Iterable[str], Parameter, str, None] = None, properties: Optional[Dict[str, Union[str, PGNode, Callable]]] = None, diff --git a/tests/rest/datacube/test_datacube.py b/tests/rest/datacube/test_datacube.py index 67faf2c3f..f353b4d42 100644 --- a/tests/rest/datacube/test_datacube.py +++ b/tests/rest/datacube/test_datacube.py @@ -137,31 +137,70 @@ def test_load_collection_connectionless_temporal_extent_shortcut(self): } } - def test_load_collection_connectionless_shapely_spatial_extent(self): - polygon = shapely.geometry.Polygon(((0.0, 1.0), (2.0, 1.0), (3.0, 2.0), (1.5, 0.0), (0.0, 1.0))) - cube = DataCube.load_collection("T3", spatial_extent=polygon) - assert cube.flat_graph() == { - "loadcollection1": { - "arguments": {"id": "T3", "spatial_extent": - {'coordinates': (((0.0,1.0),(2.0,1.0),(3.0,2.0),(1.5,0.0),(0.0,1.0)),),'type': 'Polygon'}, - "temporal_extent": None}, - "process_id": "load_collection", - "result": True, - } + def test_load_collection_spatial_extent_bbox(self, dummy_backend): + spatial_extent = {"west": 1, "south": 2, "east": 3, "north": 4} + cube = DataCube.load_collection("S2", spatial_extent=spatial_extent, connection=dummy_backend.connection) + cube.execute() + assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == { + "id": "S2", + "spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4}, + "temporal_extent": None, + } + + def test_load_collection_spatial_extent_shapely(self, dummy_backend): + polygon = shapely.geometry.Polygon([(3, 51), (4, 51), (4, 52), (3, 52)]) + cube = DataCube.load_collection("S2", spatial_extent=polygon, connection=dummy_backend.connection) + cube.execute() + assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == { + "id": "S2", + "spatial_extent": { + "type": "Polygon", + "coordinates": [[[3, 51], [4, 51], [4, 52], [3, 52], [3, 51]]], + }, + "temporal_extent": None, } @pytest.mark.parametrize("path_factory", [str, pathlib.Path]) - def test_load_collection_connectionless_local_path_spatial_extent(self, path_factory, test_data): + def test_load_collection_spatial_extent_local_path(self, dummy_backend, path_factory, test_data): path = path_factory(test_data.get_path("geojson/polygon02.json")) - cube = DataCube.load_collection("T3", spatial_extent=path) - assert cube.flat_graph() == { + cube = DataCube.load_collection("S2", spatial_extent=path, connection=dummy_backend.connection) + cube.execute() + assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == { + "id": "S2", + "spatial_extent": {"type": "Polygon", "coordinates": [[[3, 50], [4, 50], [4, 51], [3, 50]]]}, + "temporal_extent": None, + } + + def test_load_collection_spatial_extent_url(self, dummy_backend): + cube = DataCube.load_collection( + "S2", spatial_extent="https://geo.test/geometry.json", connection=dummy_backend.connection + ) + cube.execute() + assert dummy_backend.get_sync_pg() == { + "loadurl1": { + "process_id": "load_url", + "arguments": {"format": "GeoJSON", "url": "https://geo.test/geometry.json"}, + }, "loadcollection1": { - "arguments": {"id": "T3", "spatial_extent": - {"type": "Polygon", "coordinates": [[[3, 50], [4, 50], [4, 51], [3, 50]]]}, - "temporal_extent": None}, "process_id": "load_collection", + "arguments": { + "id": "S2", + "spatial_extent": {"from_node": "loadurl1"}, + "temporal_extent": None, + }, "result": True, - } + }, + } + + def test_load_collection_spatial_extent_parameter(self, dummy_backend): + cube = DataCube.load_collection( + "S2", spatial_extent=Parameter.geojson("zpatial_extent"), connection=dummy_backend.connection + ) + cube.execute() + assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == { + "id": "S2", + "spatial_extent": {"from_parameter": "zpatial_extent"}, + "temporal_extent": None, } def test_load_collection_connectionless_save_result(self): @@ -206,6 +245,71 @@ def test_load_stac_connectionless_save_result(self): }, } + def test_load_stac_spatial_extent_bbox(self, dummy_backend): + spatial_extent = {"west": 1, "south": 2, "east": 3, "north": 4} + cube = DataCube.load_stac( + "https://stac.test/data", spatial_extent=spatial_extent, connection=dummy_backend.connection + ) + cube.execute() + assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == { + "url": "https://stac.test/data", + "spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4}, + } + + def test_load_stac_spatial_extent_shapely(self, dummy_backend): + polygon = shapely.geometry.Polygon([(3, 51), (4, 51), (4, 52), (3, 52)]) + cube = DataCube.load_stac("https://stac.test/data", spatial_extent=polygon, connection=dummy_backend.connection) + cube.execute() + assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == { + "url": "https://stac.test/data", + "spatial_extent": { + "type": "Polygon", + "coordinates": [[[3, 51], [4, 51], [4, 52], [3, 52], [3, 51]]], + }, + } + + @pytest.mark.parametrize("path_factory", [str, pathlib.Path]) + def test_load_stac_spatial_extent_local_path(self, dummy_backend, path_factory, test_data): + path = path_factory(test_data.get_path("geojson/polygon02.json")) + cube = DataCube.load_stac("https://stac.test/data", spatial_extent=path, connection=dummy_backend.connection) + cube.execute() + assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == { + "url": "https://stac.test/data", + "spatial_extent": {"type": "Polygon", "coordinates": [[[3, 50], [4, 50], [4, 51], [3, 50]]]}, + } + + def test_load_stac_spatial_extent_url(self, dummy_backend): + cube = DataCube.load_stac( + "https://stac.test/data", + spatial_extent="https://geo.test/geometry.json", + connection=dummy_backend.connection, + ) + cube.execute() + assert dummy_backend.get_sync_pg() == { + "loadurl1": { + "process_id": "load_url", + "arguments": {"format": "GeoJSON", "url": "https://geo.test/geometry.json"}, + }, + "loadstac1": { + "process_id": "load_stac", + "arguments": { + "url": "https://stac.test/data", + "spatial_extent": {"from_node": "loadurl1"}, + }, + "result": True, + }, + } + + def test_load_stac_spatial_extent_parameter(self, dummy_backend): + spatial_extent = Parameter.geojson("zpatial_extent") + cube = DataCube.load_stac( + "https://stac.test/data", spatial_extent=spatial_extent, connection=dummy_backend.connection + ) + cube.execute() + assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == { + "url": "https://stac.test/data", + "spatial_extent": {"from_parameter": "zpatial_extent"}, + } def test_filter_temporal_basic_positional_args(s2cube): im = s2cube.filter_temporal("2016-01-01", "2016-03-10") diff --git a/tests/rest/test_connection.py b/tests/rest/test_connection.py index 408d79bd0..16f5cb894 100644 --- a/tests/rest/test_connection.py +++ b/tests/rest/test_connection.py @@ -2460,7 +2460,7 @@ def test_load_collection_spatial_extent_bbox(self, dummy_backend): cube.execute() assert dummy_backend.get_sync_pg()["loadcollection1"]["arguments"] == { "id": "S2", - "spatial_extent": {"east": 3, "north": 4, "south": 2, "west": 1}, + "spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4}, "temporal_extent": None, } @@ -2527,7 +2527,7 @@ def test_load_collection_spatial_extent_shapely_wrong_type(self, geojson, dummy_ ], ) @pytest.mark.parametrize("path_factory", [str, Path]) - def test_load_collection_spatial_extent_path(self, geojson, dummy_backend, tmp_path, path_factory): + def test_load_collection_spatial_extent_local_path(self, geojson, dummy_backend, tmp_path, path_factory): path = tmp_path / "geometry.json" with path.open("w") as f: json.dump(geojson, f) @@ -2935,8 +2935,8 @@ def test_load_stac_spatial_extent_bbox(self, dummy_backend): cube = dummy_backend.connection.load_stac("https://stac.test/data", spatial_extent=spatial_extent) cube.execute() assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == { - "spatial_extent": {"east": 3, "north": 4, "south": 2, "west": 1}, "url": "https://stac.test/data", + "spatial_extent": {"west": 1, "south": 2, "east": 3, "north": 4}, } @pytest.mark.parametrize( @@ -2953,8 +2953,8 @@ def test_load_stac_spatial_extent_geojson(self, dummy_backend, spatial_extent): cube = dummy_backend.connection.load_stac("https://stac.test/data", spatial_extent=spatial_extent) cube.execute() assert dummy_backend.get_sync_pg()["loadstac1"]["arguments"] == { - "spatial_extent": spatial_extent, "url": "https://stac.test/data", + "spatial_extent": spatial_extent, } @pytest.mark.parametrize( @@ -3008,7 +3008,7 @@ def test_load_stac_spatial_extent_shapely_wront_type(self, dummy_backend, geojso ], ) @pytest.mark.parametrize("path_factory", [str, Path]) - def test_load_stac_spatial_extent_path(self, geojson, dummy_backend, tmp_path, path_factory): + def test_load_stac_spatial_extent_local_path(self, geojson, dummy_backend, tmp_path, path_factory): path = tmp_path / "geometry.json" with path.open("w") as f: json.dump(geojson, f)