georust / geo

Geospatial primitives and algorithms for Rust
https://crates.io/crates/geo
Other
1.48k stars 191 forks source link

feat: implement `Validify` repair trait #1120

Closed RobWalt closed 1 month ago

RobWalt commented 7 months ago

Description

While discouraging the handling of invalid geometries and prioritizing the maintenance of geometric validity as a fundamental principle, it is recognized that geometries may occasionally deviate from perfection. In such instances, we have to deal with checking the geometry and repairing it all on our own. That's why it's time for a trait in geo which does the job for us.

This pull request introduces a new feature by implementing the Validify for the first two candidates: Self intersecting Polygons and MultiPolygons. The Validity trait serves to repair these invalid geometries by splitting them up into smaller polygons which fulfill the implicit invariants.

Changes Made

Usage Example

use geo::Validify;

// Assuming `your_geometry` is an instance of a self intersecting `Polygon`
let valid_multi_polygon = your_geometry.split_into_valid();

Testing

Documentation

urschrei commented 7 months ago

Very nice. Also worth having a look at https://github.com/tudelft3d/prepair (cc @hugoledoux) which uses a constrained triangulation approach for further inspiration if you're interested…

RobWalt commented 7 months ago

Lol that sounds a lot like what I did with SpadeBoolops. Now I wonder if I could just do a single polygon union to achieve the same 😅

Thanks a lot for the link! That's super useful!!

hugoledoux commented 7 months ago

Watch out: the machinery used to achieve what we did is CGAL, which allows us to use a robust constrained Delaunay triangulation where we can intersect constraints and attach attributes to edges and triangles, and etc etc.

A standard triangulation doesn't allow you to do this, although if https://github.com/Stoeoef/spade is used it would be a good basis (I think, I haven't thoroughly tested it).

Since you have GEOS in rust, perhaps it's simpler to translate the algo of ST_MakeValid (no docs AFAIK, the code is the ultimate docs here...).

RobWalt commented 1 month ago

I think I overengineered this a bit. I'm not seeing myself finishing it anytime soon, so I'll just gonna close this now. Feel free to pick it up or talk to me about the theoretical side of things