Skip to content
Permalink
Browse files

[FEATURE] (Processing) Eliminate with smallest polygon

  • Loading branch information
bstroebl committed Mar 13, 2014
1 parent 03ca0c8 commit bd5070136139bd68d19579cf607ef022e1d34f24
Showing with 35 additions and 16 deletions.
  1. +35 −16 python/plugins/processing/algs/ftools/Eliminate.py
@@ -50,18 +50,19 @@ class Eliminate(GeoAlgorithm):
COMPARISONVALUE = 'COMPARISONVALUE'
COMPARISON = 'COMPARISON'

MODES = ['Area', 'Common boundary']
MODE_AREA = 0
MODE_BOUNDARY = 1
MODES = ['Largest area', 'Smallest Area', 'Largest common boundary']
MODE_LARGEST_AREA = 0
MODE_SMALLEST_AREA = 1
MODE_BOUNDARY = 2

def defineCharacteristics(self):
self.name = 'Eliminate sliver polygons'
self.group = 'Vector geometry tools'
self.addParameter(ParameterVector(self.INPUT, 'Input layer',
[ParameterVector.VECTOR_TYPE_POLYGON]))
self.addParameter(ParameterBoolean(self.KEEPSELECTION,
'Use current selection in input layer (works only \
if called from toolbox)', False))
'Use current selection in input layer (works only ' + \
'if called from toolbox)', False))
self.addParameter(ParameterTableField(self.ATTRIBUTE,
'Selection attribute', self.INPUT))
self.comparisons = [
@@ -79,14 +80,15 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.COMPARISONVALUE, 'Value',
default='0'))
self.addParameter(ParameterSelection(self.MODE,
'Merge selection with the neighbouring polygon \
with the largest', self.MODES))
'Merge selection with the neighbouring polygon with the ',
self.MODES))
self.addOutput(OutputVector(self.OUTPUT, 'Cleaned layer'))

def processAlgorithm(self, progress):
inLayer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
boundary = self.getParameterValue(self.MODE) == self.MODE_BOUNDARY
smallestArea = self.getParameterValue(self.MODE) == self.MODE_SMALLEST_AREA
keepSelection = self.getParameterValue(self.KEEPSELECTION)

if not keepSelection:
@@ -246,6 +248,7 @@ def processAlgorithm(self, progress):
mergeWithFid = None
mergeWithGeom = None
max = 0
min = -1
selFeat = QgsFeature()

while fit.nextFeature(selFeat):
@@ -258,17 +261,33 @@ def processAlgorithm(self, progress):
if boundary:
selValue = iGeom.length()
else:
# Largest area. We need a common boundary in
# area. We need a common boundary in
# order to merge
if 0 < iGeom.length():
selValue = selGeom.area()
else:
selValue = 0

if selValue > max:
max = selValue
mergeWithFid = selFeat.id()
mergeWithGeom = QgsGeometry(selGeom)
selValue = -1

if -1 != selValue:
useThis = True

if smallestArea:
if -1 == min:
min = selValue
else:
if selValue < min:
min = selValue
else:
useThis = False
else:
if selValue > max:
max = selValue
else:
useThis = False

if useThis:
mergeWithFid = selFeat.id()
mergeWithGeom = QgsGeometry(selGeom)
# End while fit

if mergeWithFid is not None:
@@ -279,8 +298,8 @@ def processAlgorithm(self, progress):
madeProgress = True
else:
raise GeoAlgorithmExecutionException(
'Could not replace geometry of feature \
with id %s' % mergeWithFid)
'Could not replace geometry of feature ' + \
'with id %s' % mergeWithFid)

start = start + add
progress.setPercentage(start)

0 comments on commit bd50701

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