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?


1 Answer 1


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


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
>>> clean = bowtie.buffer(0)
>>> clean.is_valid

Now it is "valid" but it might be something very different from what you would expect. Make sure you inspect your geometries afterwards.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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