Skip to content

Commit faa3eb8

Browse files
author
cfarmer
committed
updates to union tool: more checks on GEOS processing; when geometry collection created, take only relevant geometries; should address #3071 (and possibly #3013); also adds ability to run almost all geoprocessing tools on almost all geometry types
git-svn-id: http://svn.osgeo.org/qgis/trunk@14537 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent fb942e6 commit faa3eb8

File tree

1 file changed

+66
-40
lines changed

1 file changed

+66
-40
lines changed

python/plugins/fTools/tools/doGeoprocessing.py

+66-40
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,7 @@ def manageGui( self ):
156156
self.inShapeA.clear()
157157
self.inShapeB.clear()
158158

159-
if self.myFunction == 5 or self.myFunction == 8 or self.myFunction == 3:
160-
myListA = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] )
161-
myListB = ftools_utils.getLayerNames( [ QGis.Polygon ] )
162-
elif self.myFunction == 7 or self.myFunction == 6:
163-
myListA = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] ) # added points and lines to test
164-
myListB = ftools_utils.getLayerNames( [ QGis.Point, QGis.Line, QGis.Polygon ] ) # added points and lines to test
165-
elif self.myFunction == 4:
159+
if self.myFunction == 4:
166160
myListA = ftools_utils.getLayerNames( [ QGis.Polygon ] )
167161
myListB = []
168162
else:
@@ -1053,100 +1047,132 @@ def union( self ):
10531047
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
10541048
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
10551049
vproviderA.rewind()
1050+
count = 0
10561051
while vproviderA.nextFeature( inFeatA ):
10571052
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
10581053
nElement += 1
10591054
found = False
10601055
geom = QgsGeometry( inFeatA.geometry() )
1061-
diffGeom = QgsGeometry( inFeatA.geometry() )
1056+
diff_geom = QgsGeometry( geom )
10621057
atMapA = inFeatA.attributeMap()
10631058
intersects = indexA.intersects( geom.boundingBox() )
1064-
if len( intersects ) <= 0:
1059+
if len( intersects ) < 1:
10651060
try:
10661061
outFeat.setGeometry( geom )
10671062
outFeat.setAttributeMap( atMapA )
10681063
writer.addFeature( outFeat )
10691064
except:
10701065
FEATURE_EXCEPT = False
1071-
continue
1066+
# this really shouldn't happen, as we
1067+
# haven't edited the input geom at all
1068+
# continue
10721069
else:
10731070
for id in intersects:
1071+
count += 1
10741072
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
10751073
atMapB = inFeatB.attributeMap()
10761074
tmpGeom = QgsGeometry( inFeatB.geometry() )
10771075
try:
10781076
if geom.intersects( tmpGeom ):
10791077
found = True
1080-
diff_geom = QgsGeometry( geom.difference( tmpGeom ) )
1081-
int_geom = QgsGeometry( geom.intersection( tmpGeom ) )
1082-
if int_geom.wkbType() == 7:
1083-
int_com = geom.combine( tmpGeom )
1084-
int_sym = geom.symDifference( tmpGeom )
1085-
int_geom = QgsGeometry( int_com.difference( int_sym ) )
1078+
int_geom = geom.intersection( tmpGeom )
1079+
if int_geom is None:
1080+
GEOS_EXCEPT = False
1081+
# There was a problem creating the intersection
1082+
int_geom = QgsGeometry()
1083+
else:
1084+
int_geom = QgsGeometry(int_geom)
1085+
if diff_geom.intersects( tmpGeom ):
1086+
diff_geom = diff_geom.difference( tmpGeom )
1087+
if diff_geom is None:
1088+
# It's possible there was an error here?
1089+
diff_geom = QgsGeometry()
1090+
else:
1091+
diff_geom = QgsGeometry(diff_geom)
1092+
if int_geom.wkbType() == 0:
1093+
# intersection produced different geomety types
1094+
temp_list = int_geom.asGeometryCollection()
1095+
for i in temp_list:
1096+
if i.type() == geom.type():
1097+
int_geom = QgsGeometry( i )
10861098
try:
10871099
outFeat.setGeometry( int_geom )
10881100
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
1101+
# print int_geom.wkbType()
10891102
writer.addFeature( outFeat )
1090-
except:
1091-
FEATURE_EXCEPT = False
1092-
continue
1093-
else:
1094-
try:
1095-
outFeat.setGeometry( geom )
1096-
outFeat.setAttributeMap( atMapA )
1097-
writer.addFeature( outFeat )
1098-
except:
1103+
except Exception, err:
1104+
# print str(err)
10991105
FEATURE_EXCEPT = False
1100-
continue
1101-
except:
1106+
# else:
1107+
# # this only happends if the bounding box
1108+
# # intersects, but the geometry doesn't
1109+
# try:
1110+
# outFeat.setGeometry( geom )
1111+
# outFeat.setAttributeMap( atMapA )
1112+
# print geom.wkbType()
1113+
# writer.addFeature( outFeat )
1114+
# except:
1115+
## # also shoudn't ever happen
1116+
# FEATURE_EXCEPT = False
1117+
# pass
1118+
except Exception, err:
1119+
# print str(err)
11021120
GEOS_EXCEPT = False
11031121
found = False
1104-
continue
11051122
if found:
11061123
try:
1124+
if diff_geom.wkbType() == 0:
1125+
temp_list = diff_geom.asGeometryCollection()
1126+
for i in temp_list:
1127+
if i.type() == geom.type():
1128+
diff_geom = QgsGeometry( i )
11071129
outFeat.setGeometry( diff_geom )
11081130
outFeat.setAttributeMap( atMapA )
1131+
# print diff_geom.wkbType()
11091132
writer.addFeature( outFeat )
1110-
except:
1133+
except Exception, err:
1134+
# print str(err)
11111135
FEATURE_EXCEPT = False
1112-
continue
1136+
# continue
11131137
length = len( vproviderA.fields().values() )
11141138
vproviderB.rewind()
11151139
while vproviderB.nextFeature( inFeatA ):
1116-
add = True
1140+
add = False
11171141
geom = QgsGeometry( inFeatA.geometry() )
11181142
diff_geom = QgsGeometry( geom )
11191143
atMap = inFeatA.attributeMap().values()
11201144
atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
11211145
intersects = indexB.intersects( geom.boundingBox() )
1122-
if len(intersects) <= 0:
1146+
if len(intersects) < 1:
11231147
try:
1124-
outFeat.setGeometry( diff_geom )
1148+
outFeat.setGeometry( geom )
11251149
outFeat.setAttributeMap( atMap )
11261150
writer.addFeature( outFeat )
1127-
except:
1151+
except Exception, err:
1152+
# print str(err)
11281153
FEATURE_EXCEPT = False
1129-
continue
11301154
else:
11311155
for id in intersects:
11321156
vproviderA.featureAtId( int( id ), inFeatB , True, allAttrsA )
11331157
atMapB = inFeatB.attributeMap()
11341158
tmpGeom = QgsGeometry( inFeatB.geometry() )
11351159
try:
11361160
if diff_geom.intersects( tmpGeom ):
1161+
add = True
11371162
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
1138-
except:
1163+
except Exception, err:
1164+
# print str(err)
11391165
add = False
11401166
GEOS_EXCEPT = False
1141-
break
11421167
if add:
11431168
try:
11441169
outFeat.setGeometry( diff_geom )
1145-
outFeat.setAttributeMap( atMap )
1170+
outFeat.setAttributeMap( atMapB )
11461171
writer.addFeature( outFeat )
1147-
except:
1172+
except Exception, err:
1173+
# print str(err)
11481174
FEATURE_EXCEPT = False
1149-
continue
1175+
# continue
11501176
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
11511177
nElement += 1
11521178
del writer

0 commit comments

Comments
 (0)