Skip to content

Commit 8cadb84

Browse files
issue #678 support shapely and local path in load collection spatial extent
1 parent ef50a25 commit 8cadb84

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Automatically use `load_url` when providing a URL as geometries to `DataCube.aggregate_spatial()`, `DataCube.mask_polygon()`, etc. ([#104](https://github.com/Open-EO/openeo-python-client/issues/104), [#457](https://github.com/Open-EO/openeo-python-client/issues/457))
13+
- Argument `spatial_extent` in `load_collection` supports type `shapely` and loading geometry from a local path.
1314

1415
### Changed
1516

openeo/rest/connection.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ def datacube_from_json(self, src: Union[str, Path], parameters: Optional[dict] =
12301230
def load_collection(
12311231
self,
12321232
collection_id: Union[str, Parameter],
1233-
spatial_extent: Union[Dict[str, float], Parameter, None] = None,
1233+
spatial_extent: Union[Dict[str, float], Parameter, shapely.geometry.base.BaseGeometry, None] = None,
12341234
temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None,
12351235
bands: Union[None, List[str], Parameter] = None,
12361236
properties: Union[
@@ -1243,7 +1243,12 @@ def load_collection(
12431243
Load a DataCube by collection id.
12441244
12451245
:param collection_id: image collection identifier
1246-
:param spatial_extent: limit data to specified bounding box or polygons
1246+
:param spatial_extent: limit data to specified bounding box or polygons. Can be provided in different ways:
1247+
- a shapely geometry
1248+
- a GeoJSON-style dictionary,
1249+
- a path (:py:class:`str` or :py:class:`~pathlib.Path`) to a local, client-side GeoJSON file,
1250+
which will be loaded automatically to get the geometries as GeoJSON construct.
1251+
- a :py:class:`~openeo.api.process.Parameter` instance.
12471252
:param temporal_extent: limit data to specified temporal interval.
12481253
Typically, just a two-item list or tuple containing start and end date.
12491254
See :ref:`filtering-on-temporal-extent-section` for more details on temporal extent handling and shorthand notation.

openeo/rest/datacube.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ def load_collection(
158158
:param collection_id: image collection identifier
159159
:param connection: The backend connection to use.
160160
Can be ``None`` to work without connection and collection metadata.
161-
:param spatial_extent: limit data to specified bounding box or polygons
161+
:param spatial_extent: limit data to specified bounding box or polygons. Can be provided in different ways:
162+
- a shapely geometry
163+
- a GeoJSON-style dictionary,
164+
- a path (:py:class:`str` or :py:class:`~pathlib.Path`) to a local, client-side GeoJSON file,
165+
which will be loaded automatically to get the geometries as GeoJSON construct.
166+
- a :py:class:`~openeo.api.process.Parameter` instance.
162167
:param temporal_extent: limit data to specified temporal interval.
163168
Typically, just a two-item list or tuple containing start and end date.
164169
See :ref:`filtering-on-temporal-extent-section` for more details on temporal extent handling and shorthand notation.
@@ -188,7 +193,7 @@ def load_collection(
188193
f" expected schema with type 'object' but got {spatial_extent.schema!r}."
189194
)
190195
valid_geojson_types = [
191-
"Polygon", "MultiPolygon", "GeometryCollection", "FeatureCollection"
196+
"Polygon", "MultiPolygon", "Feature", "FeatureCollection"
192197
]
193198
if spatial_extent and not (isinstance(spatial_extent, dict) and spatial_extent.keys() & {"west", "east", "north", "south"}):
194199
spatial_extent = _get_geometry_argument(argument=spatial_extent,valid_geojson_types=valid_geojson_types,connection=connection)

tests/rest/datacube/test_datacube.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ def test_load_collection_connectionless_shapely_spatial_extent(self):
149149
}
150150
}
151151

152+
@pytest.mark.parametrize("path_factory", [str, pathlib.Path])
153+
def test_load_collection_connectionless_local_path_spatial_extent(self, path_factory, test_data):
154+
path = path_factory(test_data.get_path("geojson/polygon02.json"))
155+
cube = DataCube.load_collection("T3", spatial_extent=path)
156+
assert cube.flat_graph() == {
157+
"loadcollection1": {
158+
"arguments": {"id": "T3", "spatial_extent":
159+
{"type": "Polygon", "coordinates": [[[3, 50], [4, 50], [4, 51], [3, 50]]]},
160+
"temporal_extent": None},
161+
"process_id": "load_collection",
162+
"result": True,
163+
}
164+
}
165+
152166
def test_load_collection_connectionless_save_result(self):
153167
cube = DataCube.load_collection("T3").save_result(format="GTiff")
154168
assert cube.flat_graph() == {

0 commit comments

Comments
 (0)