Skip to content

Commit

Permalink
fixup! PR #200 finetune VectorCube.apply_dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Aug 1, 2023
1 parent 3c2f0e9 commit 26c7c1a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion openeo_driver/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def apply_dimension(

if single_run_udf:
# Process with single "run_udf" node
if self._cube is None and dimension == self.DIM_GEOMETRIES and target_dimension is Non:
if self._cube is None and dimension == self.DIM_GEOMETRIES and target_dimension is None:
# TODO: this is non-standard special case: vector cube with only geometries, but no "cube" data
feature_collection = openeo.udf.FeatureCollection(id="_", data=self._as_geopandas_df())
udf_data = openeo.udf.UdfData(
Expand Down
54 changes: 54 additions & 0 deletions tests/test_vectorcube.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import textwrap

import geopandas as gpd
import numpy.testing
import pyproj
Expand All @@ -9,6 +11,7 @@
from openeo_driver.datacube import DriverVectorCube
from openeo_driver.testing import DictSubSet, ApproxGeometry
from openeo_driver.util.geometry import as_geojson_feature_collection
from openeo_driver.utils import EvalEnv

from .data import get_path

Expand All @@ -22,6 +25,10 @@ def gdf(self) -> gpd.GeoDataFrame:
df = gpd.read_file(path)
return df

@pytest.fixture
def vc(self, gdf) -> DriverVectorCube:
return DriverVectorCube(geometries=gdf)

def test_basic(self, gdf):
vc = DriverVectorCube(gdf)
assert vc.get_bounding_box() == (1, 1, 5, 4)
Expand Down Expand Up @@ -446,3 +453,50 @@ def test_buffer_points(self):
],
}
)

def test_apply_dimension_run_udf(self, vc, backend_implementation):
udf = textwrap.dedent(
"""
from openeo.udf import UdfData, FeatureCollection
def process_geometries(udf_data: UdfData) -> UdfData:
[feature_collection] = udf_data.get_feature_collection_list()
gdf = feature_collection.data
gdf["geometry"] = gdf["geometry"].buffer(distance=1, resolution=2)
udf_data.set_feature_collection_list([
FeatureCollection(id="_", data=gdf),
])
"""
)
callback = {
"runudf1": {
"process_id": "run_udf",
"arguments": {"data": {"from_parameter": "data"}, "udf": udf, "runtime": "Python"},
"result": True,
}
}
env = EvalEnv({"backend_implementation": backend_implementation})
result = vc.apply_dimension(process=callback, dimension="geometries", env=env)
assert isinstance(result, DriverVectorCube)
feature_collection = result.to_geojson()
assert feature_collection == DictSubSet(
{
"type": "FeatureCollection",
"bbox": pytest.approx((0, 0, 6, 5), abs=0.1),
"features": [
{
"type": "Feature",
"bbox": pytest.approx((0, 0, 4, 4), abs=0.1),
"geometry": DictSubSet({"type": "Polygon"}),
"id": "0",
"properties": {"id": "first", "pop": 1234},
},
{
"type": "Feature",
"bbox": pytest.approx((2, 1, 6, 5), abs=0.1),
"geometry": DictSubSet({"type": "Polygon"}),
"id": "1",
"properties": {"id": "second", "pop": 5678},
},
],
}
)

0 comments on commit 26c7c1a

Please sign in to comment.