Skip to content

Commit

Permalink
Issue #287 Support DriverVectorCube in apply_polygon
Browse files Browse the repository at this point in the history
other refs: #288, d180c24
  • Loading branch information
soxofaan committed Jun 7, 2024
1 parent 605f153 commit c95d1f1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ and start a new "In Progress" section above it.

## In progress

## 0.102.2

- Support `DriverVectorCube` in `apply_polygon` ([#287](https://github.com/Open-EO/openeo-python-driver/issues/287))

## 0.102.0

- Emit "in" operator ([Open-EO/openeo-opensearch-client#32](https://github.com/Open-EO/openeo-opensearch-client/issues/32),
Expand Down
10 changes: 5 additions & 5 deletions openeo_driver/ProcessGraphDeserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,19 +866,19 @@ def apply_polygon(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
reason = "{m!s} is not a polygon.".format(m=p)
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
polygon = MultiPolygon(polygons)
elif isinstance(polygons, DriverVectorCube):
# TODO #288: I know it's wrong to coerce to MultiPolygon here, but we stick to this ill-defined API for now.
polygon = polygons.to_multipolygon()
elif isinstance(polygons, shapely.geometry.base.BaseGeometry):
polygon = MultiPolygon(polygons)
elif isinstance(polygons, dict):
polygon = geojson_to_multipolygon(polygons)
if isinstance(polygon, shapely.geometry.Polygon):
polygon = MultiPolygon([polygon])
elif isinstance(polygons, str):
# Delayed vector is not supported yet.
reason = "Polygon of type string is not yet supported."
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
else:
reason = "Polygon type is not supported."
reason = f"unsupported type: {type(polygons).__name__}"
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)

if polygon.area == 0:
reason = "Polygon {m!s} has an area of {a!r}".format(m=polygon, a=polygon.area)
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
Expand Down
2 changes: 1 addition & 1 deletion openeo_driver/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.102.1a1"
__version__ = "0.102.2a1"
1 change: 0 additions & 1 deletion tests/data/pg/1.0/apply_polygon.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
}
]
},
"mask_value": -5,
"process": {
"process_graph": {
"runudf1": {
Expand Down
43 changes: 43 additions & 0 deletions tests/test_views_execute.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import shutil

import dataclasses
import json
import math
Expand Down Expand Up @@ -3691,6 +3693,47 @@ def test_apply_polygon(api):
assert params["spatial_extent"] == {"west": 1.0, "south": 5.0, "east": 12.0, "north": 16.0, "crs": "EPSG:4326"}


def test_apply_polygon_with_vector_cube(api, tmp_path):
shutil.copy(get_path("geojson/FeatureCollection01.json"), tmp_path / "geometry.json")
with ephemeral_fileserver(tmp_path) as fileserver_root:
url = f"{fileserver_root}/geometry.json"

pg = {
"load_raster": {
"process_id": "load_collection",
"arguments": {"id": "S2_FOOBAR"},
},
"load_vector": {
"process_id": "load_url",
"arguments": {"url": str(url), "format": "GeoJSON"},
},
"apply_polygon": {
"process_id": "apply_polygon",
"arguments": {
"data": {"from_node": "load_raster"},
"polygons": {"from_node": "load_vector"},
"process": {
"process_graph": {
"constant": {
"process_id": "constant",
"arguments": {"x": {"from_parameter": "x"}},
"result": True,
}
}
},
},
"result": True,
},
}
_ = api.check_result(pg)
dummy = dummy_backend.get_collection("S2_FOOBAR")
assert dummy.apply_polygon.call_count == 1
polygons = dummy.apply_polygon.call_args.kwargs["polygons"]
# TODO #288 instead of MultPolygon, this should actually be a vector cube, feature collection or something equivalent
assert isinstance(polygons, shapely.geometry.MultiPolygon)
assert polygons.bounds == (4.45, 51.1, 4.52, 51.2)


def test_fit_class_random_forest(api):
res = api.check_result("fit_class_random_forest.json")

Expand Down

0 comments on commit c95d1f1

Please sign in to comment.