Skip to content

Commit

Permalink
[FEATURE] (Processing) Eliminate with smallest polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Ströbl committed Mar 13, 2014
1 parent 03ca0c8 commit bd50701
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions python/plugins/processing/algs/ftools/Eliminate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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:
Expand Down Expand Up @@ -246,6 +248,7 @@ def processAlgorithm(self, progress):
mergeWithFid = None
mergeWithGeom = None
max = 0
min = -1
selFeat = QgsFeature()

while fit.nextFeature(selFeat):
Expand All @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit bd50701

Please sign in to comment.