[GIS] How to avoid self-intersections in geometry created by GDAL Polygonize in Python

gdalpolygonizepythonshapefile

I am creating geometry from a raster using GDAL Polygonize in Python. My issue is that doing this creates a lot of self intersection errors. This seems to occur due to the nature of the pixel grid.

I have noticed that this occurs mostly at edges, but can sometimes occur inside the geometry as well, as shown below.

This is part of an automated pipeline so I don't want to manually fix self-intersections in QGIS or the like; the solution needs to be able to be implemented in Python code.

Is there a way to either stop the self intersections being created in the first place, or a way to clean the generated geometry in Python?

I have tried to use shapely to clean the self intersections but the result is very poor.

Self intersection inside geometry
Self intersection at edge

Best Answer

Yes, this is because pixels always share corners with 8 others. There's no silver bullet for turning the invalid polygons into valid polygons or multipolygons. That said, in your examples above it looks like the self intersections would be solved by eroding (negative buffer, in other words) the invalid polygons by a small fraction, turning them into multipolygons.