-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] add Symetrical difference algorithm (addresses #5953)
- Loading branch information
Showing
2 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
python/plugins/processing/algs/qgis/ftools/SymetricalDifference.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# -*- 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$' | ||
|
||
from PyQt4.QtCore import * | ||
from qgis.core import * | ||
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' | ||
self.addParameter(ParameterVector(self.INPUT, 'Input layer', | ||
[ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addParameter(ParameterVector(self.OVERLAY, | ||
'Difference layer', | ||
[ParameterVector.VECTOR_TYPE_ANY])) | ||
self.addOutput(OutputVector(self.OUTPUT, 'Symetrical difference')) | ||
|
||
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: | ||
ProcessingLog.addToLog( | ||
ProcessingLog.LOG_WARNING, | ||
'Geometry exception while computing symetrical difference') | ||
if not FEATURE_EXCEPT: | ||
ProcessingLog.addToLog( | ||
ProcessingLog.LOG_WARNING, | ||
'Feature exception while computing symetrical difference') |