Skip to content

Commit 7cd2850

Browse files
committed
[BACKPORT] fix holes in Union output (fix #3581)
1 parent 09f1677 commit 7cd2850

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

python/plugins/fTools/tools/doGeoprocessing.py

+40-29
Original file line numberDiff line numberDiff line change
@@ -1064,33 +1064,40 @@ def union( self ):
10641064
vproviderB = self.vlayerB.dataProvider()
10651065
allAttrsB = vproviderB.attributeIndexes()
10661066
vproviderB.select( allAttrsB )
1067+
10671068
# check for crs compatibility
10681069
crsA = vproviderA.crs()
10691070
crsB = vproviderB.crs()
10701071
if not crsA.isValid() or not crsB.isValid():
10711072
crs_match = None
10721073
else:
10731074
crs_match = crsA == crsB
1075+
10741076
fields = ftools_utils.combineVectorFields( self.vlayerA, self.vlayerB )
10751077
longNames = ftools_utils.checkFieldNameLength( fields )
10761078
if not longNames.isEmpty():
10771079
message = QString( 'Following field names are longer than 10 characters:\n%1' ).arg( longNames.join( '\n' ) )
10781080
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() )
10811084
if writer.hasError():
10821085
return GEOS_EXCEPT, FEATURE_EXCEPT, crs_match, writer.errorMessage()
1086+
10831087
inFeatA = QgsFeature()
10841088
inFeatB = QgsFeature()
10851089
outFeat = QgsFeature()
10861090
indexA = ftools_utils.createIndex( vproviderB )
10871091
indexB = ftools_utils.createIndex( vproviderA )
1092+
10881093
nFeat = vproviderA.featureCount() * vproviderB.featureCount()
1089-
nElement = 0
10901094
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
10911095
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
1096+
10921097
vproviderA.rewind()
10931098
count = 0
1099+
nElement = 0
1100+
10941101
while vproviderA.nextFeature( inFeatA ):
10951102
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
10961103
nElement += 1
@@ -1105,10 +1112,9 @@ def union( self ):
11051112
outFeat.setAttributeMap( atMapA )
11061113
writer.addFeature( outFeat )
11071114
except:
1108-
FEATURE_EXCEPT = False
11091115
# this really shouldn't happen, as we
11101116
# haven't edited the input geom at all
1111-
# continue
1117+
FEATURE_EXCEPT = False
11121118
else:
11131119
for id in intersects:
11141120
count += 1
@@ -1119,49 +1125,48 @@ def union( self ):
11191125
if geom.intersects( tmpGeom ):
11201126
found = True
11211127
int_geom = geom.intersection( tmpGeom )
1128+
11221129
if int_geom is None:
1123-
GEOS_EXCEPT = False
11241130
# There was a problem creating the intersection
1131+
GEOS_EXCEPT = False
11251132
int_geom = QgsGeometry()
11261133
else:
11271134
int_geom = QgsGeometry(int_geom)
1135+
11281136
if diff_geom.intersects( tmpGeom ):
11291137
diff_geom = diff_geom.difference( tmpGeom )
11301138
if diff_geom is None:
11311139
# It's possible there was an error here?
11321140
diff_geom = QgsGeometry()
11331141
else:
11341142
diff_geom = QgsGeometry(diff_geom)
1143+
11351144
if int_geom.wkbType() == 0:
1136-
# intersection produced different geomety types
1145+
# intersection produced different geomety types
11371146
temp_list = int_geom.asGeometryCollection()
11381147
for i in temp_list:
11391148
if i.type() == geom.type():
11401149
int_geom = QgsGeometry( i )
11411150
try:
11421151
outFeat.setGeometry( int_geom )
11431152
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
1144-
# print int_geom.wkbType()
11451153
writer.addFeature( outFeat )
11461154
except Exception, err:
1147-
# print str(err)
11481155
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
11611166
except Exception, err:
1162-
# print str(err)
11631167
GEOS_EXCEPT = False
11641168
found = False
1169+
11651170
if found:
11661171
try:
11671172
if diff_geom.wkbType() == 0:
@@ -1171,51 +1176,57 @@ def union( self ):
11711176
diff_geom = QgsGeometry( i )
11721177
outFeat.setGeometry( diff_geom )
11731178
outFeat.setAttributeMap( atMapA )
1174-
# print diff_geom.wkbType()
11751179
writer.addFeature( outFeat )
11761180
except Exception, err:
1177-
# print str(err)
11781181
FEATURE_EXCEPT = False
1179-
# continue
1182+
11801183
length = len( vproviderA.fields().values() )
11811184
vproviderB.rewind()
1185+
11821186
while vproviderB.nextFeature( inFeatA ):
11831187
add = False
11841188
geom = QgsGeometry( inFeatA.geometry() )
11851189
diff_geom = QgsGeometry( geom )
11861190
atMap = inFeatA.attributeMap().values()
11871191
atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
11881192
intersects = indexB.intersects( geom.boundingBox() )
1193+
11891194
if len(intersects) < 1:
11901195
try:
11911196
outFeat.setGeometry( geom )
11921197
outFeat.setAttributeMap( atMap )
11931198
writer.addFeature( outFeat )
11941199
except Exception, err:
1195-
# print str(err)
11961200
FEATURE_EXCEPT = False
11971201
else:
11981202
for id in intersects:
11991203
vproviderA.featureAtId( int( id ), inFeatB , True, allAttrsA )
12001204
atMapB = inFeatB.attributeMap()
12011205
tmpGeom = QgsGeometry( inFeatB.geometry() )
1206+
12021207
try:
12031208
if diff_geom.intersects( tmpGeom ):
12041209
add = True
12051210
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 )
12061218
except Exception, err:
1207-
# print str(err)
12081219
add = False
12091220
GEOS_EXCEPT = False
1221+
12101222
if add:
12111223
try:
12121224
outFeat.setGeometry( diff_geom )
12131225
outFeat.setAttributeMap( atMapB )
12141226
writer.addFeature( outFeat )
12151227
except Exception, err:
1216-
# print str(err)
12171228
FEATURE_EXCEPT = False
1218-
# continue
1229+
12191230
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
12201231
nElement += 1
12211232
del writer

0 commit comments

Comments
 (0)