2014-09-18 19:58:41 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SymetricalDifference.py
|
|
|
|
---------------------
|
|
|
|
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$'
|
|
|
|
|
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 qgis.core import QgsFeature, QgsGeometry, QgsFeatureRequest, NULL
|
2014-09-18 19:58:41 +03:00
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
|
|
|
|
|
|
|
class SymetricalDifference(GeoAlgorithm):
|
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
OVERLAY = 'OVERLAY'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
|
|
|
self.name = 'Symetrical difference'
|
|
|
|
self.group = 'Vector overlay tools'
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY]))
|
2014-09-18 19:58:41 +03:00
|
|
|
self.addParameter(ParameterVector(self.OVERLAY,
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Difference layer'), [ParameterVector.VECTOR_TYPE_ANY]))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT,
|
|
|
|
self.tr('Symetrical 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))
|
|
|
|
|
|
|
|
providerA = layerA.dataProvider()
|
|
|
|
providerB = layerB.dataProvider()
|
|
|
|
|
|
|
|
GEOS_EXCEPT = True
|
|
|
|
FEATURE_EXCEPT = True
|
|
|
|
|
|
|
|
fields = vector.combineVectorFields(layerA, layerB)
|
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
|
|
|
|
fields, providerA.geometryType(), providerA.crs())
|
|
|
|
|
|
|
|
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:
|
|
|
|
add = True
|
|
|
|
geom = QgsGeometry(featA.geometry())
|
|
|
|
diffGeom = QgsGeometry(geom)
|
|
|
|
attrs = featA.attributes()
|
|
|
|
intersects = indexA.intersects(geom.boundingBox())
|
|
|
|
for i in intersects:
|
|
|
|
providerB.getFeatures(QgsFeatureRequest().setFilterFid(i)).nextFeature(featB)
|
|
|
|
tmpGeom = QgsGeometry(featB.geometry())
|
|
|
|
try:
|
|
|
|
if diffGeom.intersects(tmpGeom):
|
|
|
|
diffGeom = QgsGeometry(diffGeom.difference(tmpGeom))
|
|
|
|
except:
|
|
|
|
add = False
|
|
|
|
GEOS_EXCEPT = False
|
|
|
|
break
|
|
|
|
if add:
|
|
|
|
try:
|
|
|
|
outFeat.setGeometry(diffGeom)
|
|
|
|
outFeat.setAttributes(attrs)
|
|
|
|
writer.addFeature(outFeat)
|
|
|
|
except:
|
|
|
|
FEATURE_EXCEPT = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
progress.setPercentage(int(count * total))
|
|
|
|
|
|
|
|
length = len(providerA.fields())
|
|
|
|
|
|
|
|
for featA in featuresB:
|
|
|
|
add = True
|
|
|
|
geom = QgsGeometry(featA.geometry())
|
|
|
|
diffGeom = QgsGeometry(geom)
|
|
|
|
attrs = featA.attributes()
|
|
|
|
attrs = [NULL] * length + attrs
|
|
|
|
intersects = indexB.intersects(geom.boundingBox())
|
|
|
|
for i in intersects:
|
|
|
|
providerA.getFeatures(QgsFeatureRequest().setFilterFid(i)).nextFeature(featB)
|
|
|
|
tmpGeom = QgsGeometry(featB.geometry())
|
|
|
|
try:
|
|
|
|
if diffGeom.intersects(tmpGeom):
|
|
|
|
diffGeom = QgsGeometry(diffGeom.difference(tmpGeom))
|
|
|
|
except:
|
|
|
|
add = False
|
|
|
|
GEOS_EXCEPT = False
|
|
|
|
break
|
|
|
|
if add:
|
|
|
|
try:
|
|
|
|
outFeat.setGeometry(diffGeom)
|
|
|
|
outFeat.setAttributes(attrs)
|
|
|
|
writer.addFeature(outFeat)
|
|
|
|
except:
|
|
|
|
FEATURE_EXCEPT = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
progress.setPercentage(int(count * total))
|
|
|
|
|
|
|
|
del writer
|
|
|
|
|
|
|
|
if not GEOS_EXCEPT:
|
2015-01-15 20:41:15 +02:00
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
|
|
|
|
self.tr('Geometry exception while computing symetrical difference'))
|
2014-09-18 19:58:41 +03:00
|
|
|
if not FEATURE_EXCEPT:
|
2015-01-15 20:41:15 +02:00
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
|
|
|
|
self.tr('Feature exception while computing symetrical difference'))
|