Is there a way to remove self-intersections from a polygon using GEOS?
3 Answers
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.
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.
- 7,511
 - 2
 - 32
 - 45
 
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.
- 21
 - 2
 
- 
                    2It's worth to clarify that "self-intersecting polygon is invalid" but according to OGC SFS. It is a valid complex polygon in the world of geometry. – mloskot Nov 07 '11 at 12:02