georust / geo

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

`is_valid` for all geometry types. #1130

Open andriyDev opened 6 months ago

andriyDev commented 6 months ago

As stated in the description of the geometry types (e.g., Polygon, LineString), validity is not enforced, but it is expected. While the validity requirements are clearly documented, there is no "canonical" way to check if your geometry adheres to the validity requirements.

An example of when this would be desirable is loading polygons from a file. There is no guarantee that the file contained valid geometry, but once we check this, the algorithms should produce valid polygons.

Therefore, if checking validity is expensive, that is ok, since this should be a one-time cost.

Ideally this is supported by each geometry type separately, and also on Geometry.

andriyDev commented 6 months ago

For LineString, its requirements are either empty, or 2+ coordinates. This is trivial to check. In addition, if the LineString is closed, it must also not self-intersect. We can check this by using Intersections (inserting all the edges in the line string) and checking that all intersections are vertices of the original line string (it's not clear to me if a line string is allowed to use the same vertex twice, but if it's not then I think we can just check if there are any intersections).

mthh commented 6 months ago

Some time ago I tried to implement various validity checks for geo-types geometries : https://github.com/mthh/geo-validity-check

I think it implements most of the validity checks of GEOS as well as some other checks.

The non-validity diagnostic messages could greatly be improved, and there is no functionality to attempt to repair the geometries. Anyway, if that would be useful, feel free to reuse code from this crate to implement an is_valid function directly in geo.

dabreegster commented 6 months ago

Somewhat related to #1120