2

My script uses a buffer and dissolves to create polygons around my points. However, there are holes in between the points. I do not want to increase the distance because then it will also cover the unnecessary areas. Is there an option that covers the holes in my polygon? my script is below:

enter image description here

from qgis import processing
from datetime import datetime, timedelta


inlayDir = iface.activeLayer()

buffer_dist = 0.8

polygon =  processing.runAndLoadResults("native:buffer",
              {'INPUT':inlayDir,
               'DISTANCE':buffer_dist,
               'SEGMENTS':2,
               'END_CAP_STYLE':0,
               'JOIN_STYLE':0,
               'MITER_LIMIT':2,
               'DISSOLVE':True,
               'OUTPUT':'TEMPORARY_OUTPUT'})
1

2 Answers 2

5

Delete holes:

Takes a polygon layer and removes holes in polygons

Use buffer output as input:

layer = iface.activeLayer()

buffered = processing.run("native:buffer", 
    {'INPUT':layer,'DISTANCE':1300,'SEGMENTS':5,'END_CAP_STYLE':0,
    'JOIN_STYLE':0,'MITER_LIMIT':2,
    'DISSOLVE':True,'OUTPUT':'TEMPORARY_OUTPUT'})["OUTPUT"]
    
processing.runAndLoadResults("native:deleteholes", 
    {'INPUT':buffered,'MIN_AREA':0,'OUTPUT':'TEMPORARY_OUTPUT'})

enter image description here

2
1

Since QGIS 3.0 there is another option available by utilization of the removeInteriorRings() method of the QgsGeometry class.

Removes the interior rings from a (multi)polygon geometry.

If the minimumAllowedArea parameter is specified then only rings smaller than this minimum area will be removed.

from qgis import processing
from qgis.utils import iface
from qgis.core import edit, QgsGeometry, QgsProject

inlayDir = iface.activeLayer()

buffer_dist = 0.8

polygon = processing.run("native:buffer", {
            'INPUT': inlayDir,
            'DISTANCE': buffer_dist,
            'SEGMENTS': 2,
            'END_CAP_STYLE': 0,
            'JOIN_STYLE': 0,
            'MITER_LIMIT': 2,
            'DISSOLVE': True,
            'OUTPUT': 'TEMPORARY_OUTPUT'
            })['OUTPUT']

with edit(polygon):
    for feat in polygon.getFeatures():
        wkt_geom = feat.geometry().removeInteriorRings(minimumAllowedArea=-1).asWkt()
        geom = QgsGeometry.fromWkt(wkt_geom)
        polygon.changeGeometry(feat.id(), geom)

QgsProject.instance().addMapLayer(polygon)

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.