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?

  • 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
    Commented Aug 27, 2015 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
    Commented Aug 30, 2015 at 7:12

1 Answer 1

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.

  • 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
    Commented Aug 31, 2015 at 6: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
    Commented Sep 3, 2015 at 11:17

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.