When I polygonize a raster, I very often have 'pixels' holes/interior ring that touches exterior ring.

An example:

POLYGON((5 0, 10 0, 10 10, 0 10, 0 0, 5 0, 3 3, 5 6, 7 3, 5 0))


It's defined as an invalid geometry in the OGC geometry specifications.

In QGIS, how to fix this?

1 Answer 1


I've found the solution here but for PostGIS: https://gis.stackexchange.com/a/15301/93097 and I've tested it in Spatialite/Virtual layers, and it works.

Bellow the code I used with a shapefile 'my_layer' with one field "DN" and wrote in the DB Manager:

SELECT "DN", ST_Buffer(geometry, 0.0) as geometry
FROM my_layer;

In the SQL query SELECT [...] FROM [...]; :

  • "DN" as mentionned above is the field of my layer. It's a common field that contains raster informations after vectorization, stands for Digital Number. I put it in the query in order to get my attributes with the new virtual layer. If you have more/different fields, just write "my_field1", "my_field2", "my_field3" as in a SQL SELECT query instead of "DN".
  • geometry is the default field name for geometry in a QGIS shapefile or memory layer.
  • ST_Buffer(geometry, 0.0) : creates a buffer of 0, so a buffer with no distance, and it rewrites the polygon definition (vertices order) and fix self-intersections.
  • Hi, where do you use this code ?
    – Robagb
    Commented Feb 7, 2022 at 11:55
  • 1
    WHat is DN? can you explain the context?
    – Robagb
    Commented Feb 7, 2022 at 12:02
  • All the answers of your comments are already in my answer. Commented Feb 7, 2022 at 15:18
  • 1
    Hi, what is postGis, a pluggin? what is DN, do you use the field calculator? sorry it is very blur to me I am new to QGIS...
    – Robagb
    Commented Feb 7, 2022 at 16:10
  • So, I advise you to search on internet a little about SQL and databases (with spatial extensions). Commented Feb 7, 2022 at 17:11

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.