From 974dce537896617c3dce2e1f6b3f3f0cefaf0732 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Thu, 25 May 2023 11:26:12 +0200 Subject: [PATCH] Issue #197 best effort DriverVectorCube.to_legacy_save_result when no data --- openeo_driver/datacube.py | 4 ++ tests/test_views_execute.py | 103 +++++++++++++++++++++++++++++++++--- 2 files changed, 100 insertions(+), 7 deletions(-) diff --git a/openeo_driver/datacube.py b/openeo_driver/datacube.py index 93042bc2..218ee078 100644 --- a/openeo_driver/datacube.py +++ b/openeo_driver/datacube.py @@ -377,6 +377,10 @@ def to_legacy_save_result(self) -> Union["AggregatePolygonResult", "JSONResult"] # TODO: eliminate these legacy, non-standard formats? from openeo_driver.save_result import AggregatePolygonResult, JSONResult + if self._cube is None: + # No cube: no real data to return (in legacy style), so let's just return a `null` per geometry. + return JSONResult(data=[None] * self.geometry_count()) + cube = self._cube # TODO: more flexible temporal/band dimension detection? if cube.dims == (self.DIM_GEOMETRIES, "t"): diff --git a/tests/test_views_execute.py b/tests/test_views_execute.py index e2219dc5..b9790379 100644 --- a/tests/test_views_execute.py +++ b/tests/test_views_execute.py @@ -1261,17 +1261,62 @@ def fct_buffer(udf_data: UdfData): return udf_data """, ]) -def test_run_udf_on_vector(api100, udf_code): +def test_run_udf_on_vector_read_vector(api100, udf_code): udf_code = textwrap.dedent(udf_code) process_graph = { - "geojson_file": { + "get_vector_data": { "process_id": "read_vector", - "arguments": {"filename": str(get_path("geojson/GeometryCollection01.json"))}, + "arguments": {"filename": str(get_path("geojson/FeatureCollection01.json"))}, }, "udf": { "process_id": "run_udf", "arguments": { - "data": {"from_node": "geojson_file"}, + "data": {"from_node": "get_vector_data"}, + "udf": udf_code, + "runtime": "Python", + }, + "result": "true", + }, + } + resp = api100.check_result(process_graph) + assert resp.json == [ + { + "type": "Polygon", + "coordinates": [[[4.47, 51.1], [4.52, 51.1], [4.52, 51.15], [4.47, 51.15], [4.47, 51.1]]], + }, + { + "type": "Polygon", + "coordinates": [[[4.45, 51.17], [4.5, 51.17], [4.5, 51.2], [4.45, 51.2], [4.45, 51.17]]], + }, + ] + + +@pytest.mark.parametrize( + "udf_code", + [ + """ + from openeo_udf.api.datacube import DataCube # Old style openeo_udf API + def fct_buffer(udf_data: UdfData): + return udf_data + """, + """ + from openeo.udf import UdfData + def fct_buffer(udf_data: UdfData): + return udf_data + """, + ], +) +def test_run_udf_on_vector_get_geometries(api100, udf_code): + udf_code = textwrap.dedent(udf_code) + process_graph = { + "get_vector_data": { + "process_id": "get_geometries", + "arguments": {"filename": str(get_path("geojson/FeatureCollection01.json"))}, + }, + "udf": { + "process_id": "run_udf", + "arguments": { + "data": {"from_node": "get_vector_data"}, "udf": udf_code, "runtime": "Python", }, @@ -1279,9 +1324,53 @@ def test_run_udf_on_vector(api100, udf_code): } } resp = api100.check_result(process_graph) - print(resp.json) - assert len(resp.json) == 2 - assert resp.json[0]['type'] == 'Polygon' + assert resp.json == [ + { + "type": "Polygon", + "coordinates": [[[4.47, 51.1], [4.52, 51.1], [4.52, 51.15], [4.47, 51.15], [4.47, 51.1]]], + }, + { + "type": "Polygon", + "coordinates": [[[4.45, 51.17], [4.5, 51.17], [4.5, 51.2], [4.45, 51.2], [4.45, 51.17]]], + }, + ] + + +@pytest.mark.parametrize( + "udf_code", + [ + """ + from openeo_udf.api.datacube import DataCube # Old style openeo_udf API + def fct_buffer(udf_data: UdfData): + return udf_data + """, + """ + from openeo.udf import UdfData + def fct_buffer(udf_data: UdfData): + return udf_data + """, + ], +) +def test_run_udf_on_vector_load_uploaded_files(api100, udf_code): + """https://github.com/Open-EO/openeo-python-driver/issues/197""" + udf_code = textwrap.dedent(udf_code) + process_graph = { + "get_vector_data": { + "process_id": "load_uploaded_files", + "arguments": {"paths": [str(get_path("geojson/FeatureCollection01.json"))], "format": "GeoJSON"}, + }, + "udf": { + "process_id": "run_udf", + "arguments": { + "data": {"from_node": "get_vector_data"}, + "udf": udf_code, + "runtime": "Python", + }, + "result": "true", + }, + } + resp = api100.check_result(process_graph) + assert resp.json == [None, None] @pytest.mark.parametrize(