16
16
* *
17
17
***************************************************************************
18
18
"""
19
+ from sextante .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
19
20
20
21
__author__ = 'Victor Olaya'
21
22
__date__ = 'August 2012'
@@ -50,20 +51,7 @@ def processAlgorithm(self, progress):
50
51
GEOS_EXCEPT = True
51
52
FEATURE_EXCEPT = True
52
53
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
+
67
55
fields = utils .combineVectorFields (vlayerA , vlayerB )
68
56
#longNames = ftools_utils.checkFieldNameLength( fields )
69
57
#if not longNames.isEmpty():
@@ -74,10 +62,9 @@ def processAlgorithm(self, progress):
74
62
outFeat = QgsFeature ()
75
63
indexA = utils .createSpatialIndex (vlayerB )
76
64
indexB = utils .createSpatialIndex (vlayerA )
77
- vproviderA . rewind ()
65
+
78
66
count = 0
79
67
nElement = 0
80
-
81
68
featuresA = QGisLayers .features (vlayerA )
82
69
nFeat = len (featuresA )
83
70
for inFeatA in featuresA :
@@ -99,30 +86,29 @@ def processAlgorithm(self, progress):
99
86
FEATURE_EXCEPT = False
100
87
else :
101
88
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
+
107
94
if geom .intersects ( tmpGeom ):
108
95
found = True
109
96
int_geom = geom .intersection ( tmpGeom )
110
-
97
+
111
98
if int_geom is None :
112
99
# There was a problem creating the intersection
113
- GEOS_EXCEPT = False
114
- int_geom = QgsGeometry ()
100
+ raise GeoAlgorithmExecutionException ("Geometry exception while computing intersection" )
115
101
else :
116
102
int_geom = QgsGeometry (int_geom )
117
-
103
+
118
104
if diff_geom .intersects ( tmpGeom ):
119
105
diff_geom = diff_geom .difference ( tmpGeom )
120
106
if diff_geom is None :
121
107
# It's possible there was an error here?
122
108
diff_geom = QgsGeometry ()
123
109
else :
124
110
diff_geom = QgsGeometry (diff_geom )
125
-
111
+
126
112
if int_geom .wkbType () == 0 :
127
113
# intersection produced different geomety types
128
114
temp_list = int_geom .asGeometryCollection ()
@@ -131,10 +117,13 @@ def processAlgorithm(self, progress):
131
117
int_geom = QgsGeometry ( i )
132
118
try :
133
119
outFeat .setGeometry ( int_geom )
134
- outFeat .setAttributes ( atMapA .extend ( atMapB ) )
120
+ attrs = []
121
+ attrs .extend (atMapA )
122
+ attrs .extend (atMapB )
123
+ outFeat .setAttributes (attrs )
135
124
writer .addFeature ( outFeat )
136
125
except Exception , err :
137
- FEATURE_EXCEPT = False
126
+ raise GeoAlgorithmExecutionException ( "Feature exception while computing union" )
138
127
else :
139
128
# this only happends if the bounding box
140
129
# intersects, but the geometry doesn't
@@ -144,10 +133,8 @@ def processAlgorithm(self, progress):
144
133
writer .addFeature ( outFeat )
145
134
except :
146
135
# 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
+
151
138
152
139
if found :
153
140
try :
@@ -160,10 +147,9 @@ def processAlgorithm(self, progress):
160
147
outFeat .setAttributes ( atMapA )
161
148
writer .addFeature ( outFeat )
162
149
except Exception , err :
163
- FEATURE_EXCEPT = False
150
+ raise GeoAlgorithmExecutionException ( "Feature exception while computing union" )
164
151
165
- length = len ( vproviderA .fields ().values () )
166
- vproviderB .rewind ()
152
+ length = len (vproviderA .fields ())
167
153
168
154
featuresA = QGisLayers .features (vlayerB )
169
155
nFeat = len (featuresA )
@@ -172,8 +158,9 @@ def processAlgorithm(self, progress):
172
158
add = False
173
159
geom = QgsGeometry ( inFeatA .geometry () )
174
160
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 ) )
177
164
intersects = indexB .intersects ( geom .boundingBox () )
178
165
179
166
if len (intersects ) < 1 :
@@ -182,10 +169,10 @@ def processAlgorithm(self, progress):
182
169
outFeat .setAttributes ( atMap )
183
170
writer .addFeature ( outFeat )
184
171
except Exception , err :
185
- FEATURE_EXCEPT = False
172
+ raise GeoAlgorithmExecutionException ( "Feature exception while computing union" )
186
173
else :
187
174
for id in intersects :
188
- vlayerA .featureAtId ( int ( id ), inFeatB , True , allAttrsA )
175
+ vlayerA .featureAtId ( int ( id ), inFeatB , True )
189
176
atMapB = inFeatB .attributes ()
190
177
tmpGeom = QgsGeometry ( inFeatB .geometry () )
191
178
try :
@@ -199,15 +186,15 @@ def processAlgorithm(self, progress):
199
186
outFeat .setAttributes ( atMap )
200
187
writer .addFeature ( outFeat )
201
188
except Exception , err :
202
- add = False
203
- GEOS_EXCEPT = False
189
+ raise GeoAlgorithmExecutionException ("Geometry exception while computing intersection" )
204
190
205
191
if add :
206
192
try :
207
193
outFeat .setGeometry ( diff_geom )
208
194
outFeat .setAttributes ( atMapB )
209
195
writer .addFeature ( outFeat )
210
- except Exception , err :
196
+ except Exception , err :
197
+ raise err
211
198
FEATURE_EXCEPT = False
212
199
nElement += 1
213
200
@@ -223,4 +210,4 @@ def defineCharacteristics(self):
223
210
self .group = "Vector overlay tools"
224
211
self .addParameter (ParameterVector (Union .INPUT , "Input layer" , ParameterVector .VECTOR_TYPE_ANY ))
225
212
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 " ))
0 commit comments