Skip to content
Permalink
Browse files

Merge pull request #1997 from Gustry/delete-holes

[Processing][Feature] Add new algo to remove holes in a polygon layer
  • Loading branch information
volaya committed May 6, 2015
2 parents e58e8ac + 9d6b280 commit 410e6f36bf1e1443e48f0a1657ad6e348a284b68
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
DeleteHoles.py
---------------------
Date : April 2015
Copyright : (C) 2015 by Etienne Trimaille
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Etienne Trimaille'
__date__ = 'April 2015'
__copyright__ = '(C) 2015, Etienne Trimaille'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.core import QgsFeature, QgsGeometry
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector


class DeleteHoles(GeoAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'

def defineCharacteristics(self):
self.name = 'Delete holes'
self.group = 'Vector geometry tools'

self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_POLYGON]))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Output')))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
layer.pendingFields(),
layer.wkbType(),
layer.crs())

features = vector.features(layer)
count = len(features)
total = 100.0 / float(count)

feat = QgsFeature()
for count, f in enumerate(features):

geometry = f.geometry()
if geometry.isMultipart():
multi_polygon = geometry.asMultiPolygon()
for polygon in multi_polygon:
for ring in polygon[1:]:
polygon.remove(ring)
geometry = QgsGeometry.fromMultiPolygon(multi_polygon)

else:
polygon = geometry.asPolygon()
for ring in polygon[1:]:
polygon.remove(ring)
geometry = QgsGeometry.fromPolygon(polygon)

feat.setGeometry(geometry)
feat.setAttributes(f.attributes())
writer.addFeature(feat)
progress.setPercentage(int(count * total))

del writer
@@ -84,6 +84,7 @@
from Eliminate import Eliminate
from SpatialJoin import SpatialJoin
from DeleteColumn import DeleteColumn
from DeleteHoles import DeleteHoles
from DeleteDuplicateGeometries import DeleteDuplicateGeometries
from TextToFloat import TextToFloat
from ExtractByAttribute import ExtractByAttribute
@@ -151,7 +152,7 @@ def __init__(self):
VariableDistanceBuffer(), Dissolve(), Difference(),
Intersection(), Union(), Clip(), ExtentFromLayer(),
RandomSelection(), RandomSelectionWithinSubsets(),
SelectByLocation(), RandomExtract(),
SelectByLocation(), RandomExtract(), DeleteHoles(),
RandomExtractWithinSubsets(), ExtractByLocation(),
SpatialJoin(), RegularPoints(), SymmetricalDifference(),
VectorSplit(), VectorGrid(), DeleteColumn(),

0 comments on commit 410e6f3

Please sign in to comment.
You can’t perform that action at this time.