|
10 | 10 |
|
11 | 11 | from openeo_driver.datacube import DriverVectorCube
|
12 | 12 | from openeo_driver.errors import OpenEOApiException
|
13 |
| -from openeo_driver.testing import ApproxGeometry, DictSubSet, IsNan |
| 13 | +from openeo_driver.testing import ApproxGeometry, DictSubSet, IsNan, ApproxGeoJSONByBounds |
14 | 14 | from openeo_driver.util.geometry import as_geojson_feature_collection
|
15 | 15 | from openeo_driver.utils import EvalEnv
|
16 | 16 |
|
@@ -808,7 +808,7 @@ def test_apply_dimension_run_udf_change_geometry(self, gdf, backend_implementati
|
808 | 808 | udf = textwrap.dedent(
|
809 | 809 | """
|
810 | 810 | from openeo.udf import UdfData, FeatureCollection
|
811 |
| - def process_geometries(udf_data: UdfData) -> UdfData: |
| 811 | + def process_vector_cube(udf_data: UdfData) -> UdfData: |
812 | 812 | [feature_collection] = udf_data.get_feature_collection_list()
|
813 | 813 | gdf = feature_collection.data
|
814 | 814 | gdf["geometry"] = gdf["geometry"].buffer(distance=1, resolution=2)
|
@@ -850,3 +850,62 @@ def process_geometries(udf_data: UdfData) -> UdfData:
|
850 | 850 | ],
|
851 | 851 | }
|
852 | 852 | )
|
| 853 | + |
| 854 | + @pytest.mark.parametrize("dimension", ["bands", "properties"]) |
| 855 | + def test_apply_dimension_run_udf_add_properties(self, gdf, backend_implementation, dimension): |
| 856 | + vc = DriverVectorCube.from_geodataframe(gdf, dimension_name=dimension) |
| 857 | + udf = textwrap.dedent( |
| 858 | + """ |
| 859 | + from openeo.udf import UdfData, FeatureCollection |
| 860 | + def process_vector_cube(udf_data: UdfData) -> UdfData: |
| 861 | + [feature_collection] = udf_data.get_feature_collection_list() |
| 862 | + gdf = feature_collection.data |
| 863 | + gdf["popone"] = gdf["pop"] + 1 |
| 864 | + gdf["poppop"] = gdf["pop"] ** 2 |
| 865 | + udf_data.set_feature_collection_list([ |
| 866 | + FeatureCollection(id="_", data=gdf), |
| 867 | + ]) |
| 868 | + """ |
| 869 | + ) |
| 870 | + callback = { |
| 871 | + "runudf1": { |
| 872 | + "process_id": "run_udf", |
| 873 | + "arguments": {"data": {"from_parameter": "data"}, "udf": udf, "runtime": "Python"}, |
| 874 | + "result": True, |
| 875 | + } |
| 876 | + } |
| 877 | + env = EvalEnv({"backend_implementation": backend_implementation}) |
| 878 | + result = vc.apply_dimension(process=callback, dimension=dimension, env=env) |
| 879 | + assert isinstance(result, DriverVectorCube) |
| 880 | + assert result.to_internal_json() == { |
| 881 | + "geometries": { |
| 882 | + "type": "FeatureCollection", |
| 883 | + "features": [ |
| 884 | + { |
| 885 | + "type": "Feature", |
| 886 | + "geometry": ApproxGeoJSONByBounds(1, 1, 3, 3, types=["Polygon"], abs=0.01), |
| 887 | + "id": "0", |
| 888 | + "properties": {"id": "first", "pop": 1234, "popone": 1235, "poppop": 1522756}, |
| 889 | + "bbox": pytest.approx((1, 1, 3, 3), abs=0.01), |
| 890 | + }, |
| 891 | + { |
| 892 | + "type": "Feature", |
| 893 | + "geometry": ApproxGeoJSONByBounds(3, 2, 5, 4, types=["Polygon"], abs=0.01), |
| 894 | + "id": "1", |
| 895 | + "properties": {"id": "second", "pop": 5678, "popone": 5679, "poppop": 32239684}, |
| 896 | + "bbox": pytest.approx((3, 2, 5, 4), abs=0.01), |
| 897 | + }, |
| 898 | + ], |
| 899 | + "bbox": pytest.approx((1, 1, 5, 4), abs=0.01), |
| 900 | + }, |
| 901 | + "cube": { |
| 902 | + "name": None, |
| 903 | + "dims": ("geometries", "properties"), |
| 904 | + "coords": { |
| 905 | + "geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)}, |
| 906 | + "properties": {"attrs": {}, "data": ["pop", "popone", "poppop"], "dims": ("properties",)}, |
| 907 | + }, |
| 908 | + "data": [[1234, 1235, 1522756], [5678, 5679, 32239684]], |
| 909 | + "attrs": {}, |
| 910 | + }, |
| 911 | + } |
0 commit comments