Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] more flexible parsing of geometries for /snapshot #175

Open
spwoodcock opened this issue Dec 7, 2023 · 1 comment
Open

[FEATURE] more flexible parsing of geometries for /snapshot #175

spwoodcock opened this issue Dec 7, 2023 · 1 comment

Comments

@spwoodcock
Copy link
Member

spwoodcock commented Dec 7, 2023

This is low priority

Problem

  • /snapshot accepts a JSON, with a geometry key.
  • 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.
@spwoodcock
Copy link
Member Author

spwoodcock commented Feb 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant