Skip to content

Commit

Permalink
fix: add check for correct type of geojson input
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 7, 2025
1 parent 710db22 commit 6d4656e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
27 changes: 19 additions & 8 deletions geojson_aoi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import json
import logging
from pathlib import Path
from typing import Any, Literal

AllowedInputTypes = Literal[
"Polygon", "MultiPolygon", "Feature", "FeatureCollection", "GeometryCollection"
from typing import Any

AllowedInputTypes = [
"Polygon",
"MultiPolygon",
"Feature",
"FeatureCollection",
"GeometryCollection",
]
Coordinate = float | int
PointGeom = tuple[Coordinate, Coordinate]
Expand Down Expand Up @@ -113,8 +117,8 @@ def split_multigeom(


def _ensure_right_hand_rule(
coordinates: list[list[PointGeom]],
) -> list[list[PointGeom]]:
coordinates: PolygonGeom,
) -> PolygonGeom:
"""Ensure the outer ring follows the right-hand rule (clockwise)."""

def is_clockwise(ring: list[PointGeom]) -> bool:
Expand All @@ -136,7 +140,7 @@ def is_clockwise(ring: list[PointGeom]) -> bool:
return coordinates


def _create_convex_hull(polygons: list[list[PointGeom]]) -> list[PointGeom]:
def _create_convex_hull(polygons: PolygonGeom) -> list[PointGeom]:
"""Create a convex hull from a list of polygons."""
from itertools import chain

Expand Down Expand Up @@ -314,10 +318,17 @@ def parse_aoi(
else:
raise ValueError("GeoJSON input must be a valid dict, str, or bytes")

print(geojson_parsed)
print("type" in geojson_parsed)

# Throw error if no data
if geojson_parsed is None or geojson_parsed == {}:
if geojson_parsed is None or geojson_parsed == {} or "type" not in geojson_parsed:
raise ValueError("Provided GeoJSON is empty")

# Throw error if wrong geometry type
if geojson_parsed["type"] not in AllowedInputTypes:
raise ValueError(f"The GeoJSON type must be one of: {AllowedInputTypes}")

# Convert to FeatureCollection
featcol = geojson_to_featcol(geojson_parsed)
if not featcol.get("features", []):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def test_invalid_input():
with pytest.raises(ValueError, match="Provided GeoJSON is empty"):
parse_aoi("{}")

with pytest.raises(ValueError, match="The GeoJSON type must be one of:"):
parse_aoi({"type": "Point"})


def test_file_input(tmp_path):
"""GeoJSON file input for parse_aoi function."""
Expand Down

0 comments on commit 6d4656e

Please sign in to comment.