Skip to content

Commit

Permalink
review Difference tool
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Mar 21, 2013
1 parent aa4f70c commit 6df3540
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions python/plugins/sextante/algs/ftools/Difference.py
Expand Up @@ -22,90 +22,93 @@
# This will get replaced with a git SHA1 when you do a git archive # This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'



from PyQt4.QtCore import * from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import * from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector
from sextante.algs.ftools import FToolsUtils as utils
from sextante.core.SextanteLog import SextanteLog from sextante.core.SextanteLog import SextanteLog
from sextante.core.GeoAlgorithm import GeoAlgorithm from sextante.core.GeoAlgorithm import GeoAlgorithm


from sextante.parameters.ParameterVector import ParameterVector

from sextante.outputs.OutputVector import OutputVector

from sextante.algs.ftools import FToolsUtils as utils


class Difference(GeoAlgorithm): class Difference(GeoAlgorithm):


INPUT = "INPUT" INPUT = "INPUT"
INPUT2 = "INPUT2" OVERLAY = "OVERLAY"
OUTPUT = "OUTPUT" OUTPUT = "OUTPUT"


#=========================================================================== #===========================================================================
# def getIcon(self): # def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/difference.png") # return QtGui.QIcon(os.path.dirname(__file__) + "/icons/difference.png")
#=========================================================================== #===========================================================================


def defineCharacteristics(self):
self.name = "Difference"
self.group = "Vector overlay tools"
self.addParameter(ParameterVector(Difference.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(Difference.OVERLAY, "Difference layer", ParameterVector.VECTOR_TYPE_ANY))
self.addOutput(OutputVector(Difference.OUTPUT, "Difference"))

def processAlgorithm(self, progress): def processAlgorithm(self, progress):
vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Difference.INPUT)) layerA = QGisLayers.getObjectFromUri(self.getParameterValue(Difference.INPUT))
vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Difference.INPUT2)) layerB = QGisLayers.getObjectFromUri(self.getParameterValue(Difference.OVERLAY))

GEOS_EXCEPT = True GEOS_EXCEPT = True

FEATURE_EXCEPT = True FEATURE_EXCEPT = True
vproviderA = vlayerA.dataProvider()
vproviderB = vlayerB.dataProvider() writer = self.getOutputFromName(Difference.OUTPUT).getVectorWriter(layerA.pendingFields(),
fields = vproviderA.fields() layerA.dataProvider().geometryType(), layerA.dataProvider().crs())
# check for crs compatibility
crsA = vproviderA.crs()
crsB = vproviderB.crs()
if not crsA.isValid() or not crsB.isValid():
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Difference. Invalid CRS. Results might be unexpected")
else:
if crsA != crsB:
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Difference. Non-matching CRSs. Results might be unexpected")
writer = self.getOutputFromName(Difference.OUTPUT).getVectorWriter(fields, vproviderA.geometryType(), vproviderA.crs() )
inFeatA = QgsFeature() inFeatA = QgsFeature()
inFeatB = QgsFeature() inFeatB = QgsFeature()
outFeat = QgsFeature() outFeat = QgsFeature()
index = utils.createSpatialIndex(vlayerB)
nElement = 0 index = utils.createSpatialIndex(layerB)
selectionA = QGisLayers.features(vlayerA)
nFeat = len(selectionA) selectionA = QGisLayers.features(layerA)

current = 0
total = 100.0 / float(len(selectionA))

for inFeatA in selectionA: for inFeatA in selectionA:
nElement += 1
progress.setPercentage(nElement/float(nFeat) * 100)
add = True add = True
geom = QgsGeometry( inFeatA.geometry() ) geom = QgsGeometry(inFeatA.geometry())
diff_geom = QgsGeometry( geom ) diff_geom = QgsGeometry(geom)
atMap = inFeatA.attributes() attrs = inFeatA.attributes()
intersects = index.intersects( geom.boundingBox() ) intersections = index.intersects(geom.boundingBox())
for id in intersects: for i in intersections:
vlayerB.featureAtId( int( id ), inFeatB , True) request = QgsFeatureRequest().setFilterFid(i)
tmpGeom = QgsGeometry( inFeatB.geometry() ) inFeatB = layerB.getFeatures(request).next()
tmpGeom = QgsGeometry(inFeatB.geometry())
try: try:
if diff_geom.intersects( tmpGeom ): if diff_geom.intersects(tmpGeom):
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) ) diff_geom = QgsGeometry(diff_geom.difference(tmpGeom))
except: except:
GEOS_EXCEPT = False GEOS_EXCEPT = False
add = False add = False
break break

if add: if add:
try: try:
outFeat.setGeometry( diff_geom ) outFeat.setGeometry(diff_geom)
outFeat.setAttributes( atMap ) outFeat.setAttributes(attrs)
writer.addFeature( outFeat ) writer.addFeature(outFeat)
except: except:
FEATURE_EXCEPT = False FEATURE_EXCEPT = False
continue continue


current += 1
progress.setPercentage(int(current * total))


del writer del writer

if not GEOS_EXCEPT: if not GEOS_EXCEPT:
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing difference") SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing difference")
if not FEATURE_EXCEPT: if not FEATURE_EXCEPT:
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing difference") SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing difference")

def defineCharacteristics(self):
self.name = "Difference"
self.group = "Vector overlay tools"
self.addParameter(ParameterVector(Difference.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(Difference.INPUT2, "Difference layer", ParameterVector.VECTOR_TYPE_ANY))
self.addOutput(OutputVector(Difference.OUTPUT, "Difference"))

0 comments on commit 6df3540

Please sign in to comment.