Skip to content

Commit 487e5c0

Browse files
committed
Tighten resample_spatial/resample_cube_spatial argument handling
related to #347 and Open-EO/openeo-python-client#690
1 parent df49864 commit 487e5c0

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

openeo_driver/ProcessGraphDeserializer.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
Processing,
4141
OpenEoBackendImplementation,
4242
)
43+
from openeo_driver.constants import RESAMPLE_SPATIAL_METHODS, RESAMPLE_SPATIAL_ALIGNS
4344
from openeo_driver.datacube import (
4445
DriverDataCube,
4546
DriverVectorCube,
@@ -1582,24 +1583,23 @@ def ndvi(args: dict, env: EvalEnv) -> DriverDataCube:
15821583
@process
15831584
def resample_spatial(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
15841585
cube: DriverDataCube = args.get_required("data", expected_type=DriverDataCube)
1585-
resolution = args.get_optional("resolution", 0)
1586-
projection = args.get_optional("projection", None)
1587-
method = args.get_optional("method", "near")
1588-
align = args.get_optional("align", "upper-left")
1586+
resolution = args.get_optional(
1587+
"resolution",
1588+
default=0,
1589+
validator=lambda v: isinstance(v, (int, float)) or (isinstance(v, (tuple, list)) and len(v) == 2),
1590+
)
1591+
projection = args.get_optional("projection", default=None)
1592+
method = args.get_enum("method", options=RESAMPLE_SPATIAL_METHODS, default="near")
1593+
align = args.get_enum("align", options=RESAMPLE_SPATIAL_ALIGNS, default="upper-left")
15891594
return cube.resample_spatial(resolution=resolution, projection=projection, method=method, align=align)
15901595

15911596

15921597
@process
1593-
def resample_cube_spatial(args: dict, env: EvalEnv) -> DriverDataCube:
1594-
image_collection = extract_arg(args, 'data')
1595-
target_image_collection = extract_arg(args, 'target')
1596-
method = args.get('method', 'near')
1597-
if not isinstance(image_collection, DriverDataCube):
1598-
raise ProcessParameterInvalidException(
1599-
parameter="data", process="resample_cube_spatial",
1600-
reason=f"Invalid data type {type(image_collection)!r} expected raster-cube."
1601-
)
1602-
return image_collection.resample_cube_spatial(target=target_image_collection, method=method)
1598+
def resample_cube_spatial(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
1599+
cube: DriverDataCube = args.get_required("data", expected_type=DriverDataCube)
1600+
target: DriverDataCube = args.get_required("target", expected_type=DriverDataCube)
1601+
method = args.get_enum("method", options=RESAMPLE_SPATIAL_METHODS, default="near")
1602+
return cube.resample_cube_spatial(target=target, method=method)
16031603

16041604

16051605
@process

openeo_driver/constants.py

+27
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,30 @@ class JOB_STATUS:
2222
CANCELED = "canceled"
2323
FINISHED = "finished"
2424
ERROR = "error"
25+
26+
27+
# Resample methods as used in official specs of `resample_spatial` and `resample_cube_spatial`
28+
RESAMPLE_SPATIAL_METHODS = [
29+
"average",
30+
"bilinear",
31+
"cubic",
32+
"cubicspline",
33+
"lanczos",
34+
"max",
35+
"med",
36+
"min",
37+
"mode",
38+
"near",
39+
"q1",
40+
"q3",
41+
"rms",
42+
"sum",
43+
]
44+
45+
# Align options as used in official spec of `resample_spatial`
46+
RESAMPLE_SPATIAL_ALIGNS = [
47+
"lower-left",
48+
"upper-left",
49+
"lower-right",
50+
"upper-right",
51+
]

tests/data/pg/1.0/resample_and_merge_cubes.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"target": {
2121
"from_node": "collection2"
2222
},
23-
"method": "cube"
23+
"method": "cubic"
2424
},
2525
"result": false
2626
},
@@ -52,4 +52,4 @@
5252
},
5353
"result": true
5454
}
55-
}
55+
}

tests/test_views_execute.py

+1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ def test_execute_resample_and_merge_cubes(api):
522522
assert last_load_collection_call.target_resolution == [10, 10]
523523
assert dummy.merge_cubes.call_count == 1
524524
assert dummy.resample_cube_spatial.call_count == 1
525+
assert dummy.resample_cube_spatial.call_args.kwargs["method"] == "cubic"
525526
args, kwargs = dummy.merge_cubes.call_args
526527
assert args[1:] == ('or',)
527528

0 commit comments

Comments
 (0)