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 Original file line Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ class Eliminate(GeoAlgorithm):
COMPARISONVALUE = 'COMPARISONVALUE' COMPARISONVALUE = 'COMPARISONVALUE'
COMPARISON = 'COMPARISON' COMPARISON = 'COMPARISON'


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


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


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


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


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


if selValue > max: if -1 != selValue:
max = selValue useThis = True
mergeWithFid = selFeat.id()
mergeWithGeom = QgsGeometry(selGeom) 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 # End while fit


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


start = start + add start = start + add
progress.setPercentage(start) progress.setPercentage(start)
Expand Down

0 comments on commit bd50701

Please sign in to comment.