0

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?

closed as off-topic by ahmadhanb, BERA, Erik, Jochen Schwarze, Vince Feb 6 at 11:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – ahmadhanb, BERA, Erik, Jochen Schwarze, Vince
If this question can be reworded to fit the rules in the help center, please edit the question.

0

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.

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