2013-09-20 12:46:18 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2017-02-23 09:33:28 +01:00
|
|
|
EliminateSelection.py
|
2013-09-20 12:46:18 +02:00
|
|
|
---------------------
|
2017-02-23 09:33:28 +01:00
|
|
|
Date : January 2017
|
|
|
|
Copyright : (C) 2017 by Bernhard Ströbl
|
2013-09-20 12:46:18 +02:00
|
|
|
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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import range
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2013-09-26 12:14:08 +02:00
|
|
|
__author__ = 'Bernhard Ströbl'
|
2017-02-23 09:33:28 +01:00
|
|
|
__date__ = 'January 2017'
|
|
|
|
__copyright__ = '(C) 2017, 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$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-04-24 14:35:50 +10:00
|
|
|
from qgis.core import (QgsFeatureRequest,
|
|
|
|
QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsGeometry,
|
|
|
|
QgsMessageLog,
|
|
|
|
QgsProcessingUtils)
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
2017-03-04 19:41:23 +01:00
|
|
|
from processing.tools import dataobjects
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class EliminateSelection(QgisAlgorithm):
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
MODE = 'MODE'
|
2013-09-26 12:14:08 +02:00
|
|
|
|
2014-03-13 15:24:38 +01:00
|
|
|
MODE_LARGEST_AREA = 0
|
|
|
|
MODE_SMALLEST_AREA = 1
|
|
|
|
MODE_BOUNDARY = 2
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'eliminate.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.modes = [self.tr('Largest area'),
|
|
|
|
self.tr('Smallest Area'),
|
|
|
|
self.tr('Largest common boundary')]
|
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON]))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterSelection(self.MODE,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Merge selection with the neighbouring polygon with the'),
|
2015-08-31 16:59:11 +02:00
|
|
|
self.modes))
|
2017-02-23 09:33:28 +01:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Eliminated'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'eliminateselectedpolygons'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Eliminate selected polygons')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
inLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
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
|
|
|
|
2017-02-23 09:33:28 +01:00
|
|
|
if inLayer.selectedFeatureCount() == 0:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('{0}: (No selection in input layer "{1}")').format(self.displayName(), self.getParameterValue(self.INPUT)),
|
|
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2013-09-26 12:14:08 +02:00
|
|
|
featToEliminate = []
|
2017-02-23 09:33:28 +01:00
|
|
|
selFeatIds = inLayer.selectedFeatureIds()
|
|
|
|
output = self.getOutputFromName(self.OUTPUT)
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = output.getVectorWriter(inLayer.fields(), inLayer.wkbType(), inLayer.crs(), context)
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2017-02-23 09:33:28 +01:00
|
|
|
for aFeat in inLayer.getFeatures():
|
|
|
|
if aFeat.id() in selFeatIds:
|
|
|
|
# Keep references to the features to eliminate
|
|
|
|
featToEliminate.append(aFeat)
|
|
|
|
else:
|
|
|
|
# write the others to output
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(aFeat, QgsFeatureSink.FastInsert)
|
2017-02-23 09:33:28 +01:00
|
|
|
|
|
|
|
# Delete all features to eliminate in processLayer
|
|
|
|
processLayer = output.layer
|
2016-03-16 12:48:38 +01:00
|
|
|
processLayer.startEditing()
|
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
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(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()
|
2016-08-01 16:25:46 +10:00
|
|
|
geom2Eliminate = feat.geometry()
|
2013-09-26 12:14:08 +02:00
|
|
|
bbox = geom2Eliminate.boundingBox()
|
2016-03-16 12:48:38 +01:00
|
|
|
fit = processLayer.getFeatures(
|
2016-10-17 10:30:55 +10:00
|
|
|
QgsFeatureRequest().setFilterRect(bbox).setSubsetOfAttributes([]))
|
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()
|
|
|
|
|
2016-10-17 11:18:46 +10:00
|
|
|
# use prepared geometries for faster intersection tests
|
|
|
|
engine = QgsGeometry.createGeometryEngine(geom2Eliminate.geometry())
|
|
|
|
engine.prepareGeometry()
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
while fit.nextFeature(selFeat):
|
2016-08-01 16:25:46 +10:00
|
|
|
selGeom = selFeat.geometry()
|
2013-09-26 12:14:08 +02:00
|
|
|
|
2016-10-17 11:18:46 +10:00
|
|
|
if engine.intersects(selGeom.geometry()):
|
2013-10-01 20:52:22 +03:00
|
|
|
# We have a candidate
|
|
|
|
iGeom = geom2Eliminate.intersection(selGeom)
|
2013-09-26 12:14:08 +02:00
|
|
|
|
2016-08-02 08:39:58 +10:00
|
|
|
if not iGeom:
|
2015-04-16 17:54:15 +02:00
|
|
|
continue
|
|
|
|
|
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
|
|
|
|
2016-03-16 12:48:38 +01:00
|
|
|
if processLayer.changeGeometry(mergeWithFid, newGeom):
|
2013-09-26 12:14:08 +02:00
|
|
|
madeProgress = True
|
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('Could not replace geometry of feature with id {0}').format(mergeWithFid))
|
2013-09-26 12:14:08 +02:00
|
|
|
|
|
|
|
start = start + add
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(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
|
2017-02-23 09:33:28 +01:00
|
|
|
if not processLayer.commitChanges():
|
|
|
|
raise GeoAlgorithmExecutionException(self.tr('Could not commit changes'))
|
2013-09-20 12:46:18 +02:00
|
|
|
|
2013-09-26 12:14:08 +02:00
|
|
|
for feature in featNotEliminated:
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(feature, QgsFeatureSink.FastInsert)
|