Skip to content

feat: merge multi-feature geometry input into a single polygon - #312

Open
dulcetberg wants to merge 1 commit into
hotosm:developfrom
dulcetberg:feat/merge-multi-feature-geometry
Open

dulcetberg wants to merge 1 commit into
hotosm:developfrom
dulcetberg:feat/merge-multi-feature-geometry

Conversation

@dulcetberg

Copy link
Copy Markdown

Summary

Partially addresses #175.

GeometryValidatorMixin.validate_geometry previously rejected any FeatureCollection with more than one feature outright ("Feature collection with multiple features is not supported yet"), forcing every upstream caller to pre-extract/merge geometries themselves before calling raw-data-api.

Now, when a FeatureCollection has multiple Polygon/MultiPolygon features (still validated the same as before — non-polygon feature types are still rejected), they're merged via Shapely's unary_union and reduced to their convex hull, returned as a single Polygon. This is the approach @spwoodcock sketched in the issue thread — a deliberate simplification rather than generating extracts per-feature. The merged hull can enclose more area than the union of the inputs if features are far apart from each other; that's documented in a code comment at the call site.

Single-feature FeatureCollection, bare Feature, and bare Polygon/MultiPolygon inputs are all unchanged.

What this doesn't do

Doesn't implement GeometryCollection support or the full geojson-aoi-parser integration also discussed in the issue. geojson-aoi-parser needs a live PostGIS connection internally, which isn't available at this synchronous Pydantic-validation layer without a bigger architectural change (passing DB access into validation, or moving validation later in the request flow). Scoping this PR to the Shapely-only approach that fits the existing validator as-is, and leaving the DB-aware integration as a separate follow-up if it's wanted.

Adds shapely as a new dependency (wasn't previously used directly in this repo).

Test plan

  • Verified end-to-end against the real RawDataCurrentParamsBase model: multi-feature merge produces the expected convex-hull polygon.
  • Verified single-feature FeatureCollection, bare Feature, and bare Polygon inputs are all unchanged.
  • Verified a FeatureCollection containing a non-Polygon/MultiPolygon feature (e.g. LineString) is still rejected.
  • pytest tests/test_app.py passes (5 passed).

🤖 Generated with Claude Code

Partially addresses hotosm#175.

GeometryValidatorMixin.validate_geometry previously rejected any
FeatureCollection with more than one feature outright ("Feature
collection with multiple features is not supported yet"), forcing
every upstream caller to pre-extract/merge geometries themselves
before calling raw-data-api.

Now, when a FeatureCollection has multiple Polygon/MultiPolygon
features (still validated the same as before - non-polygon feature
types are still rejected), they're merged via Shapely's unary_union
and reduced to their convex hull, returned as a single Polygon. This
is a deliberate simplification (per the approach @spwoodcock sketched
in the issue thread) rather than generating extracts per-feature -
the merged hull can enclose more area than the union of the inputs
if features are far apart, which is documented in a code comment.

Single-feature FeatureCollection, bare Feature, and bare
Polygon/MultiPolygon inputs are all unchanged.

Doesn't implement GeometryCollection support or the full
geojson-aoi-parser integration also discussed in the issue -
geojson-aoi-parser needs a live PostGIS connection internally, which
isn't available at this synchronous Pydantic-validation layer without
a bigger architectural change (passing DB access into validation, or
moving validation later in the request flow). Scoping this PR to the
Shapely-only approach that fits the existing validator, and leaving
the DB-aware integration as a separate follow-up.

Adds shapely as a new dependency (not previously used directly in
this repo).

Verified end-to-end against the real RawDataCurrentParamsBase model:
multi-feature merge produces the expected convex-hull polygon;
single-feature FeatureCollection, bare Feature, and bare Polygon
inputs are all unchanged; a FeatureCollection containing a
non-Polygon/MultiPolygon feature is still rejected. `pytest
tests/test_app.py` passes (5 passed).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Brian Bergstrom <dulcetberg@gmail.com>
@spwoodcock

Copy link
Copy Markdown
Member

See hotosm/drone-tm#872 (comment)

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

Successfully merging this pull request may close these issues.

2 participants