Skip to content

Commit

Permalink
EP-3981 #114 add to_vector_cube process (custom) for converting Geo…
Browse files Browse the repository at this point in the history
…Json to VectorCube
  • Loading branch information
soxofaan committed Apr 22, 2022
1 parent ec74ca3 commit 927a115
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
18 changes: 18 additions & 0 deletions openeo_driver/ProcessGraphDeserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ def aggregate_polygon(args: dict, env: EvalEnv) -> DriverDataCube:
def aggregate_spatial(args: dict, env: EvalEnv) -> DriverDataCube:
reduce_pg = extract_deep(args, "reducer", "process_graph")
cube = extract_arg(args, 'data')
# TODO: drop `target_dimension`? see https://github.com/Open-EO/openeo-processes/issues/366
target_dimension = args.get('target_dimension', None)

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


@non_standard_process(
ProcessSpec(
id="to_vector_cube",
description="[EXPERIMENTAL:] Converts given data (e.g. GeoJson object) to a vector cube."
)
.param('data', description="GeoJson object.", schema={"type": "object", "subtype": "geojson"})
.returns("vector-cube", schema={"type": "object", "subtype": "vector-cube"})
)
def to_vector_cube(args: Dict, env: EvalEnv):
# TODO: standardization of something like this? https://github.com/Open-EO/openeo-processes/issues/346
data = extract_arg(args, "data", process_id="to_vector_cube")
if isinstance(data, dict) and data.get("type") in {"Polygon", "MultiPolygon", "Feature", "FeatureCollection"}:
return DriverVectorCube.from_geojson(data)
# TODO: support more inputs: string with geojson, string with WKT, list of WKT, string with URL to GeoJSON, ...
raise FeatureUnsupportedException(f"Converting {type(data)} to vector cube is not supported")


@non_standard_process(
ProcessSpec("get_geometries", description="Reads vector data from a file or a URL or get geometries from a FeatureCollection")
.param('filename', description="filename or http url of a vector file", schema={"type": "string"}, required=False)
Expand Down
1 change: 1 addition & 0 deletions openeo_driver/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def aggregate_spatial(
reducer: dict,
target_dimension: str = "result",
) -> Union["AggregatePolygonResult", "AggregatePolygonSpatialResult", "DriverVectorCube"]:
# TODO: drop `target_dimension`? see https://github.com/Open-EO/openeo-processes/issues/366
self._not_implemented()


Expand Down
78 changes: 78 additions & 0 deletions tests/test_views_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2582,3 +2582,81 @@ def test_if_merge_cubes(api100):
"result": True
}
})


@pytest.mark.parametrize(["geojson", "expected"], [
(
{"type": "Polygon", "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]},
[
{
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [[[1, 1], [3, 1], [2, 3], [1, 1]]]},
"properties": {},
},
],
),
(
{"type": "MultiPolygon", "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]},
[
{
"type": "Feature",
"geometry": {"type": "MultiPolygon", "coordinates": [[[[1, 1], [3, 1], [2, 3], [1, 1]]]]},
"properties": {},
},
],
),
(
{
"type": "Feature",
"geometry": {"type": "MultiPolygon", "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]},
"properties": {"id": "12_3"},
},
[
{
"type": "Feature",
"geometry": {"type": "MultiPolygon", "coordinates": [[[[1, 1], [3, 1], [2, 3], [1, 1]]]]},
"properties": {"id": "12_3"},
},
],
),
(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]},
"properties": {"id": 1},
},
{
"type": "Feature",
"geometry": {"type": "MultiPolygon", "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]},
"properties": {"id": 2},
},
]},
[
{
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [[[1, 1], [3, 1], [2, 3], [1, 1]]]},
"properties": {"id": 1},
},
{
"type": "Feature",
"geometry": {"type": "MultiPolygon", "coordinates": [[[[1, 1], [3, 1], [2, 3], [1, 1]]]]},
"properties": {"id": 2},
},
],
),
])
def test_to_vector_cube(api100, geojson, expected):
res = api100.check_result({
"vc": {
"process_id": "to_vector_cube",
"arguments": {"data": geojson},
"result": True,
}
})
assert res.json == DictSubSet({
"type": "FeatureCollection",
"features": expected,
})

0 comments on commit 927a115

Please sign in to comment.