Skip to content

Commit c95d1f1

Browse files
committed
Issue #287 Support DriverVectorCube in apply_polygon
other refs: #288, d180c24
1 parent 605f153 commit c95d1f1

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ and start a new "In Progress" section above it.
2121

2222
## In progress
2323

24+
## 0.102.2
25+
26+
- Support `DriverVectorCube` in `apply_polygon` ([#287](https://github.com/Open-EO/openeo-python-driver/issues/287))
27+
2428
## 0.102.0
2529

2630
- Emit "in" operator ([Open-EO/openeo-opensearch-client#32](https://github.com/Open-EO/openeo-opensearch-client/issues/32),

openeo_driver/ProcessGraphDeserializer.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -866,19 +866,19 @@ def apply_polygon(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
866866
reason = "{m!s} is not a polygon.".format(m=p)
867867
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
868868
polygon = MultiPolygon(polygons)
869+
elif isinstance(polygons, DriverVectorCube):
870+
# TODO #288: I know it's wrong to coerce to MultiPolygon here, but we stick to this ill-defined API for now.
871+
polygon = polygons.to_multipolygon()
869872
elif isinstance(polygons, shapely.geometry.base.BaseGeometry):
870873
polygon = MultiPolygon(polygons)
871874
elif isinstance(polygons, dict):
872875
polygon = geojson_to_multipolygon(polygons)
873876
if isinstance(polygon, shapely.geometry.Polygon):
874877
polygon = MultiPolygon([polygon])
875-
elif isinstance(polygons, str):
876-
# Delayed vector is not supported yet.
877-
reason = "Polygon of type string is not yet supported."
878-
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
879878
else:
880-
reason = "Polygon type is not supported."
879+
reason = f"unsupported type: {type(polygons).__name__}"
881880
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
881+
882882
if polygon.area == 0:
883883
reason = "Polygon {m!s} has an area of {a!r}".format(m=polygon, a=polygon.area)
884884
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)

openeo_driver/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.102.1a1"
1+
__version__ = "0.102.2a1"

tests/data/pg/1.0/apply_polygon.json

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
}
3131
]
3232
},
33-
"mask_value": -5,
3433
"process": {
3534
"process_graph": {
3635
"runudf1": {

tests/test_views_execute.py

+43
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import shutil
2+
13
import dataclasses
24
import json
35
import math
@@ -3691,6 +3693,47 @@ def test_apply_polygon(api):
36913693
assert params["spatial_extent"] == {"west": 1.0, "south": 5.0, "east": 12.0, "north": 16.0, "crs": "EPSG:4326"}
36923694

36933695

3696+
def test_apply_polygon_with_vector_cube(api, tmp_path):
3697+
shutil.copy(get_path("geojson/FeatureCollection01.json"), tmp_path / "geometry.json")
3698+
with ephemeral_fileserver(tmp_path) as fileserver_root:
3699+
url = f"{fileserver_root}/geometry.json"
3700+
3701+
pg = {
3702+
"load_raster": {
3703+
"process_id": "load_collection",
3704+
"arguments": {"id": "S2_FOOBAR"},
3705+
},
3706+
"load_vector": {
3707+
"process_id": "load_url",
3708+
"arguments": {"url": str(url), "format": "GeoJSON"},
3709+
},
3710+
"apply_polygon": {
3711+
"process_id": "apply_polygon",
3712+
"arguments": {
3713+
"data": {"from_node": "load_raster"},
3714+
"polygons": {"from_node": "load_vector"},
3715+
"process": {
3716+
"process_graph": {
3717+
"constant": {
3718+
"process_id": "constant",
3719+
"arguments": {"x": {"from_parameter": "x"}},
3720+
"result": True,
3721+
}
3722+
}
3723+
},
3724+
},
3725+
"result": True,
3726+
},
3727+
}
3728+
_ = api.check_result(pg)
3729+
dummy = dummy_backend.get_collection("S2_FOOBAR")
3730+
assert dummy.apply_polygon.call_count == 1
3731+
polygons = dummy.apply_polygon.call_args.kwargs["polygons"]
3732+
# TODO #288 instead of MultPolygon, this should actually be a vector cube, feature collection or something equivalent
3733+
assert isinstance(polygons, shapely.geometry.MultiPolygon)
3734+
assert polygons.bounds == (4.45, 51.1, 4.52, 51.2)
3735+
3736+
36943737
def test_fit_class_random_forest(api):
36953738
res = api.check_result("fit_class_random_forest.json")
36963739

0 commit comments

Comments
 (0)