Skip to content

Commit 927a115

Browse files
committed
EP-3981 #114 add to_vector_cube process (custom) for converting GeoJson to VectorCube
1 parent ec74ca3 commit 927a115

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

openeo_driver/ProcessGraphDeserializer.py

+18
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ def aggregate_polygon(args: dict, env: EvalEnv) -> DriverDataCube:
946946
def aggregate_spatial(args: dict, env: EvalEnv) -> DriverDataCube:
947947
reduce_pg = extract_deep(args, "reducer", "process_graph")
948948
cube = extract_arg(args, 'data')
949+
# TODO: drop `target_dimension`? see https://github.com/Open-EO/openeo-processes/issues/366
949950
target_dimension = args.get('target_dimension', None)
950951

951952
geoms = extract_arg(args, 'geometries')
@@ -1364,6 +1365,23 @@ def load_uploaded_files(args: dict, env: EvalEnv) -> DriverVectorCube:
13641365
raise FeatureUnsupportedException(f"Loading format {format!r} is not supported")
13651366

13661367

1368+
@non_standard_process(
1369+
ProcessSpec(
1370+
id="to_vector_cube",
1371+
description="[EXPERIMENTAL:] Converts given data (e.g. GeoJson object) to a vector cube."
1372+
)
1373+
.param('data', description="GeoJson object.", schema={"type": "object", "subtype": "geojson"})
1374+
.returns("vector-cube", schema={"type": "object", "subtype": "vector-cube"})
1375+
)
1376+
def to_vector_cube(args: Dict, env: EvalEnv):
1377+
# TODO: standardization of something like this? https://github.com/Open-EO/openeo-processes/issues/346
1378+
data = extract_arg(args, "data", process_id="to_vector_cube")
1379+
if isinstance(data, dict) and data.get("type") in {"Polygon", "MultiPolygon", "Feature", "FeatureCollection"}:
1380+
return DriverVectorCube.from_geojson(data)
1381+
# TODO: support more inputs: string with geojson, string with WKT, list of WKT, string with URL to GeoJSON, ...
1382+
raise FeatureUnsupportedException(f"Converting {type(data)} to vector cube is not supported")
1383+
1384+
13671385
@non_standard_process(
13681386
ProcessSpec("get_geometries", description="Reads vector data from a file or a URL or get geometries from a FeatureCollection")
13691387
.param('filename', description="filename or http url of a vector file", schema={"type": "string"}, required=False)

openeo_driver/datacube.py

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def aggregate_spatial(
112112
reducer: dict,
113113
target_dimension: str = "result",
114114
) -> Union["AggregatePolygonResult", "AggregatePolygonSpatialResult", "DriverVectorCube"]:
115+
# TODO: drop `target_dimension`? see https://github.com/Open-EO/openeo-processes/issues/366
115116
self._not_implemented()
116117

117118

tests/test_views_execute.py

+78
Original file line numberDiff line numberDiff line change
@@ -2582,3 +2582,81 @@ def test_if_merge_cubes(api100):
25822582
"result": True
25832583
}
25842584
})
2585+
2586+
2587+
@pytest.mark.parametrize(["geojson", "expected"], [
2588+
(
2589+
{"type": "Polygon", "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]},
2590+
[
2591+
{
2592+
"type": "Feature",
2593+
"geometry": {"type": "Polygon", "coordinates": [[[1, 1], [3, 1], [2, 3], [1, 1]]]},
2594+
"properties": {},
2595+
},
2596+
],
2597+
),
2598+
(
2599+
{"type": "MultiPolygon", "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]},
2600+
[
2601+
{
2602+
"type": "Feature",
2603+
"geometry": {"type": "MultiPolygon", "coordinates": [[[[1, 1], [3, 1], [2, 3], [1, 1]]]]},
2604+
"properties": {},
2605+
},
2606+
],
2607+
),
2608+
(
2609+
{
2610+
"type": "Feature",
2611+
"geometry": {"type": "MultiPolygon", "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]},
2612+
"properties": {"id": "12_3"},
2613+
},
2614+
[
2615+
{
2616+
"type": "Feature",
2617+
"geometry": {"type": "MultiPolygon", "coordinates": [[[[1, 1], [3, 1], [2, 3], [1, 1]]]]},
2618+
"properties": {"id": "12_3"},
2619+
},
2620+
],
2621+
),
2622+
(
2623+
{
2624+
"type": "FeatureCollection",
2625+
"features": [
2626+
{
2627+
"type": "Feature",
2628+
"geometry": {"type": "Polygon", "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]},
2629+
"properties": {"id": 1},
2630+
},
2631+
{
2632+
"type": "Feature",
2633+
"geometry": {"type": "MultiPolygon", "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]},
2634+
"properties": {"id": 2},
2635+
},
2636+
]},
2637+
[
2638+
{
2639+
"type": "Feature",
2640+
"geometry": {"type": "Polygon", "coordinates": [[[1, 1], [3, 1], [2, 3], [1, 1]]]},
2641+
"properties": {"id": 1},
2642+
},
2643+
{
2644+
"type": "Feature",
2645+
"geometry": {"type": "MultiPolygon", "coordinates": [[[[1, 1], [3, 1], [2, 3], [1, 1]]]]},
2646+
"properties": {"id": 2},
2647+
},
2648+
],
2649+
),
2650+
])
2651+
def test_to_vector_cube(api100, geojson, expected):
2652+
res = api100.check_result({
2653+
"vc": {
2654+
"process_id": "to_vector_cube",
2655+
"arguments": {"data": geojson},
2656+
"result": True,
2657+
}
2658+
})
2659+
assert res.json == DictSubSet({
2660+
"type": "FeatureCollection",
2661+
"features": expected,
2662+
})

0 commit comments

Comments
 (0)