Skip to content

Commit 48db4a9

Browse files
committed
Issue #229 fixup apply_polygon "polygons" argument
1 parent 957f081 commit 48db4a9

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

CHANGELOG.md

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

2020
## In progress
2121

22+
## 0.77.2
23+
24+
- fixup "polygons" argument of "apply_polygon" ([#229](https://github.com/Open-EO/openeo-python-driver/issues/229))
25+
2226
## 0.76.1
2327

2428
- Attempt to workaround issue with in-place process graph modification

openeo_driver/ProcessGraphDeserializer.py

+41-3
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,7 @@ def reduce_dimension(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
789789
@process_registry_100.add_function(
790790
spec=read_spec("openeo-processes/experimental/chunk_polygon.json"), name="chunk_polygon"
791791
)
792-
@process_registry_100.add_function(spec=read_spec("openeo-processes/2.x/proposals/apply_polygon.json"))
793-
@process_registry_2xx.add_function(spec=read_spec("openeo-processes/2.x/proposals/apply_polygon.json"))
794-
def apply_polygon(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
792+
def chunk_polygon(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
795793
# TODO #229 deprecate this process and promote the "apply_polygon" name.
796794
# See https://github.com/Open-EO/openeo-processes/issues/287, https://github.com/Open-EO/openeo-processes/pull/298
797795
data_cube = args.get_required("data", expected_type=DriverDataCube)
@@ -829,6 +827,46 @@ def apply_polygon(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
829827
return data_cube.chunk_polygon(reducer=reduce_pg, chunks=polygon, mask_value=mask_value, context=context, env=env)
830828

831829

830+
@process_registry_100.add_function(spec=read_spec("openeo-processes/2.x/proposals/apply_polygon.json"))
831+
@process_registry_2xx.add_function(spec=read_spec("openeo-processes/2.x/proposals/apply_polygon.json"))
832+
def apply_polygon(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
833+
data_cube = args.get_required("data", expected_type=DriverDataCube)
834+
process = args.get_deep("process", "process_graph", expected_type=dict)
835+
polygons = args.get_required("polygons")
836+
mask_value = args.get_optional("mask_value", expected_type=(int, float), default=None)
837+
context = args.get_optional("context", default=None)
838+
839+
# TODO #114 EP-3981 normalize first to vector cube and simplify logic
840+
# TODO: this logic (copied from original chunk_polygon implementation) coerces the input polygons
841+
# to a single MultiPolygon of pure (non-multi) polygons, which is conceptually wrong.
842+
# Instead it should normalize to a feature collection or vector cube.
843+
if isinstance(polygons, DelayedVector):
844+
polygons = list(polygons.geometries)
845+
for p in polygons:
846+
if not isinstance(p, shapely.geometry.Polygon):
847+
reason = "{m!s} is not a polygon.".format(m=p)
848+
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
849+
polygon = MultiPolygon(polygons)
850+
elif isinstance(polygons, shapely.geometry.base.BaseGeometry):
851+
polygon = MultiPolygon(polygons)
852+
elif isinstance(polygons, dict):
853+
polygon = geojson_to_multipolygon(polygons)
854+
if isinstance(polygon, shapely.geometry.Polygon):
855+
polygon = MultiPolygon([polygon])
856+
elif isinstance(polygons, str):
857+
# Delayed vector is not supported yet.
858+
reason = "Polygon of type string is not yet supported."
859+
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
860+
else:
861+
reason = "Polygon type is not supported."
862+
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
863+
if polygon.area == 0:
864+
reason = "Polygon {m!s} has an area of {a!r}".format(m=polygon, a=polygon.area)
865+
raise ProcessParameterInvalidException(parameter="polygons", process="apply_polygon", reason=reason)
866+
867+
return data_cube.apply_polygon(polygons=polygon, process=process, mask_value=mask_value, context=context, env=env)
868+
869+
832870
@process_registry_100.add_function(spec=read_spec("openeo-processes/experimental/fit_class_random_forest.json"))
833871
@process_registry_2xx.add_function(spec=read_spec("openeo-processes/experimental/fit_class_random_forest.json"))
834872
def fit_class_random_forest(args: dict, env: EvalEnv) -> DriverMlModel:

openeo_driver/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.77.1a1"
1+
__version__ = "0.77.2a1"

openeo_driver/datacube.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
import abc
23
import inspect
34
import io
@@ -125,9 +126,23 @@ def chunk_polygon(
125126
env: EvalEnv,
126127
context: Optional[dict] = None,
127128
) -> "DriverDataCube":
128-
# TODO: rename/update `chunk_polygon` to `apply_polygon` (https://github.com/Open-EO/openeo-processes/pull/298)
129+
# TODO #229 drop this deprecated API once unused (replaced by `apply_polygon`) (https://github.com/Open-EO/openeo-processes/pull/298)
129130
self._not_implemented()
130131

132+
def apply_polygon(
133+
self,
134+
*,
135+
# TODO #229 better type for `polygons` arg: should be vector cube or feature collection like construct
136+
polygons: shapely.geometry.base.BaseGeometry,
137+
process: dict,
138+
mask_value: Optional[float] = None,
139+
context: Optional[dict] = None,
140+
env: EvalEnv,
141+
) -> DriverDataCube:
142+
# TODO #229 remove this temporary adapter to deprecated `chunk_polygon` method.
143+
return self.chunk_polygon(reducer=process, chunks=polygons, mask_value=mask_value, env=env, context=context)
144+
# self._not_implemented()
145+
131146
def add_dimension(self, name: str, label, type: str = "other") -> 'DriverDataCube':
132147
self._not_implemented()
133148

tests/data/pg/1.0/apply_polygon.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"process_id": "apply_polygon",
88
"arguments": {
99
"data": {"from_node": "collection"},
10-
"chunks": {
10+
"polygons": {
1111
"type": "FeatureCollection",
1212
"features": [
1313
{

0 commit comments

Comments
 (0)