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
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
from qgis.core import Qgis, QgsFeature, QgsGeometry, QgsFeatureRequest, NULL, QgsWkbTypes
|
2014-09-18 19:58:41 +03:00
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2016-01-29 11:03:38 +02:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-09-18 19:58:41 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
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
|
|
|
|
2015-04-10 15:31:31 +03:00
|
|
|
class SymmetricalDifference(GeoAlgorithm):
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
OVERLAY = 'OVERLAY'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
def getIcon(self):
|
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'sym_difference.png'))
|
|
|
|
|
2014-09-18 19:58:41 +03:00
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Symmetrical difference')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Vector overlay tools')
|
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')))
|
2014-09-18 19:58:41 +03:00
|
|
|
self.addParameter(ParameterVector(self.OVERLAY,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Difference layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT,
|
2015-10-22 21:56:54 +02:00
|
|
|
self.tr('Symmetrical difference')))
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
layerA = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(self.INPUT))
|
|
|
|
layerB = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(self.OVERLAY))
|
|
|
|
|
2016-10-19 19:58:23 +07:00
|
|
|
geomType = QgsWkbTypes.multiType(layerA.wkbType())
|
2014-09-18 19:58:41 +03:00
|
|
|
fields = vector.combineVectorFields(layerA, layerB)
|
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
|
2016-08-04 07:33:49 +10:00
|
|
|
fields, geomType, layerA.crs())
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
featB = QgsFeature()
|
|
|
|
outFeat = QgsFeature()
|
|
|
|
|
|
|
|
indexA = vector.spatialindex(layerB)
|
|
|
|
indexB = vector.spatialindex(layerA)
|
|
|
|
|
|
|
|
featuresA = vector.features(layerA)
|
|
|
|
featuresB = vector.features(layerB)
|
|
|
|
|
|
|
|
total = 100.0 / (len(featuresA) * len(featuresB))
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
for featA in featuresA:
|
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()
|
|
|
|
intersects = indexA.intersects(geom.boundingBox())
|
2016-10-17 10:30:55 +10:00
|
|
|
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
|
|
|
|
for featB in layerB.getFeatures(request):
|
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)
|
|
|
|
writer.addFeature(outFeat)
|
|
|
|
except:
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
|
|
|
|
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
|
|
|
|
continue
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
count += 1
|
|
|
|
progress.setPercentage(int(count * total))
|
|
|
|
|
2016-08-04 07:33:49 +10:00
|
|
|
length = len(layerA.fields())
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
for featA in featuresB:
|
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
|
|
|
|
intersects = indexB.intersects(geom.boundingBox())
|
2016-10-17 10:30:55 +10:00
|
|
|
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
|
|
|
|
for featB in layerA.getFeatures(request):
|
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)
|
|
|
|
writer.addFeature(outFeat)
|
|
|
|
except:
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
|
|
|
|
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
|
|
|
|
continue
|
2014-09-18 19:58:41 +03:00
|
|
|
|
|
|
|
count += 1
|
|
|
|
progress.setPercentage(int(count * total))
|
|
|
|
|
|
|
|
del writer
|