Skip to content

Commit

Permalink
disallow Point Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bossie committed Jan 24, 2025
1 parent 3e8f3eb commit eff3674
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
26 changes: 25 additions & 1 deletion openeo_driver/ProcessGraphDeserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,13 +1440,21 @@ def filter_labels(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:

def _extract_bbox_extent(args: dict, field="extent", process_id="filter_bbox", handle_geojson=False) -> dict:
extent = extract_arg(args, name=field, process_id=process_id)
# TODO: handle vector cube
if handle_geojson and extent.get("type") in [
"Polygon",
"MultiPolygon",
"GeometryCollection",
"GeometryCollection", # TODO: disallow GeometryCollection?
"Feature",
"FeatureCollection",
]:
if not _contains_polygon(extent):
raise ProcessParameterInvalidException(
parameter=field,
process=process_id,
reason="unsupported GeoJSON; requires at least one Polygon or MultiPolygon",
)

w, s, e, n = DriverVectorCube.from_geojson(extent).get_bounding_box()
# TODO: support (non-standard) CRS field in GeoJSON?
d = {"west": w, "south": s, "east": e, "north": n, "crs": "EPSG:4326"}
Expand All @@ -1462,6 +1470,22 @@ def _extract_bbox_extent(args: dict, field="extent", process_id="filter_bbox", h
return d


def _contains_polygon(geojson: dict) -> bool:
if geojson["type"] in ["Polygon", "MultiPolygon"]:
return True

if geojson["type"] == "Feature":
return _contains_polygon(geojson["geometry"])

if geojson["type"] == "FeatureCollection":
return any(_contains_polygon(feature) for feature in geojson["features"])

if geojson["type"] == "GeometryCollection":
return any(_contains_polygon(geometry) for geometry in geojson["geometries"])

return False


@process
def filter_bbox(args: ProcessArgs, env: EvalEnv) -> DriverDataCube:
cube: DriverDataCube = args.get_required("data", expected_type=DriverDataCube)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_dry_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,37 @@ def test_load_stac_properties(dry_run_env, dry_run_tracer):
]


def test_load_stac_spatial_extent_requires_a_polygon(dry_run_tracer, backend_implementation):
pg = {
"loadstac1": {
"process_id": "load_stac",
"arguments": {
"url": "https://stac.test",
"spatial_extent": {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.0, 50.0]
}
}
},
"result": True,
}
}

dry_run_env = EvalEnv(
{ENV_DRY_RUN_TRACER: dry_run_tracer, "backend_implementation": backend_implementation, "version": "2.0.0"}
)

with pytest.raises(OpenEOApiException) as e:
evaluate(pg, dry_run_env)

assert e.value.message == (
"The value passed for parameter 'spatial_extent' in process 'load_stac' is invalid: "
"unsupported GeoJSON; requires at least one Polygon or MultiPolygon"
)


@pytest.mark.parametrize(
["arguments", "expected"],
[
Expand Down
7 changes: 0 additions & 7 deletions tests/test_views_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,6 @@ def test_load_collection_filter(api):
),
{"west": 11, "south": 22, "east": 44, "north": 55, "crs": "EPSG:4326"},
),
(
as_geojson_feature_collection(
shapely.geometry.Point(2, 3),
shapely.geometry.Point(4, 5),
),
{"west": 2, "south": 3, "east": 4, "north": 5, "crs": "EPSG:4326"},
),
],
)
def test_load_collection_spatial_extent_geojson(api, spatial_extent, expected):
Expand Down

0 comments on commit eff3674

Please sign in to comment.