geo icon indicating copy to clipboard operation
geo copied to clipboard

Using Simplification algorithm on a polygon can result in an invalid polygon

Open frewsxcv opened this issue 8 years ago • 3 comments

https://github.com/georust/rust-geo/pull/135#issuecomment-320788702

Maybe the algorithm should sometimes return an Option<T> since it can sometimes fail.

frewsxcv avatar Aug 14 '17 03:08 frewsxcv

Polygon::new can result in an invalid polygon. 😉 This is back to the validity thing again.

Personally I think that the simplification algorithm should return the geometry type, rather than an Option. Allow the caller to call the future .is_valid() function on it.

  • PostGIS's ST_Simplify can return invalid polygons.
  • In PostGIS there are some ways to make some invalid geometries valid again (e.g. ST_MakeValid or the ST_Buffer(geom, 0) trick). rust-geo might have that later, in which case an invalid polygon from simplification could be "fixed" later. If we returned an Option, then you would never have the chance to return an invalid polygon.
  • Some software can work with invalid polygons. I think mapnik is able to draw some invalid polygons.

amandasaurus avatar Aug 14 '17 07:08 amandasaurus

I've been looking at R-tree implementations in Rust again since this came up, and I'm making good progress with Spade's R* implementation, now that I've managed to implement the PointN trait for Point. I've been working on implementing the technique from here (See WIP here).

If / when that lands, I'm still in favour of returning an Option or Result from anything that can produce invalid geometries. Returning one of these doesn't preclude you from trying to fix the geometry yourself, it just gives you the choice between using an unwrap and taking your chances, or implementing your own fix. We should probably still benchmark a validity check, but unless it's hugely expensive, I think we should be erring on the side of flexible safety.

urschrei avatar Aug 14 '17 11:08 urschrei

Currently, I'm in favor of returning Option/Result types for methods/constructors that can return invalid geometries. At least for validation, right now, I'm envisioning something along the lines of:

impl<T: Float> LineString<T> {
  fn from_vec(v: Vec<T>) -> Result<LineString> { .. }

  /// << insert big disclaimer here >>
  fn from_vec_unchecked(v: Vec<T>) -> LineString { .. }
}

frewsxcv avatar Aug 15 '17 02:08 frewsxcv

Here's a simple case (epsilon = 1.0) simplifying a Polygon exterior ring that results in a ring with only two points:

Before:

LineString([Coord { x: 42.59023437500002, y: 15.303417968749995 }, Coord { x: 42.558691406250006, y: 15.281201171874995 }, Coord { x: 42.54902343750001, y: 15.320068359375 }, Coord { x: 42.56972656250002, y: 15.407324218749991 }, Coord { x: 42.60234375000002, y: 15.432519531249994 }, Coord { x: 42.62451171875, y: 15.36796875 }, Coord { x: 42.610449218750006, y: 15.332275390625 }, Coord { x: 42.59023437500002, y: 15.303417968749995 }])

After:

LineString([Coord { x: 42.59023437500002, y: 15.303417968749995 }, Coord { x: 42.59023437500002, y: 15.303417968749995 }])

frewsxcv avatar Nov 29 '22 05:11 frewsxcv

In the case of polygon rings, could we just break once the LineString ring is 3 coordinates?

frewsxcv avatar Nov 29 '22 16:11 frewsxcv

I think so (that's what we do in visvalingam_preserve – actually it's 4 coordinates there, but no matter) but is there a perf hit for checking the length on every loop?

urschrei avatar Nov 29 '22 16:11 urschrei

Here's my fix attempt: https://github.com/georust/geo/pull/943

frewsxcv avatar Nov 30 '22 05:11 frewsxcv