How to repair a polygon with self-intersection?
-
28-09-2019 - |
Question
Is there a way to remove self-intersections from a polygon using GEOS?
Solution
So, I have to answer the question myself. Maybe it will help someone.
You can repair a polygon using the geos::operation::buffer::BufferOp class. E.g.
geos::geom::Geometry * result =
geos::operation::buffer::BufferOp::bufferOp(polygon, 1.0);
You can find some more good recipes in the Secrets of the JTS Topology Suite.
OTHER TIPS
Often point intersections are allowed, so you can change
1 2 4
*--* *
| \ /|
| X |
| / \|
*--* *
6 5 3
to
1 2 4
*--* *
| \ /|
| 3*6|
| / \|
*--* *
8 7 5
where points 3 and 6 are the same. If point intersections are not allowed, move one of them a small amount.
In my experience, most such intersections some from a faulty polygon simplification, so it would be better to go back to the source if possible.
Self-intersected polygon is invalid. Thus BufferOp may give invalid result. I didn't find any way to fix self-intersected polygon in geos. st_makevalid function in PostGIS uses geos. So it is possible to investigate the source code.