Skip to content

Commit

Permalink
Issue #197 best effort DriverVectorCube.to_legacy_save_result when no…
Browse files Browse the repository at this point in the history
… data
  • Loading branch information
soxofaan committed May 25, 2023
1 parent 35b4e38 commit 974dce5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 7 deletions.
4 changes: 4 additions & 0 deletions openeo_driver/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
103 changes: 96 additions & 7 deletions tests/test_views_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,27 +1261,116 @@ 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",
},
"result": "true"
}
}
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(
Expand Down

0 comments on commit 974dce5

Please sign in to comment.