4

I am a QGIS python user. I want to iterate over the rings of a polygon to remove holes in the polygon, so:

  • I need to iterate over the rings in a QgsGeometry polygon
  • I need to detect the main ring of the polygon
  • I need to remove every ring, which is not the main ring

How can I do that in python QGIS?

Matthias Kuhn
  • 27,780
  • 3
  • 88
  • 129
Marcel GJS
  • 473
  • 3
  • 13
  • 1
    You could perhaps look into QgsGeometry.deleteRing which, from the description, can be called with QgsGeometry.deleteRing(int, int partNum=0) -> bool. I'm not an expert so hopefully others can advise. – Joseph Aug 27 '15 at 11:43
  • What do you want to do if the geometry is a MultiPolygon, ie more than one outer ring - as in the case of a feature that is two islands? – Spacedman Aug 30 '15 at 07:12

1 Answers1

6
if geom.isMultipart() is False: # if only simple polygon, calculate only for this
        polyg = geom.asPolygon() # transform to list of points
        for ring in polyg:
            # doSomething...FILL HOLES
else: # is multipart
        multi = geom.asMultiPolygon()
        for polyg in multi:
            for ring in polyg:
                # doSomething ... FILL HOLES

I use this for testing and looping trought multipolygon and/or simple polygon.

Marcel GJS
  • 473
  • 3
  • 13
  • I have solution, for find the first ring (main ring in geometry), in QGIS Python is it always first rinng in geom.asPolygon() list. After, I will add there full code for fill holes. – Marcel GJS Aug 31 '15 at 06:21
  • My example for fill holes is in function fillHoles of maptools module. In this module are more useful functions. First ring in polygon geometry is always main ring. – Marcel GJS Sep 03 '15 at 11:17