3

I have a self-intersecting polygon inside a shapefile. Does there exist a Python library which spots the self-intersections and makes it possible to correct them?

I have found it is possible in QGIS but how can I achieve the same result in Python? Do I need a PostGIS DB or I can handle it with a Python library?

2

The Shapely library is commonly used for geometric operations in Python, alternatively you could use GDAL's ogr.

https://pypi.org/project/Shapely/

Fixing invalid polygons is hard because there really is no way of knowing how the polygon is supposed to be like. One common approach is using .buffer(0) to try to make it valid somehow.

The Shapely documentation includes an example for all this: https://shapely.readthedocs.io/en/stable/manual.html#object.buffer

>>> coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]
>>> bowtie = Polygon(coords)
>>> bowtie.is_valid
False
>>> clean = bowtie.buffer(0)
>>> clean.is_valid
True

A manual approach will almost always be more safe and better.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.