raw-data-api
raw-data-api copied to clipboard
[FEATURE] more flexible parsing of geometries for /snapshot
This is low priority
Problem
/snapshotaccepts a JSON, with ageometrykey.- The geometry must be of type Polygon or MultiPolygon.
- Passing a Feature or FeatureCollection fails.
- I have not testing passing a GeometryCollection, but I assume that fails too.
- Upstream services that use raw-data-api must parse GeoJSON and extract only a Polygon or MultiPolygon first.
Solution
- As raw-data-api is the lowest level, it would be nice to have the handling here, to prevent duplicating geometry extraction code.
- It's not too tricky to identify the type of GeoJSON passed in by the user, using various libraries available:
python-geojson,geojson-pydantic, etc.
Multiple Geometries
- This also raises the question for what to do if the user includes multiple geometries in their FeatureCollection or GeometryCollection.
- If all nested geometries are of type Polygon, then they could be merged together.
- But if different types are contained together, this causes issues.
- If handling multiple geometries is required, then this becomes more complex.
Additional context
- We mostly parse everything into a standard FeatureCollection for FMTM, for flexibility of parsing many types of files.
One option for supporting multiple geometries could be to simply convex hull them:
import json
from shapely import to_geojson
from shapely.geometry import shape
from shapely.ops import unary_union
if (geom_type := aoi_geojson.get("type")) == "FeatureCollection":
# Convert each feature into a Shapely geometry
geometries = [
shape(feature.get("geometry")) for feature in aoi_geojson.get("features", [])
]
merged_geom = unary_union(geometries)
elif geom_type == "Feature":
merged_geom = shape(aoi_geojson.get("geometry"))
else:
merged_geom = shape(aoi_geojson)
# Convex hull with enclose all geometries in a single polygon
merged_aoi_polygon = json.loads(to_geojson(merged_geom.convex_hull))
- It could also be possible to generate extracts based on multipolygons / multiple features, instead of enclosing in a convex hull, however I am sure this is more complex to implement.
- If the user provides a feature collection with geoms very far apart (e.g. in different cities), then the merged AOI would be far too large. Extract generation would presumably fail.