2014-09-18 19:58:41 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2015-10-22 21:56:54 +02:00
|
|
|
SymmetricalDifference.py
|
2014-09-18 19:58:41 +03:00
|
|
|
---------------------
|
|
|
|
Date : September 2014
|
|
|
|
Copyright : (C) 2014 by Alexander Bruy
|
|
|
|
Email : alexander dot bruy at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'September 2014'
|
|
|
|
__copyright__ = '(C) 2014, Alexander Bruy'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__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 (QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsGeometry,
|
|
|
|
QgsFeatureRequest,
|
|
|
|
NULL,
|
|
|
|
QgsWkbTypes,
|
|
|
|
QgsMessageLog,
|
2017-09-21 15:28:22 +10:00
|
|
|
QgsProcessingUtils,
|
2017-06-22 18:21:16 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
2017-07-12 07:50:13 +10:00
|
|
|
QgsProcessingParameterFeatureSink,
|
2017-06-22 18:21:16 +10:00
|
|
|
QgsSpatialIndex)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import vector
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class SymmetricalDifference(QgisAlgorithm):
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
OVERLAY = 'OVERLAY'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
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', 'sym_difference.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector overlay')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-06-22 18:21:16 +10:00
|
|
|
|
2017-07-10 16:31:14 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
2017-06-22 18:21:16 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.OVERLAY,
|
|
|
|
self.tr('Difference layer')))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Symmetrical difference')))
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'symmetricaldifference'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Symmetrical difference')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-06-22 18:21:16 +10:00
|
|
|
sourceA = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
sourceB = self.parameterAsSource(parameters, self.OVERLAY, context)
|
|
|
|
|
|
|
|
geomType = QgsWkbTypes.multiType(sourceA.wkbType())
|
2017-09-21 15:28:22 +10:00
|
|
|
fields = QgsProcessingUtils.combineFields(sourceA.fields(), sourceB.fields())
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2017-06-22 18:21:16 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
fields, geomType, sourceA.sourceCrs())
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
featB = QgsFeature()
|
|
|
|
outFeat = QgsFeature()
|
|
|
|
|
2017-07-15 16:43:44 +10:00
|
|
|
indexA = QgsSpatialIndex(sourceA, feedback)
|
|
|
|
indexB = QgsSpatialIndex(sourceB.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(sourceA.sourceCrs())), feedback)
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / (sourceA.featureCount() * sourceB.featureCount()) if sourceA.featureCount() and sourceB.featureCount() else 1
|
2014-09-18 19:58:41 +03:00
|
|
|
count = 0
|
|
|
|
|
2017-06-22 18:21:16 +10:00
|
|
|
for featA in sourceA.getFeatures():
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2016-08-01 16:25:46 +10:00
|
|
|
geom = featA.geometry()
|
2014-09-18 19:58:41 +03:00
|
|
|
diffGeom = QgsGeometry(geom)
|
|
|
|
attrs = featA.attributes()
|
2017-06-22 18:21:16 +10:00
|
|
|
intersects = indexB.intersects(geom.boundingBox())
|
2016-10-17 10:30:55 +10:00
|
|
|
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
|
2017-06-22 18:21:16 +10:00
|
|
|
request.setDestinationCrs(sourceA.sourceCrs())
|
|
|
|
for featB in sourceB.getFeatures(request):
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2016-08-01 16:25:46 +10:00
|
|
|
tmpGeom = featB.geometry()
|
2016-01-29 11:03:38 +02:00
|
|
|
if diffGeom.intersects(tmpGeom):
|
|
|
|
diffGeom = QgsGeometry(diffGeom.difference(tmpGeom))
|
2016-12-15 12:25:20 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
outFeat.setGeometry(diffGeom)
|
|
|
|
outFeat.setAttributes(attrs)
|
2017-06-23 14:34:38 +10:00
|
|
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2016-12-15 12:25:20 +01:00
|
|
|
except:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'),
|
|
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
2016-12-15 12:25:20 +01:00
|
|
|
continue
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
count += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(count * total))
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2017-06-22 18:21:16 +10:00
|
|
|
length = len(sourceA.fields())
|
|
|
|
|
|
|
|
for featA in sourceB.getFeatures(QgsFeatureRequest().setDestinationCrs(sourceA.sourceCrs())):
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2016-08-01 16:25:46 +10:00
|
|
|
geom = featA.geometry()
|
2014-09-18 19:58:41 +03:00
|
|
|
diffGeom = QgsGeometry(geom)
|
|
|
|
attrs = featA.attributes()
|
|
|
|
attrs = [NULL] * length + attrs
|
2017-06-22 18:21:16 +10:00
|
|
|
intersects = indexA.intersects(geom.boundingBox())
|
2016-10-17 10:30:55 +10:00
|
|
|
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
|
2017-06-22 18:21:16 +10:00
|
|
|
for featB in sourceA.getFeatures(request):
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2016-08-01 16:25:46 +10:00
|
|
|
tmpGeom = featB.geometry()
|
2016-01-29 11:03:38 +02:00
|
|
|
if diffGeom.intersects(tmpGeom):
|
|
|
|
diffGeom = QgsGeometry(diffGeom.difference(tmpGeom))
|
2016-12-15 12:25:20 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
outFeat.setGeometry(diffGeom)
|
|
|
|
outFeat.setAttributes(attrs)
|
2017-06-23 14:34:38 +10:00
|
|
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2016-12-15 12:25:20 +01:00
|
|
|
except:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'),
|
|
|
|
self.tr('Processing'), QgsMessageLog.WARNING)
|
2016-12-15 12:25:20 +01:00
|
|
|
continue
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
count += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(count * total))
|
2014-09-18 19:58:41 +03:00
|
|
|
|
2017-06-22 18:21:16 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|