Skip to content

Commit 059fd2c

Browse files
committed
Fix #11724 - do to not add extra geometries in union (ftools + processing)
1 parent 1eaec17 commit 059fd2c

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

python/plugins/fTools/tools/doGeoprocessing.py

+11-25
Original file line numberDiff line numberDiff line change
@@ -1123,9 +1123,8 @@ def union( self ):
11231123
while fitA.nextFeature( inFeatA ):
11241124
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
11251125
nElement += 1
1126-
found = False
1126+
lstIntersectingB = []
11271127
geom = QgsGeometry( inFeatA.geometry() )
1128-
diff_geom = QgsGeometry( geom )
11291128
atMapA = inFeatA.attributes()
11301129
intersects = indexA.intersects( geom.boundingBox() )
11311130
if len( intersects ) < 1:
@@ -1145,8 +1144,8 @@ def union( self ):
11451144
tmpGeom = QgsGeometry( inFeatB.geometry() )
11461145
try:
11471146
if geom.intersects( tmpGeom ):
1148-
found = True
11491147
int_geom = geom.intersection( tmpGeom )
1148+
lstIntersectingB.append(tmpGeom)
11501149

11511150
if int_geom is None:
11521151
# There was a problem creating the intersection
@@ -1155,14 +1154,6 @@ def union( self ):
11551154
else:
11561155
int_geom = QgsGeometry(int_geom)
11571156

1158-
if diff_geom.intersects( tmpGeom ):
1159-
diff_geom = diff_geom.difference( tmpGeom )
1160-
if diff_geom is None:
1161-
# It's possible there was an error here?
1162-
diff_geom = QgsGeometry()
1163-
else:
1164-
diff_geom = QgsGeometry(diff_geom)
1165-
11661157
if int_geom.wkbType() == 0:
11671158
# intersection produced different geometry types
11681159
temp_list = int_geom.asGeometryCollection()
@@ -1188,22 +1179,17 @@ def union( self ):
11881179
writer.addFeature( outFeat )
11891180
except Exception, err:
11901181
FEATURE_EXCEPT = False
1191-
else:
1192-
# this only happends if the bounding box
1193-
# intersects, but the geometry doesn't
1194-
try:
1195-
outFeat.setGeometry( geom )
1196-
outFeat.setAttributes( atMapA )
1197-
writer.addFeature( outFeat )
1198-
except:
1199-
# also shoudn't ever happen
1200-
FEATURE_EXCEPT = False
12011182
except Exception, err:
12021183
GEOS_EXCEPT = False
1203-
found = False
12041184

1205-
if found:
1206-
try:
1185+
try:
1186+
# the remaining bit of inFeatA's geometry
1187+
# if there is nothing left, this will just silently fail and we're good
1188+
diff_geom = QgsGeometry( geom )
1189+
if len(lstIntersectingB) != 0:
1190+
intB = QgsGeometry.unaryUnion(lstIntersectingB)
1191+
diff_geom = diff_geom.difference(intB)
1192+
12071193
if diff_geom.wkbType() == 0:
12081194
temp_list = diff_geom.asGeometryCollection()
12091195
for i in temp_list:
@@ -1212,7 +1198,7 @@ def union( self ):
12121198
outFeat.setGeometry( diff_geom )
12131199
outFeat.setAttributes( atMapA )
12141200
writer.addFeature( outFeat )
1215-
except Exception, err:
1201+
except Exception, err:
12161202
FEATURE_EXCEPT = False
12171203

12181204
length = len( vproviderA.fields() )

python/plugins/processing/algs/qgis/Union.py

+11-25
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ def processAlgorithm(self, progress):
6565
for inFeatA in featuresA:
6666
progress.setPercentage(nElement / float(nFeat) * 50)
6767
nElement += 1
68-
found = False
68+
lstIntersectingB = []
6969
geom = QgsGeometry(inFeatA.geometry())
70-
diff_geom = QgsGeometry(geom)
7170
atMapA = inFeatA.attributes()
7271
intersects = indexA.intersects(geom.boundingBox())
7372
if len(intersects) < 1:
@@ -89,8 +88,8 @@ def processAlgorithm(self, progress):
8988
tmpGeom = QgsGeometry(inFeatB.geometry())
9089

9190
if geom.intersects(tmpGeom):
92-
found = True
9391
int_geom = geom.intersection(tmpGeom)
92+
lstIntersectingB.append(tmpGeom)
9493

9594
if int_geom is None:
9695
# There was a problem creating the intersection
@@ -100,14 +99,6 @@ def processAlgorithm(self, progress):
10099
else:
101100
int_geom = QgsGeometry(int_geom)
102101

103-
if diff_geom.intersects(tmpGeom):
104-
diff_geom = diff_geom.difference(tmpGeom)
105-
if diff_geom is None:
106-
# It's possible there was an error here?
107-
diff_geom = QgsGeometry()
108-
else:
109-
diff_geom = QgsGeometry(diff_geom)
110-
111102
if int_geom.wkbType() == 0:
112103
# Intersection produced different geomety types
113104
temp_list = int_geom.asGeometryCollection()
@@ -124,20 +115,15 @@ def processAlgorithm(self, progress):
124115
except Exception, err:
125116
raise GeoAlgorithmExecutionException(
126117
self.tr('Feature exception while computing union'))
127-
else:
128-
# This only happends if the bounding box intersects,
129-
# but the geometry doesn't
130-
try:
131-
outFeat.setGeometry(geom)
132-
outFeat.setAttributes(atMapA)
133-
writer.addFeature(outFeat)
134-
except:
135-
# Also shoudn't ever happen
136-
raise GeoAlgorithmExecutionException(
137-
self.tr('Feature exception while computing union'))
138118

139-
if found:
140-
try:
119+
try:
120+
# the remaining bit of inFeatA's geometry
121+
# if there is nothing left, this will just silently fail and we're good
122+
diff_geom = QgsGeometry( geom )
123+
if len(lstIntersectingB) != 0:
124+
intB = QgsGeometry.unaryUnion(lstIntersectingB)
125+
diff_geom = diff_geom.difference(intB)
126+
141127
if diff_geom.wkbType() == 0:
142128
temp_list = diff_geom.asGeometryCollection()
143129
for i in temp_list:
@@ -146,7 +132,7 @@ def processAlgorithm(self, progress):
146132
outFeat.setGeometry(diff_geom)
147133
outFeat.setAttributes(atMapA)
148134
writer.addFeature(outFeat)
149-
except Exception, err:
135+
except Exception, err:
150136
raise GeoAlgorithmExecutionException(
151137
self.tr('Feature exception while computing union'))
152138

0 commit comments

Comments
 (0)