Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed bug in mmqgis/ftools algs in sextante after adaptation to new v…
…ector api
  • Loading branch information
volaya committed Feb 6, 2013
1 parent cb566c0 commit 63ed164
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 75 deletions.
7 changes: 4 additions & 3 deletions python/plugins/sextante/algs/ftools/FToolsUtils.py
Expand Up @@ -110,10 +110,11 @@ def getUniqueValuesCount(layer, fieldIndex):

# From two input field maps, create single field map
def combineVectorFields( layerA, layerB ):
fieldsA = layerA.fields()
fieldsB = layerB.fields()
fieldsA = layerA.dataProvider().fields()
fieldsB = layerB.dataProvider().fields()
fieldsB = testForUniqueness( fieldsA, fieldsB )
fieldsA.extend( fieldsB )
for field in fieldsB:
fieldsA.append(field)
return fieldsA

# Check if two input field maps are unique, and resolve name issues if they aren't
Expand Down
34 changes: 9 additions & 25 deletions python/plugins/sextante/algs/ftools/Intersection.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
Expand Down Expand Up @@ -46,25 +47,9 @@ class Intersection(GeoAlgorithm):
def processAlgorithm(self, progress):
vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT))
vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT2))
GEOS_EXCEPT = True
FEATURE_EXCEPT = True
vproviderA = vlayerA.dataProvider()
vproviderB = vlayerB.dataProvider()

# check for crs compatibility
crsA = vproviderA.crs()
crsB = vproviderB.crs()
if not crsA.isValid() or not crsB.isValid():
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Invalid CRS. Results might be unexpected")
else:
if not crsA != crsB:
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Non-matching CRSs. Results might be unexpected")
fields = utils.combineVectorFields(vlayerA, vlayerB)
#=======================================================================
# longNames = ftools_utils.checkFieldNameLength( fields )
# if not longNames.isEmpty():
# raise GeoAlgorithmExecutionException("Following field names are longer than 10 characters:\n" + longNames.join('\n') )
#=======================================================================
writer = self.getOutputFromName(Intersection.OUTPUT).getVectorWriter(fields, vproviderA.geometryType(), vproviderA.crs() )
inFeatA = QgsFeature()
inFeatB = QgsFeature()
Expand Down Expand Up @@ -92,20 +77,19 @@ def processAlgorithm(self, progress):
int_geom = QgsGeometry( int_com.difference( int_sym ) )
try:
outFeat.setGeometry( int_geom )
outFeat.setAttributes( atMapA.extend( atMapB ) )
attrs = []
attrs.extend(atMapA)
attrs.extend(atMapB)
outFeat.setAttributes(attrs)
writer.addFeature( outFeat )
except:
FEATURE_EXCEPT = False
continue
raise GeoAlgorithmExecutionException("Feature exception while computing intersection")
except:
GEOS_EXCEPT = False
break
raise GeoAlgorithmExecutionException("Geometry exception while computing intersection")


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


def defineCharacteristics(self):
self.name = "Intersection"
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/algs/ftools/SelectByLocation.py
Expand Up @@ -81,7 +81,7 @@ def processAlgorithm(self, progress):
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
for i in intersects:
inputProvider.featureAtId(i, infeat, True)
inputLayer.featureAtId(i, infeat, True)
tmpGeom = QgsGeometry( infeat.geometry() )
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
Expand Down
73 changes: 30 additions & 43 deletions python/plugins/sextante/algs/ftools/Union.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down Expand Up @@ -50,20 +51,7 @@ def processAlgorithm(self, progress):
GEOS_EXCEPT = True
FEATURE_EXCEPT = True
vproviderA = vlayerA.dataProvider()
allAttrsA = vproviderA.attributeIndexes()
vproviderA.select( allAttrsA )
vproviderB = vlayerB.dataProvider()
allAttrsB = vproviderB.attributeIndexes()
vproviderB.select( allAttrsB )

# check for crs compatibility
crsA = vproviderA.crs()
crsB = vproviderB.crs()
if not crsA.isValid() or not crsB.isValid():
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Union. Invalid CRS. Results might be unexpected")
else:
if not crsA != crsB:
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Union. Non-matching CRSs. Results might be unexpected")

fields = utils.combineVectorFields(vlayerA, vlayerB )
#longNames = ftools_utils.checkFieldNameLength( fields )
#if not longNames.isEmpty():
Expand All @@ -74,10 +62,9 @@ def processAlgorithm(self, progress):
outFeat = QgsFeature()
indexA = utils.createSpatialIndex(vlayerB)
indexB = utils.createSpatialIndex(vlayerA)
vproviderA.rewind()

count = 0
nElement = 0

featuresA = QGisLayers.features(vlayerA)
nFeat = len(featuresA)
for inFeatA in featuresA:
Expand All @@ -99,30 +86,29 @@ def processAlgorithm(self, progress):
FEATURE_EXCEPT = False
else:
for id in intersects:
count += 1
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
atMapB = inFeatB.attributes()
tmpGeom = QgsGeometry( inFeatB.geometry() )
try:
count += 1
vlayerB.featureAtId( int( id ), inFeatB , True)
atMapB = inFeatB.attributes()
tmpGeom = QgsGeometry( inFeatB.geometry() )

if geom.intersects( tmpGeom ):
found = True
int_geom = geom.intersection( tmpGeom )

if int_geom is None:
# There was a problem creating the intersection
GEOS_EXCEPT = False
int_geom = QgsGeometry()
raise GeoAlgorithmExecutionException("Geometry exception while computing intersection")
else:
int_geom = QgsGeometry(int_geom)

if diff_geom.intersects( tmpGeom ):
diff_geom = diff_geom.difference( tmpGeom )
if diff_geom is None:
# It's possible there was an error here?
diff_geom = QgsGeometry()
else:
diff_geom = QgsGeometry(diff_geom)

if int_geom.wkbType() == 0:
# intersection produced different geomety types
temp_list = int_geom.asGeometryCollection()
Expand All @@ -131,10 +117,13 @@ def processAlgorithm(self, progress):
int_geom = QgsGeometry( i )
try:
outFeat.setGeometry( int_geom )
outFeat.setAttributes( atMapA.extend( atMapB ) )
attrs = []
attrs.extend(atMapA)
attrs.extend(atMapB)
outFeat.setAttributes(attrs)
writer.addFeature( outFeat )
except Exception, err:
FEATURE_EXCEPT = False
raise GeoAlgorithmExecutionException("Feature exception while computing union")
else:
# this only happends if the bounding box
# intersects, but the geometry doesn't
Expand All @@ -144,10 +133,8 @@ def processAlgorithm(self, progress):
writer.addFeature( outFeat )
except:
# also shoudn't ever happen
FEATURE_EXCEPT = False
except Exception, err:
GEOS_EXCEPT = False
found = False
raise GeoAlgorithmExecutionException("Feature exception while computing union")


if found:
try:
Expand All @@ -160,10 +147,9 @@ def processAlgorithm(self, progress):
outFeat.setAttributes( atMapA )
writer.addFeature( outFeat )
except Exception, err:
FEATURE_EXCEPT = False
raise GeoAlgorithmExecutionException("Feature exception while computing union")

length = len( vproviderA.fields().values() )
vproviderB.rewind()
length = len(vproviderA.fields())

featuresA = QGisLayers.features(vlayerB)
nFeat = len(featuresA)
Expand All @@ -172,8 +158,9 @@ def processAlgorithm(self, progress):
add = False
geom = QgsGeometry( inFeatA.geometry() )
diff_geom = QgsGeometry( geom )
atMap = inFeatA.attributes()
atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
atMap = [None] * length
atMap.extend(inFeatA.attributes())
#atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
intersects = indexB.intersects( geom.boundingBox() )

if len(intersects) < 1:
Expand All @@ -182,10 +169,10 @@ def processAlgorithm(self, progress):
outFeat.setAttributes( atMap )
writer.addFeature( outFeat )
except Exception, err:
FEATURE_EXCEPT = False
raise GeoAlgorithmExecutionException("Feature exception while computing union")
else:
for id in intersects:
vlayerA.featureAtId( int( id ), inFeatB , True, allAttrsA )
vlayerA.featureAtId( int( id ), inFeatB , True)
atMapB = inFeatB.attributes()
tmpGeom = QgsGeometry( inFeatB.geometry() )
try:
Expand All @@ -199,15 +186,15 @@ def processAlgorithm(self, progress):
outFeat.setAttributes( atMap )
writer.addFeature( outFeat )
except Exception, err:
add = False
GEOS_EXCEPT = False
raise GeoAlgorithmExecutionException("Geometry exception while computing intersection")

if add:
try:
outFeat.setGeometry( diff_geom )
outFeat.setAttributes( atMapB )
writer.addFeature( outFeat )
except Exception, err:
except Exception, err:
raise err
FEATURE_EXCEPT = False
nElement += 1

Expand All @@ -223,4 +210,4 @@ def defineCharacteristics(self):
self.group = "Vector overlay tools"
self.addParameter(ParameterVector(Union.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(Union.INPUT2, "Input layer 2", ParameterVector.VECTOR_TYPE_ANY))
self.addOutput(OutputVector(Union.OUTPUT, "Intersection"))
self.addOutput(OutputVector(Union.OUTPUT, "Union"))
4 changes: 1 addition & 3 deletions python/plugins/sextante/algs/mmqgisx/MMQGISXAlgorithms.py
Expand Up @@ -21,12 +21,10 @@
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterString import ParameterString
from sextante.parameters.ParameterTable import ParameterTable
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.parameters.ParameterVector import ParameterVector
from sextante.outputs.OutputTable import OutputTable
from sextante.outputs.OutputVector import OutputVector
from sextante.algs.mmqgisx.mmqgisx_library import *
from sextante.core.QGisLayers import QGisLayers


class mmqgisx_delete_columns_algorithm(GeoAlgorithm):
Expand Down

0 comments on commit 63ed164

Please sign in to comment.