Skip to content

Commit 8716d00

Browse files
committed
Issue #509 document Parameter support for load_collection and filter_bbox
also warn when parameter schema is invalid
1 parent bc9e852 commit 8716d00

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

openeo/rest/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ def datacube_from_json(self, src: Union[str, Path], parameters: Optional[dict] =
11401140
def load_collection(
11411141
self,
11421142
collection_id: Union[str, Parameter],
1143-
spatial_extent: Optional[Dict[str, float]] = None,
1143+
spatial_extent: Union[Dict[str, float], Parameter, None] = None,
11441144
temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None,
11451145
bands: Union[None, List[str], Parameter] = None,
11461146
properties: Union[

openeo/rest/datacube.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def load_collection(
121121
cls,
122122
collection_id: Union[str, Parameter],
123123
connection: Connection = None,
124-
spatial_extent: Optional[Dict[str, float]] = None,
124+
spatial_extent: Union[Dict[str, float], Parameter, None] = None,
125125
temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None,
126126
bands: Union[None, List[str], Parameter] = None,
127127
fetch_metadata: bool = True,
@@ -157,6 +157,13 @@ def load_collection(
157157
"""
158158
if temporal_extent:
159159
temporal_extent = cls._get_temporal_extent(extent=temporal_extent)
160+
161+
if isinstance(spatial_extent, Parameter):
162+
if spatial_extent.schema.get("type") != "object":
163+
warnings.warn(
164+
"Unexpected parameterized `spatial_extent` in `load_collection`:"
165+
f" expected schema with type 'object' but got {spatial_extent.schema!r}."
166+
)
160167
arguments = {
161168
'id': collection_id,
162169
# TODO: spatial_extent could also be a "geojson" subtype object, so we might want to allow (and convert) shapely shapes as well here.
@@ -250,11 +257,13 @@ def _get_temporal_extent(
250257
return args[0]
251258
elif len(args) == 0 and isinstance(extent, Parameter):
252259
assert start_date is None and end_date is None
260+
# TODO: warn about unexpected parameter schema
253261
return extent
254262
else:
255263
def convertor(d: Any) -> Any:
256264
# TODO: can this be generalized through _FromNodeMixin?
257265
if isinstance(d, Parameter) or isinstance(d, PGNode):
266+
# TODO: warn about unexpected parameter schema
258267
return d
259268
elif isinstance(d, ProcessBuilderBase):
260269
return d.pgnode
@@ -385,6 +394,11 @@ def filter_bbox(
385394
raise ValueError(args)
386395

387396
if isinstance(bbox, Parameter):
397+
if bbox.schema.get("type") != "object":
398+
warnings.warn(
399+
"Unexpected parameterized `extent` in `filter_bbox`:"
400+
f" expected schema with type 'object' but got {bbox.schema!r}."
401+
)
388402
extent = bbox
389403
else:
390404
if bbox:

tests/rest/datacube/test_datacube100.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,32 @@ def test_filter_bbox_parameter(con100: Connection):
267267
assert _get_leaf_node(cube) == expected
268268

269269

270+
def test_filter_bbox_parameter_invalid_schema(con100: Connection):
271+
expected = {
272+
"process_id": "filter_bbox",
273+
"arguments": {
274+
"data": {"from_node": "loadcollection1"},
275+
"extent": {"from_parameter": "my_bbox"},
276+
},
277+
"result": True,
278+
}
279+
bbox_param = Parameter(name="my_bbox", schema={"type": "string"})
280+
281+
with pytest.warns(
282+
UserWarning,
283+
match="Unexpected parameterized `extent` in `filter_bbox`: expected schema with type 'object' but got {'type': 'string'}.",
284+
):
285+
cube = con100.load_collection("S2").filter_bbox(bbox_param)
286+
assert _get_leaf_node(cube) == expected
287+
288+
with pytest.warns(
289+
UserWarning,
290+
match="Unexpected parameterized `extent` in `filter_bbox`: expected schema with type 'object' but got {'type': 'string'}.",
291+
):
292+
cube = con100.load_collection("S2").filter_bbox(bbox=bbox_param)
293+
assert _get_leaf_node(cube) == expected
294+
295+
270296
@pytest.mark.parametrize(["args", "expected"], [
271297
((3, 4, 52, 51,), {"west": 3, "south": 51, "east": 4, "north": 52}),
272298
((3, 4, 52, 51, 4326,), {"west": 3, "south": 51, "east": 4, "north": 52, "crs": 4326}),

tests/rest/test_udp.py

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ def test_delete(con100, requests_mock):
244244
assert adapter.called
245245

246246

247-
def test_build_parameterized_cube_basic(con100):
248-
layer = Parameter.string("layer")
249-
dates = Parameter.string("dates")
250-
bbox = Parameter("bbox", schema="object")
247+
def test_build_parameterized_cube_basic(con100, recwarn):
248+
layer = Parameter.string("layer", description="Collection Id")
249+
dates = Parameter.string("dates", description="Temporal extent")
250+
bbox = Parameter("bbox", schema="object", description="bbox")
251251
cube = con100.load_collection(layer).filter_temporal(dates).filter_bbox(bbox)
252252

253253
assert cube.flat_graph() == {
@@ -265,6 +265,7 @@ def test_build_parameterized_cube_basic(con100):
265265
"result": True,
266266
}
267267
}
268+
assert recwarn.list == []
268269

269270

270271
def test_build_parameterized_cube_single_date(con100):
@@ -293,10 +294,10 @@ def test_build_parameterized_cube_single_date(con100):
293294
}
294295

295296

296-
def test_build_parameterized_cube_start_date(con100):
297-
layer = Parameter.string("layer")
298-
start = Parameter.string("start")
299-
bbox = Parameter("bbox", schema="object")
297+
def test_build_parameterized_cube_start_date(con100, recwarn):
298+
layer = Parameter.string("layer", description="Collection id")
299+
start = Parameter.string("start", description="Start date")
300+
bbox = Parameter("bbox", schema="object", description="Bbox")
300301
cube = con100.load_collection(layer).filter_temporal(start, None).filter_bbox(bbox)
301302

302303
assert cube.flat_graph() == {
@@ -314,12 +315,13 @@ def test_build_parameterized_cube_start_date(con100):
314315
"result": True,
315316
}
316317
}
318+
assert recwarn.list == []
317319

318320

319-
def test_build_parameterized_cube_load_collection(con100):
320-
layer = Parameter.string("layer")
321-
dates = Parameter.string("dates")
322-
bbox = Parameter("bbox", schema="object")
321+
def test_build_parameterized_cube_load_collection(con100, recwarn):
322+
layer = Parameter.string("layer", description="Collection id")
323+
dates = Parameter.string("dates", description="temporal extent")
324+
bbox = Parameter("bbox", schema="object", description="bbox")
323325
cube = con100.load_collection(layer, spatial_extent=bbox, temporal_extent=dates)
324326

325327
assert cube.flat_graph() == {
@@ -333,6 +335,52 @@ def test_build_parameterized_cube_load_collection(con100):
333335
"result": True,
334336
}
335337
}
338+
assert recwarn.list == []
339+
340+
341+
def test_build_parameterized_cube_load_collection_invalid_bbox_schema(con100):
342+
layer = Parameter.string("layer", description="Collection id")
343+
dates = Parameter.string("dates", description="Temporal extent")
344+
bbox = Parameter.string("bbox", description="Spatial extent")
345+
with pytest.warns(
346+
UserWarning,
347+
match="Unexpected parameterized `spatial_extent` in `load_collection`: expected schema with type 'object' but got {'type': 'string'}.",
348+
):
349+
cube = con100.load_collection(layer, spatial_extent=bbox, temporal_extent=dates)
350+
351+
assert cube.flat_graph() == {
352+
"loadcollection1": {
353+
"process_id": "load_collection",
354+
"arguments": {
355+
"id": {"from_parameter": "layer"},
356+
"temporal_extent": {"from_parameter": "dates"},
357+
"spatial_extent": {"from_parameter": "bbox"},
358+
},
359+
"result": True,
360+
}
361+
}
362+
363+
364+
def test_build_parameterized_cube_filter_bbox_invalid_schema(con100):
365+
layer = Parameter.string("layer", description="Collection id")
366+
bbox = Parameter.string("bbox", description="Spatial extent")
367+
with pytest.warns(
368+
UserWarning,
369+
match="Unexpected parameterized `extent` in `filter_bbox`: expected schema with type 'object' but got {'type': 'string'}.",
370+
):
371+
cube = con100.load_collection(layer).filter_bbox(bbox)
372+
373+
assert cube.flat_graph() == {
374+
"loadcollection1": {
375+
"process_id": "load_collection",
376+
"arguments": {"id": {"from_parameter": "layer"}, "temporal_extent": None, "spatial_extent": None},
377+
},
378+
"filterbbox1": {
379+
"process_id": "filter_bbox",
380+
"arguments": {"data": {"from_node": "loadcollection1"}, "extent": {"from_parameter": "bbox"}},
381+
"result": True,
382+
},
383+
}
336384

337385

338386
def test_build_parameterized_cube_load_collection_band(con100):

0 commit comments

Comments
 (0)