2013-09-20 12:46:18 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
***************************************************************************
|
|
|
|
|
Eliminate.py
|
|
|
|
|
---------------------
|
|
|
|
|
Date : August 2012
|
|
|
|
|
Copyright : (C) 2013 by Bernhard Str<EFBFBD>bl
|
|
|
|
|
Email : bernhard.stroebl@jena.de
|
|
|
|
|
***************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* 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. *
|
|
|
|
|
* *
|
|
|
|
|
***************************************************************************
|
|
|
|
|
"""
|
|
|
|
|
|
2013-09-26 12:14:08 +02:00
|
|
|
|
__author__ = 'Bernhard Ströbl'
|
|
|
|
|
__date__ = 'September 2013'
|
|
|
|
|
__copyright__ = '(C) 2013, Bernhard Ströbl'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
2013-09-20 12:46:18 +02:00
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
2013-09-20 12:46:18 +02:00
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
|
from qgis.core import *
|
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
|
from processing.core.GeoAlgorithmExecutionException import \
|
|
|
|
|
GeoAlgorithmExecutionException
|
|
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
2014-07-14 14:19:09 +02:00
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
|
from processing.core.parameters import ParameterBoolean
|
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
|
from processing.core.parameters import ParameterString
|
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
|
from processing.tools import dataobjects
|
|
|
|
|
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
|
|
|
|
class Eliminate(GeoAlgorithm):
|
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
MODE = 'MODE'
|
|
|
|
|
KEEPSELECTION = 'KEEPSELECTION'
|
|
|
|
|
ATTRIBUTE = 'ATTRIBUTE'
|
|
|
|
|
COMPARISONVALUE = 'COMPARISONVALUE'
|
|
|
|
|
COMPARISON = 'COMPARISON'
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
2014-03-13 15:24:38 +01:00
|
|
|
|
MODES = ['Largest area', 'Smallest Area', 'Largest common boundary']
|
|
|
|
|
MODE_LARGEST_AREA = 0
|
|
|
|
|
MODE_SMALLEST_AREA = 1
|
|
|
|
|
MODE_BOUNDARY = 2
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
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,
|
2014-03-13 15:24:38 +01:00
|
|
|
|
'Use current selection in input layer (works only ' + \
|
|
|
|
|
'if called from toolbox)', False))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
self.addParameter(ParameterTableField(self.ATTRIBUTE,
|
|
|
|
|
'Selection attribute', self.INPUT))
|
|
|
|
|
self.comparisons = [
|
|
|
|
|
'==',
|
|
|
|
|
'!=',
|
|
|
|
|
'>',
|
|
|
|
|
'>=',
|
|
|
|
|
'<',
|
|
|
|
|
'<=',
|
|
|
|
|
'begins with',
|
|
|
|
|
'contains',
|
|
|
|
|
]
|
|
|
|
|
self.addParameter(ParameterSelection(self.COMPARISON, 'Comparison',
|
|
|
|
|
self.comparisons, default=0))
|
|
|
|
|
self.addParameter(ParameterString(self.COMPARISONVALUE, 'Value',
|
|
|
|
|
default='0'))
|
|
|
|
|
self.addParameter(ParameterSelection(self.MODE,
|
2014-03-13 15:24:38 +01:00
|
|
|
|
'Merge selection with the neighbouring polygon with the ',
|
|
|
|
|
self.MODES))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, 'Cleaned layer'))
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2013-10-01 20:52:22 +03:00
|
|
|
|
inLayer = dataobjects.getObjectFromUri(
|
|
|
|
|
self.getParameterValue(self.INPUT))
|
2013-09-26 12:14:08 +02:00
|
|
|
|
boundary = self.getParameterValue(self.MODE) == self.MODE_BOUNDARY
|
2014-03-13 15:24:38 +01:00
|
|
|
|
smallestArea = self.getParameterValue(self.MODE) == self.MODE_SMALLEST_AREA
|
2013-09-26 12:14:08 +02:00
|
|
|
|
keepSelection = self.getParameterValue(self.KEEPSELECTION)
|
|
|
|
|
|
|
|
|
|
if not keepSelection:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# Make a selection with the values provided
|
2013-09-26 12:14:08 +02:00
|
|
|
|
attribute = self.getParameterValue(self.ATTRIBUTE)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
comparison = self.comparisons[
|
|
|
|
|
self.getParameterValue(self.COMPARISON)]
|
2013-09-26 12:14:08 +02:00
|
|
|
|
comparisonvalue = self.getParameterValue(self.COMPARISONVALUE)
|
|
|
|
|
|
|
|
|
|
selectindex = inLayer.dataProvider().fieldNameIndex(attribute)
|
|
|
|
|
selectType = inLayer.dataProvider().fields()[selectindex].type()
|
|
|
|
|
selectionError = False
|
|
|
|
|
|
|
|
|
|
if selectType == 2:
|
|
|
|
|
try:
|
|
|
|
|
y = int(comparisonvalue)
|
|
|
|
|
except ValueError:
|
|
|
|
|
selectionError = True
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg = 'Cannot convert "' + unicode(comparisonvalue) \
|
|
|
|
|
+ '" to integer'
|
2013-09-26 12:14:08 +02:00
|
|
|
|
elif selectType == 6:
|
|
|
|
|
try:
|
|
|
|
|
y = float(comparisonvalue)
|
|
|
|
|
except ValueError:
|
|
|
|
|
selectionError = True
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg = 'Cannot convert "' + unicode(comparisonvalue) \
|
|
|
|
|
+ '" to float'
|
|
|
|
|
elif selectType == 10:
|
|
|
|
|
# 10: string, boolean
|
2013-09-26 12:14:08 +02:00
|
|
|
|
try:
|
|
|
|
|
y = unicode(comparisonvalue)
|
|
|
|
|
except ValueError:
|
|
|
|
|
selectionError = True
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg = 'Cannot convert "' + unicode(comparisonvalue) \
|
|
|
|
|
+ '" to unicode'
|
|
|
|
|
elif selectType == 14:
|
|
|
|
|
# date
|
|
|
|
|
dateAndFormat = comparisonvalue.split(' ')
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
|
|
if len(dateAndFormat) == 1:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# QtCore.QDate object
|
|
|
|
|
y = QLocale.system().toDate(dateAndFormat[0])
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
|
|
if y.isNull():
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg = 'Cannot convert "' + unicode(dateAndFormat) \
|
|
|
|
|
+ '" to date with system date format ' \
|
|
|
|
|
+ QLocale.system().dateFormat()
|
2013-09-26 12:14:08 +02:00
|
|
|
|
elif len(dateAndFormat) == 2:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
y = QDate.fromString(dateAndFormat[0], dateAndFormat[1])
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
|
|
if y.isNull():
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg = 'Cannot convert "' + unicode(dateAndFormat[0]) \
|
|
|
|
|
+ '" to date with format string "' \
|
|
|
|
|
+ unicode(dateAndFormat[1] + '". ')
|
2013-09-26 12:14:08 +02:00
|
|
|
|
else:
|
|
|
|
|
y = QDate()
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg = ''
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if y.isNull():
|
|
|
|
|
# Conversion was unsuccessfull
|
2013-09-26 12:14:08 +02:00
|
|
|
|
selectionError = True
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg += 'Enter the date and the date format, e.g. \
|
|
|
|
|
"07.26.2011" "MM.dd.yyyy".'
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if (comparison == 'begins with' or comparison == 'contains') \
|
|
|
|
|
and selectType != 10:
|
2013-09-26 12:14:08 +02:00
|
|
|
|
selectionError = True
|
2013-10-01 20:52:22 +03:00
|
|
|
|
msg = '"' + comparison \
|
|
|
|
|
+ '" can only be used with string fields'
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
|
|
selected = []
|
|
|
|
|
|
|
|
|
|
if selectionError:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
raise GeoAlgorithmExecutionException(
|
|
|
|
|
'Error in selection input: ' + msg)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
else:
|
|
|
|
|
for feature in inLayer.getFeatures():
|
|
|
|
|
aValue = feature.attributes()[selectindex]
|
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if aValue is None:
|
2013-09-26 12:14:08 +02:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if selectType == 2:
|
|
|
|
|
x = int(aValue)
|
|
|
|
|
elif selectType == 6:
|
|
|
|
|
x = float(aValue)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
elif selectType == 10:
|
|
|
|
|
# 10: string, boolean
|
2013-09-26 12:14:08 +02:00
|
|
|
|
x = unicode(aValue)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
elif selectType == 14:
|
|
|
|
|
# date
|
|
|
|
|
x = aValue # should be date
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
|
|
match = False
|
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if comparison == '==':
|
|
|
|
|
match = x == y
|
|
|
|
|
elif comparison == '!=':
|
|
|
|
|
match = x != y
|
|
|
|
|
elif comparison == '>':
|
|
|
|
|
match = x > y
|
|
|
|
|
elif comparison == '>=':
|
|
|
|
|
match = x >= y
|
|
|
|
|
elif comparison == '<':
|
|
|
|
|
match = x < y
|
|
|
|
|
elif comparison == '<=':
|
|
|
|
|
match = x <= y
|
|
|
|
|
elif comparison == 'begins with':
|
2013-09-26 12:14:08 +02:00
|
|
|
|
match = x.startswith(y)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
elif comparison == 'contains':
|
|
|
|
|
match = x.find(y) >= 0
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if match:
|
2013-09-26 12:14:08 +02:00
|
|
|
|
selected.append(feature.id())
|
|
|
|
|
|
|
|
|
|
inLayer.setSelectedFeatures(selected)
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-09-26 12:14:08 +02:00
|
|
|
|
if inLayer.selectedFeatureCount() == 0:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
|
|
|
|
|
self.commandLineName()
|
|
|
|
|
+ '(No selection in input layer "'
|
|
|
|
|
+ self.getParameterValue(self.INPUT) + '")')
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# Keep references to the features to eliminate
|
2013-09-26 12:14:08 +02:00
|
|
|
|
featToEliminate = []
|
|
|
|
|
for aFeat in inLayer.selectedFeatures():
|
2013-10-01 20:52:22 +03:00
|
|
|
|
featToEliminate.append(aFeat)
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# Delete all features to eliminate in inLayer (we won't save this)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
inLayer.startEditing()
|
|
|
|
|
inLayer.deleteSelectedFeatures()
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
|
|
|
|
# ANALYZE
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if len(featToEliminate) > 0: # Prevent zero division
|
2013-09-26 12:14:08 +02:00
|
|
|
|
start = 20.00
|
2013-10-01 20:52:22 +03:00
|
|
|
|
add = 80.00 / len(featToEliminate)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
else:
|
|
|
|
|
start = 100
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
progress.setPercentage(start)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
madeProgress = True
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# We go through the list and see if we find any polygons we can
|
|
|
|
|
# merge the selected with. If we have no success with some we
|
|
|
|
|
# merge and then restart the whole story.
|
|
|
|
|
while madeProgress: # Check if we made any progress
|
2013-09-26 12:14:08 +02:00
|
|
|
|
madeProgress = False
|
|
|
|
|
featNotEliminated = []
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# Iterate over the polygons to eliminate
|
|
|
|
|
for i in range(len(featToEliminate)):
|
2013-09-26 12:14:08 +02:00
|
|
|
|
feat = featToEliminate.pop()
|
|
|
|
|
geom2Eliminate = feat.geometry()
|
|
|
|
|
bbox = geom2Eliminate.boundingBox()
|
2013-10-01 20:52:22 +03:00
|
|
|
|
fit = inLayer.getFeatures(
|
|
|
|
|
QgsFeatureRequest().setFilterRect(bbox))
|
2013-09-26 12:14:08 +02:00
|
|
|
|
mergeWithFid = None
|
|
|
|
|
mergeWithGeom = None
|
|
|
|
|
max = 0
|
2014-03-13 15:24:38 +01:00
|
|
|
|
min = -1
|
2013-09-26 12:14:08 +02:00
|
|
|
|
selFeat = QgsFeature()
|
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
while fit.nextFeature(selFeat):
|
2013-09-26 12:14:08 +02:00
|
|
|
|
selGeom = selFeat.geometry()
|
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if geom2Eliminate.intersects(selGeom):
|
|
|
|
|
# We have a candidate
|
|
|
|
|
iGeom = geom2Eliminate.intersection(selGeom)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
|
|
if boundary:
|
|
|
|
|
selValue = iGeom.length()
|
2013-10-01 20:52:22 +03:00
|
|
|
|
else:
|
2014-03-13 15:24:38 +01:00
|
|
|
|
# area. We need a common boundary in
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# order to merge
|
2013-09-26 12:14:08 +02:00
|
|
|
|
if 0 < iGeom.length():
|
|
|
|
|
selValue = selGeom.area()
|
2013-09-20 12:46:18 +02:00
|
|
|
|
else:
|
2014-03-13 15:24:38 +01:00
|
|
|
|
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)
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# End while fit
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if mergeWithFid is not None:
|
|
|
|
|
# A successful candidate
|
|
|
|
|
newGeom = mergeWithGeom.combine(geom2Eliminate)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
if inLayer.changeGeometry(mergeWithFid, newGeom):
|
2013-09-26 12:14:08 +02:00
|
|
|
|
madeProgress = True
|
|
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2014-03-13 15:24:38 +01:00
|
|
|
|
'Could not replace geometry of feature ' + \
|
|
|
|
|
'with id %s' % mergeWithFid)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
|
|
start = start + add
|
2013-10-01 20:52:22 +03:00
|
|
|
|
progress.setPercentage(start)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
featNotEliminated.append(feat)
|
|
|
|
|
|
|
|
|
|
# End for featToEliminate
|
|
|
|
|
|
2013-09-26 12:14:08 +02:00
|
|
|
|
featToEliminate = featNotEliminated
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# End while
|
|
|
|
|
|
|
|
|
|
# Create output
|
2013-09-26 12:14:08 +02:00
|
|
|
|
provider = inLayer.dataProvider()
|
2013-10-01 20:52:22 +03:00
|
|
|
|
output = self.getOutputFromName(self.OUTPUT)
|
|
|
|
|
writer = output.getVectorWriter(provider.fields(),
|
|
|
|
|
provider.geometryType(), inLayer.crs())
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# Write all features that are left over to output layer
|
2013-09-26 12:14:08 +02:00
|
|
|
|
iterator = inLayer.getFeatures()
|
|
|
|
|
for feature in iterator:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
writer.addFeature(feature)
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
# Leave inLayer untouched
|
2013-09-26 12:14:08 +02:00
|
|
|
|
inLayer.rollBack()
|
2013-09-20 12:46:18 +02:00
|
|
|
|
|
2013-09-26 12:14:08 +02:00
|
|
|
|
for feature in featNotEliminated:
|
2013-10-01 20:52:22 +03:00
|
|
|
|
writer.addFeature(feature)
|