@@ -1064,33 +1064,40 @@ def union( self ):
1064
1064
vproviderB = self .vlayerB .dataProvider ()
1065
1065
allAttrsB = vproviderB .attributeIndexes ()
1066
1066
vproviderB .select ( allAttrsB )
1067
+
1067
1068
# check for crs compatibility
1068
1069
crsA = vproviderA .crs ()
1069
1070
crsB = vproviderB .crs ()
1070
1071
if not crsA .isValid () or not crsB .isValid ():
1071
1072
crs_match = None
1072
1073
else :
1073
1074
crs_match = crsA == crsB
1075
+
1074
1076
fields = ftools_utils .combineVectorFields ( self .vlayerA , self .vlayerB )
1075
1077
longNames = ftools_utils .checkFieldNameLength ( fields )
1076
1078
if not longNames .isEmpty ():
1077
1079
message = QString ( 'Following field names are longer than 10 characters:\n %1' ).arg ( longNames .join ( '\n ' ) )
1078
1080
return GEOS_EXCEPT , FEATURE_EXCEPT , crs_match , message
1079
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
1080
- fields , vproviderA .geometryType (), vproviderA .crs () )
1081
+
1082
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
1083
+ vproviderA .geometryType (), vproviderA .crs () )
1081
1084
if writer .hasError ():
1082
1085
return GEOS_EXCEPT , FEATURE_EXCEPT , crs_match , writer .errorMessage ()
1086
+
1083
1087
inFeatA = QgsFeature ()
1084
1088
inFeatB = QgsFeature ()
1085
1089
outFeat = QgsFeature ()
1086
1090
indexA = ftools_utils .createIndex ( vproviderB )
1087
1091
indexB = ftools_utils .createIndex ( vproviderA )
1092
+
1088
1093
nFeat = vproviderA .featureCount () * vproviderB .featureCount ()
1089
- nElement = 0
1090
1094
self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
1091
1095
self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
1096
+
1092
1097
vproviderA .rewind ()
1093
1098
count = 0
1099
+ nElement = 0
1100
+
1094
1101
while vproviderA .nextFeature ( inFeatA ):
1095
1102
self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
1096
1103
nElement += 1
@@ -1105,10 +1112,9 @@ def union( self ):
1105
1112
outFeat .setAttributeMap ( atMapA )
1106
1113
writer .addFeature ( outFeat )
1107
1114
except :
1108
- FEATURE_EXCEPT = False
1109
1115
# this really shouldn't happen, as we
1110
1116
# haven't edited the input geom at all
1111
- # continue
1117
+ FEATURE_EXCEPT = False
1112
1118
else :
1113
1119
for id in intersects :
1114
1120
count += 1
@@ -1119,49 +1125,48 @@ def union( self ):
1119
1125
if geom .intersects ( tmpGeom ):
1120
1126
found = True
1121
1127
int_geom = geom .intersection ( tmpGeom )
1128
+
1122
1129
if int_geom is None :
1123
- GEOS_EXCEPT = False
1124
1130
# There was a problem creating the intersection
1131
+ GEOS_EXCEPT = False
1125
1132
int_geom = QgsGeometry ()
1126
1133
else :
1127
1134
int_geom = QgsGeometry (int_geom )
1135
+
1128
1136
if diff_geom .intersects ( tmpGeom ):
1129
1137
diff_geom = diff_geom .difference ( tmpGeom )
1130
1138
if diff_geom is None :
1131
1139
# It's possible there was an error here?
1132
1140
diff_geom = QgsGeometry ()
1133
1141
else :
1134
1142
diff_geom = QgsGeometry (diff_geom )
1143
+
1135
1144
if int_geom .wkbType () == 0 :
1136
- # intersection produced different geomety types
1145
+ # intersection produced different geomety types
1137
1146
temp_list = int_geom .asGeometryCollection ()
1138
1147
for i in temp_list :
1139
1148
if i .type () == geom .type ():
1140
1149
int_geom = QgsGeometry ( i )
1141
1150
try :
1142
1151
outFeat .setGeometry ( int_geom )
1143
1152
outFeat .setAttributeMap ( ftools_utils .combineVectorAttributes ( atMapA , atMapB ) )
1144
- # print int_geom.wkbType()
1145
1153
writer .addFeature ( outFeat )
1146
1154
except Exception , err :
1147
- # print str(err)
1148
1155
FEATURE_EXCEPT = False
1149
- # else:
1150
- # # this only happends if the bounding box
1151
- # # intersects, but the geometry doesn't
1152
- # try:
1153
- # outFeat.setGeometry( geom )
1154
- # outFeat.setAttributeMap( atMapA )
1155
- # print geom.wkbType()
1156
- # writer.addFeature( outFeat )
1157
- # except:
1158
- ## # also shoudn't ever happen
1159
- # FEATURE_EXCEPT = False
1160
- # pass
1156
+ else :
1157
+ # this only happends if the bounding box
1158
+ # intersects, but the geometry doesn't
1159
+ try :
1160
+ outFeat .setGeometry ( geom )
1161
+ outFeat .setAttributeMap ( atMapA )
1162
+ writer .addFeature ( outFeat )
1163
+ except :
1164
+ # also shoudn't ever happen
1165
+ FEATURE_EXCEPT = False
1161
1166
except Exception , err :
1162
- # print str(err)
1163
1167
GEOS_EXCEPT = False
1164
1168
found = False
1169
+
1165
1170
if found :
1166
1171
try :
1167
1172
if diff_geom .wkbType () == 0 :
@@ -1171,51 +1176,57 @@ def union( self ):
1171
1176
diff_geom = QgsGeometry ( i )
1172
1177
outFeat .setGeometry ( diff_geom )
1173
1178
outFeat .setAttributeMap ( atMapA )
1174
- # print diff_geom.wkbType()
1175
1179
writer .addFeature ( outFeat )
1176
1180
except Exception , err :
1177
- # print str(err)
1178
1181
FEATURE_EXCEPT = False
1179
- # continue
1182
+
1180
1183
length = len ( vproviderA .fields ().values () )
1181
1184
vproviderB .rewind ()
1185
+
1182
1186
while vproviderB .nextFeature ( inFeatA ):
1183
1187
add = False
1184
1188
geom = QgsGeometry ( inFeatA .geometry () )
1185
1189
diff_geom = QgsGeometry ( geom )
1186
1190
atMap = inFeatA .attributeMap ().values ()
1187
1191
atMap = dict ( zip ( range ( length , length + len ( atMap ) ), atMap ) )
1188
1192
intersects = indexB .intersects ( geom .boundingBox () )
1193
+
1189
1194
if len (intersects ) < 1 :
1190
1195
try :
1191
1196
outFeat .setGeometry ( geom )
1192
1197
outFeat .setAttributeMap ( atMap )
1193
1198
writer .addFeature ( outFeat )
1194
1199
except Exception , err :
1195
- # print str(err)
1196
1200
FEATURE_EXCEPT = False
1197
1201
else :
1198
1202
for id in intersects :
1199
1203
vproviderA .featureAtId ( int ( id ), inFeatB , True , allAttrsA )
1200
1204
atMapB = inFeatB .attributeMap ()
1201
1205
tmpGeom = QgsGeometry ( inFeatB .geometry () )
1206
+
1202
1207
try :
1203
1208
if diff_geom .intersects ( tmpGeom ):
1204
1209
add = True
1205
1210
diff_geom = QgsGeometry ( diff_geom .difference ( tmpGeom ) )
1211
+ else :
1212
+ # this only happends if the bounding box
1213
+ # intersects, but the geometry doesn't
1214
+ outFeat .setGeometry ( diff_geom )
1215
+ outFeat .setAttributeMap ( atMap )
1216
+ print geom .wkbType ()
1217
+ writer .addFeature ( outFeat )
1206
1218
except Exception , err :
1207
- # print str(err)
1208
1219
add = False
1209
1220
GEOS_EXCEPT = False
1221
+
1210
1222
if add :
1211
1223
try :
1212
1224
outFeat .setGeometry ( diff_geom )
1213
1225
outFeat .setAttributeMap ( atMapB )
1214
1226
writer .addFeature ( outFeat )
1215
1227
except Exception , err :
1216
- # print str(err)
1217
1228
FEATURE_EXCEPT = False
1218
- # continue
1229
+
1219
1230
self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
1220
1231
nElement += 1
1221
1232
del writer
0 commit comments