7

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))

image

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

In QGIS, how to fix this?

Taras
  • 32,823
  • 4
  • 66
  • 137
J. Monticolo
  • 15,695
  • 1
  • 29
  • 64

1 Answers1

8

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.
J. Monticolo
  • 15,695
  • 1
  • 29
  • 64
  • Hi, where do you use this code ? – Robagb Feb 07 '22 at 11:55
  • 1
    WHat is DN? can you explain the context? – Robagb Feb 07 '22 at 12:02
  • All the answers of your comments are already in my answer. – J. Monticolo Feb 07 '22 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 Feb 07 '22 at 16:10
  • So, I advise you to search on internet a little about SQL and databases (with spatial extensions). – J. Monticolo Feb 07 '22 at 17:11
  • "Use the forum search feature!" is not an answer. What is the meaning of "DN" and why have you included it? If it's just a field relevant to your database and not a part of the answer, why included it? – MrYellow Dec 15 '23 at 02:28
  • @MrYellow: about my previous answer to Robagb, for me all is in the answer and my intention regarding my advice was kind. About the meaning of "DN", I specified it just above the code and it's a field of my layer. It's not useful to fix the polygon geometry but useful to retrieve my layer attributes in the new geometry fixed virtual layer. – J. Monticolo Dec 15 '23 at 09:12
  • Probably helpful to remove from your example the unrelated off-topic field which is obviously causing confusion. – MrYellow Dec 16 '23 at 22:28
  • @MrYellow: I understand your opinion, but in my question : "When I polygonize a raster", so in the same case of me, a user will have a high probability to have the same field. I explained after your comment why I let the field and avoid questions like : "how I get my attributes back". – J. Monticolo Dec 17 '23 at 13:29
  • Fair, I'm usually wrong when confident. ;-) – MrYellow Dec 18 '23 at 14:43