Skip to content

Commit 63ed164

Browse files
committed
fixed bug in mmqgis/ftools algs in sextante after adaptation to new vector api
1 parent cb566c0 commit 63ed164

File tree

5 files changed

+45
-75
lines changed

5 files changed

+45
-75
lines changed

python/plugins/sextante/algs/ftools/FToolsUtils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ def getUniqueValuesCount(layer, fieldIndex):
110110

111111
# From two input field maps, create single field map
112112
def combineVectorFields( layerA, layerB ):
113-
fieldsA = layerA.fields()
114-
fieldsB = layerB.fields()
113+
fieldsA = layerA.dataProvider().fields()
114+
fieldsB = layerB.dataProvider().fields()
115115
fieldsB = testForUniqueness( fieldsA, fieldsB )
116-
fieldsA.extend( fieldsB )
116+
for field in fieldsB:
117+
fieldsA.append(field)
117118
return fieldsA
118119

119120
# Check if two input field maps are unique, and resolve name issues if they aren't

python/plugins/sextante/algs/ftools/Intersection.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19+
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
1920
__author__ = 'Victor Olaya'
2021
__date__ = 'August 2012'
2122
__copyright__ = '(C) 2012, Victor Olaya'
@@ -46,25 +47,9 @@ class Intersection(GeoAlgorithm):
4647
def processAlgorithm(self, progress):
4748
vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT))
4849
vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT2))
49-
GEOS_EXCEPT = True
50-
FEATURE_EXCEPT = True
5150
vproviderA = vlayerA.dataProvider()
52-
vproviderB = vlayerB.dataProvider()
5351

54-
# check for crs compatibility
55-
crsA = vproviderA.crs()
56-
crsB = vproviderB.crs()
57-
if not crsA.isValid() or not crsB.isValid():
58-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Invalid CRS. Results might be unexpected")
59-
else:
60-
if not crsA != crsB:
61-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Non-matching CRSs. Results might be unexpected")
6252
fields = utils.combineVectorFields(vlayerA, vlayerB)
63-
#=======================================================================
64-
# longNames = ftools_utils.checkFieldNameLength( fields )
65-
# if not longNames.isEmpty():
66-
# raise GeoAlgorithmExecutionException("Following field names are longer than 10 characters:\n" + longNames.join('\n') )
67-
#=======================================================================
6853
writer = self.getOutputFromName(Intersection.OUTPUT).getVectorWriter(fields, vproviderA.geometryType(), vproviderA.crs() )
6954
inFeatA = QgsFeature()
7055
inFeatB = QgsFeature()
@@ -92,20 +77,19 @@ def processAlgorithm(self, progress):
9277
int_geom = QgsGeometry( int_com.difference( int_sym ) )
9378
try:
9479
outFeat.setGeometry( int_geom )
95-
outFeat.setAttributes( atMapA.extend( atMapB ) )
80+
attrs = []
81+
attrs.extend(atMapA)
82+
attrs.extend(atMapB)
83+
outFeat.setAttributes(attrs)
9684
writer.addFeature( outFeat )
9785
except:
98-
FEATURE_EXCEPT = False
99-
continue
86+
raise GeoAlgorithmExecutionException("Feature exception while computing intersection")
10087
except:
101-
GEOS_EXCEPT = False
102-
break
88+
raise GeoAlgorithmExecutionException("Geometry exception while computing intersection")
89+
10390

10491
del writer
105-
if not GEOS_EXCEPT:
106-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing intersection")
107-
if not FEATURE_EXCEPT:
108-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing intersection")
92+
10993

11094
def defineCharacteristics(self):
11195
self.name = "Intersection"

python/plugins/sextante/algs/ftools/SelectByLocation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def processAlgorithm(self, progress):
8181
geom = QgsGeometry(feat.geometry())
8282
intersects = index.intersects(geom.boundingBox())
8383
for i in intersects:
84-
inputProvider.featureAtId(i, infeat, True)
84+
inputLayer.featureAtId(i, infeat, True)
8585
tmpGeom = QgsGeometry( infeat.geometry() )
8686
if geom.intersects(tmpGeom):
8787
selectedSet.append(infeat.id())

python/plugins/sextante/algs/ftools/Union.py

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19+
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
1920

2021
__author__ = 'Victor Olaya'
2122
__date__ = 'August 2012'
@@ -50,20 +51,7 @@ def processAlgorithm(self, progress):
5051
GEOS_EXCEPT = True
5152
FEATURE_EXCEPT = True
5253
vproviderA = vlayerA.dataProvider()
53-
allAttrsA = vproviderA.attributeIndexes()
54-
vproviderA.select( allAttrsA )
55-
vproviderB = vlayerB.dataProvider()
56-
allAttrsB = vproviderB.attributeIndexes()
57-
vproviderB.select( allAttrsB )
58-
59-
# check for crs compatibility
60-
crsA = vproviderA.crs()
61-
crsB = vproviderB.crs()
62-
if not crsA.isValid() or not crsB.isValid():
63-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Union. Invalid CRS. Results might be unexpected")
64-
else:
65-
if not crsA != crsB:
66-
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Union. Non-matching CRSs. Results might be unexpected")
54+
6755
fields = utils.combineVectorFields(vlayerA, vlayerB )
6856
#longNames = ftools_utils.checkFieldNameLength( fields )
6957
#if not longNames.isEmpty():
@@ -74,10 +62,9 @@ def processAlgorithm(self, progress):
7462
outFeat = QgsFeature()
7563
indexA = utils.createSpatialIndex(vlayerB)
7664
indexB = utils.createSpatialIndex(vlayerA)
77-
vproviderA.rewind()
65+
7866
count = 0
7967
nElement = 0
80-
8168
featuresA = QGisLayers.features(vlayerA)
8269
nFeat = len(featuresA)
8370
for inFeatA in featuresA:
@@ -99,30 +86,29 @@ def processAlgorithm(self, progress):
9986
FEATURE_EXCEPT = False
10087
else:
10188
for id in intersects:
102-
count += 1
103-
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
104-
atMapB = inFeatB.attributes()
105-
tmpGeom = QgsGeometry( inFeatB.geometry() )
106-
try:
89+
count += 1
90+
vlayerB.featureAtId( int( id ), inFeatB , True)
91+
atMapB = inFeatB.attributes()
92+
tmpGeom = QgsGeometry( inFeatB.geometry() )
93+
10794
if geom.intersects( tmpGeom ):
10895
found = True
10996
int_geom = geom.intersection( tmpGeom )
110-
97+
11198
if int_geom is None:
11299
# There was a problem creating the intersection
113-
GEOS_EXCEPT = False
114-
int_geom = QgsGeometry()
100+
raise GeoAlgorithmExecutionException("Geometry exception while computing intersection")
115101
else:
116102
int_geom = QgsGeometry(int_geom)
117-
103+
118104
if diff_geom.intersects( tmpGeom ):
119105
diff_geom = diff_geom.difference( tmpGeom )
120106
if diff_geom is None:
121107
# It's possible there was an error here?
122108
diff_geom = QgsGeometry()
123109
else:
124110
diff_geom = QgsGeometry(diff_geom)
125-
111+
126112
if int_geom.wkbType() == 0:
127113
# intersection produced different geomety types
128114
temp_list = int_geom.asGeometryCollection()
@@ -131,10 +117,13 @@ def processAlgorithm(self, progress):
131117
int_geom = QgsGeometry( i )
132118
try:
133119
outFeat.setGeometry( int_geom )
134-
outFeat.setAttributes( atMapA.extend( atMapB ) )
120+
attrs = []
121+
attrs.extend(atMapA)
122+
attrs.extend(atMapB)
123+
outFeat.setAttributes(attrs)
135124
writer.addFeature( outFeat )
136125
except Exception, err:
137-
FEATURE_EXCEPT = False
126+
raise GeoAlgorithmExecutionException("Feature exception while computing union")
138127
else:
139128
# this only happends if the bounding box
140129
# intersects, but the geometry doesn't
@@ -144,10 +133,8 @@ def processAlgorithm(self, progress):
144133
writer.addFeature( outFeat )
145134
except:
146135
# also shoudn't ever happen
147-
FEATURE_EXCEPT = False
148-
except Exception, err:
149-
GEOS_EXCEPT = False
150-
found = False
136+
raise GeoAlgorithmExecutionException("Feature exception while computing union")
137+
151138

152139
if found:
153140
try:
@@ -160,10 +147,9 @@ def processAlgorithm(self, progress):
160147
outFeat.setAttributes( atMapA )
161148
writer.addFeature( outFeat )
162149
except Exception, err:
163-
FEATURE_EXCEPT = False
150+
raise GeoAlgorithmExecutionException("Feature exception while computing union")
164151

165-
length = len( vproviderA.fields().values() )
166-
vproviderB.rewind()
152+
length = len(vproviderA.fields())
167153

168154
featuresA = QGisLayers.features(vlayerB)
169155
nFeat = len(featuresA)
@@ -172,8 +158,9 @@ def processAlgorithm(self, progress):
172158
add = False
173159
geom = QgsGeometry( inFeatA.geometry() )
174160
diff_geom = QgsGeometry( geom )
175-
atMap = inFeatA.attributes()
176-
atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
161+
atMap = [None] * length
162+
atMap.extend(inFeatA.attributes())
163+
#atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
177164
intersects = indexB.intersects( geom.boundingBox() )
178165

179166
if len(intersects) < 1:
@@ -182,10 +169,10 @@ def processAlgorithm(self, progress):
182169
outFeat.setAttributes( atMap )
183170
writer.addFeature( outFeat )
184171
except Exception, err:
185-
FEATURE_EXCEPT = False
172+
raise GeoAlgorithmExecutionException("Feature exception while computing union")
186173
else:
187174
for id in intersects:
188-
vlayerA.featureAtId( int( id ), inFeatB , True, allAttrsA )
175+
vlayerA.featureAtId( int( id ), inFeatB , True)
189176
atMapB = inFeatB.attributes()
190177
tmpGeom = QgsGeometry( inFeatB.geometry() )
191178
try:
@@ -199,15 +186,15 @@ def processAlgorithm(self, progress):
199186
outFeat.setAttributes( atMap )
200187
writer.addFeature( outFeat )
201188
except Exception, err:
202-
add = False
203-
GEOS_EXCEPT = False
189+
raise GeoAlgorithmExecutionException("Geometry exception while computing intersection")
204190

205191
if add:
206192
try:
207193
outFeat.setGeometry( diff_geom )
208194
outFeat.setAttributes( atMapB )
209195
writer.addFeature( outFeat )
210-
except Exception, err:
196+
except Exception, err:
197+
raise err
211198
FEATURE_EXCEPT = False
212199
nElement += 1
213200

@@ -223,4 +210,4 @@ def defineCharacteristics(self):
223210
self.group = "Vector overlay tools"
224211
self.addParameter(ParameterVector(Union.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
225212
self.addParameter(ParameterVector(Union.INPUT2, "Input layer 2", ParameterVector.VECTOR_TYPE_ANY))
226-
self.addOutput(OutputVector(Union.OUTPUT, "Intersection"))
213+
self.addOutput(OutputVector(Union.OUTPUT, "Union"))

python/plugins/sextante/algs/mmqgisx/MMQGISXAlgorithms.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
from sextante.parameters.ParameterNumber import ParameterNumber
2222
from sextante.parameters.ParameterSelection import ParameterSelection
2323
from sextante.parameters.ParameterString import ParameterString
24-
from sextante.parameters.ParameterTable import ParameterTable
2524
from sextante.parameters.ParameterTableField import ParameterTableField
2625
from sextante.parameters.ParameterVector import ParameterVector
27-
from sextante.outputs.OutputTable import OutputTable
2826
from sextante.outputs.OutputVector import OutputVector
29-
from sextante.algs.mmqgisx.mmqgisx_library import *
27+
from sextante.core.QGisLayers import QGisLayers
3028

3129

3230
class mmqgisx_delete_columns_algorithm(GeoAlgorithm):

0 commit comments

Comments
 (0)