Skip to content

Commit

Permalink
Tighten resample_spatial/resample_cube_spatial argument handling
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jan 20, 2025
1 parent df49864 commit 487e5c0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
28 changes: 14 additions & 14 deletions openeo_driver/ProcessGraphDeserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Processing,
OpenEoBackendImplementation,
)
from openeo_driver.constants import RESAMPLE_SPATIAL_METHODS, RESAMPLE_SPATIAL_ALIGNS
from openeo_driver.datacube import (
DriverDataCube,
DriverVectorCube,
Expand Down Expand Up @@ -1582,24 +1583,23 @@ def ndvi(args: dict, env: EvalEnv) -> DriverDataCube:
@process
def resample_spatial(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
cube: DriverDataCube = args.get_required("data", expected_type=DriverDataCube)
resolution = args.get_optional("resolution", 0)
projection = args.get_optional("projection", None)
method = args.get_optional("method", "near")
align = args.get_optional("align", "upper-left")
resolution = args.get_optional(
"resolution",
default=0,
validator=lambda v: isinstance(v, (int, float)) or (isinstance(v, (tuple, list)) and len(v) == 2),
)
projection = args.get_optional("projection", default=None)
method = args.get_enum("method", options=RESAMPLE_SPATIAL_METHODS, default="near")
align = args.get_enum("align", options=RESAMPLE_SPATIAL_ALIGNS, default="upper-left")
return cube.resample_spatial(resolution=resolution, projection=projection, method=method, align=align)


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


@process
Expand Down
27 changes: 27 additions & 0 deletions openeo_driver/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,30 @@ class JOB_STATUS:
CANCELED = "canceled"
FINISHED = "finished"
ERROR = "error"


# Resample methods as used in official specs of `resample_spatial` and `resample_cube_spatial`
RESAMPLE_SPATIAL_METHODS = [
"average",
"bilinear",
"cubic",
"cubicspline",
"lanczos",
"max",
"med",
"min",
"mode",
"near",
"q1",
"q3",
"rms",
"sum",
]

# Align options as used in official spec of `resample_spatial`
RESAMPLE_SPATIAL_ALIGNS = [
"lower-left",
"upper-left",
"lower-right",
"upper-right",
]
4 changes: 2 additions & 2 deletions tests/data/pg/1.0/resample_and_merge_cubes.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"target": {
"from_node": "collection2"
},
"method": "cube"
"method": "cubic"
},
"result": false
},
Expand Down Expand Up @@ -52,4 +52,4 @@
},
"result": true
}
}
}
1 change: 1 addition & 0 deletions tests/test_views_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ def test_execute_resample_and_merge_cubes(api):
assert last_load_collection_call.target_resolution == [10, 10]
assert dummy.merge_cubes.call_count == 1
assert dummy.resample_cube_spatial.call_count == 1
assert dummy.resample_cube_spatial.call_args.kwargs["method"] == "cubic"
args, kwargs = dummy.merge_cubes.call_args
assert args[1:] == ('or',)

Expand Down

0 comments on commit 487e5c0

Please sign in to comment.