Skip to content

Commit ed40da8

Browse files
author
cfarmer
committedAug 12, 2009
Additional updates to 'Clip' Geoprocessing tool: Properly fixes #1758.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11348 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

1 file changed

+130
-60
lines changed

1 file changed

+130
-60
lines changed
 

‎python/plugins/fTools/tools/doGeoprocessing.py

Lines changed: 130 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,55 +1213,90 @@ def clip( self ):
12131213
nElement += 1
12141214
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
12151215
geom = QgsGeometry( inFeatA.geometry() )
1216-
diff_geom = QgsGeometry( geom )
1216+
int_geom = QgsGeometry( geom )
12171217
atMap = inFeatA.attributeMap()
12181218
intersects = index.intersects( geom.boundingBox() )
1219+
found = False
1220+
first = True
12191221
for id in intersects:
12201222
if id in selectionB:
12211223
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
12221224
tmpGeom = QgsGeometry( inFeatB.geometry() )
1225+
if tmpGeom.intersects( geom ):
1226+
found = True
1227+
if first:
1228+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
1229+
first = False
1230+
else:
1231+
try:
1232+
cur_geom = QgsGeometry( outFeat.geometry() )
1233+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
1234+
outFeat.setGeometry( QgsGeometry( new_geom ) )
1235+
except:
1236+
GEOS_EXCEPT = False
1237+
break
1238+
if found:
1239+
try:
1240+
cur_geom = QgsGeometry( outFeat.geometry() )
1241+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
1242+
if new_geom.wkbType() == 7:
1243+
int_com = QgsGeometry( geom.combine( cur_geom ) )
1244+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
1245+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
12231246
try:
1224-
if geom.intersects( tmpGeom ):
1225-
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
1247+
outFeat.setGeometry( new_geom )
1248+
outFeat.setAttributeMap( atMap )
1249+
writer.addFeature( outFeat )
12261250
except:
1227-
add = False
1228-
GEOS_EXCEPT = False
1229-
break
1230-
try:
1231-
outGeom = QgsGeometry( geom.difference( diff_geom ) )
1232-
outFeat.setGeometry( outGeom )
1233-
outFeat.setAttributeMap( atMap )
1234-
writer.addFeature( outFeat )
1235-
except:
1236-
GEOS_EXCEPT = False
1237-
break
1251+
FEAT_EXCEPT = False
1252+
continue
1253+
except:
1254+
GEOS_EXCEPT = False
1255+
continue
12381256
# we have no selection in overlay layer
12391257
else:
12401258
for inFeatA in selectionA:
12411259
nElement += 1
12421260
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
12431261
geom = QgsGeometry( inFeatA.geometry() )
1244-
diff_geom = QgsGeometry( geom )
12451262
atMap = inFeatA.attributeMap()
12461263
intersects = index.intersects( geom.boundingBox() )
1264+
found = False
1265+
first = True
12471266
for id in intersects:
12481267
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
12491268
tmpGeom = QgsGeometry( inFeatB.geometry() )
1269+
if tmpGeom.intersects( geom ):
1270+
found = True
1271+
if first:
1272+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
1273+
first = False
1274+
else:
1275+
try:
1276+
cur_geom = QgsGeometry( outFeat.geometry() )
1277+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
1278+
outFeat.setGeometry( QgsGeometry( new_geom ) )
1279+
except:
1280+
GEOS_EXCEPT = False
1281+
break
1282+
if found:
12501283
try:
1251-
if geom.intersects( tmpGeom ):
1252-
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
1284+
cur_geom = QgsGeometry( outFeat.geometry() )
1285+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
1286+
if new_geom.wkbType() == 7:
1287+
int_com = QgsGeometry( geom.combine( cur_geom ) )
1288+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
1289+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
1290+
try:
1291+
outFeat.setGeometry( new_geom )
1292+
outFeat.setAttributeMap( atMap )
1293+
writer.addFeature( outFeat )
1294+
except:
1295+
FEAT_EXCEPT = False
1296+
continue
12531297
except:
1254-
add = False
12551298
GEOS_EXCEPT = False
1256-
break
1257-
try:
1258-
outGeom = QgsGeometry( geom.difference( diff_geom ) )
1259-
outFeat.setGeometry( outGeom )
1260-
outFeat.setAttributeMap( atMap )
1261-
writer.addFeature( outFeat )
1262-
except:
1263-
GEOS_EXCEPT = False
1264-
break
1299+
continue
12651300
# there is no selection in input layer
12661301
else:
12671302
nFeat = vproviderA.featureCount()
@@ -1274,55 +1309,90 @@ def clip( self ):
12741309
nElement += 1
12751310
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
12761311
geom = QgsGeometry( inFeatA.geometry() )
1277-
diff_geom = QgsGeometry( geom )
12781312
atMap = inFeatA.attributeMap()
12791313
intersects = index.intersects( geom.boundingBox() )
1314+
found = False
1315+
first = True
12801316
for id in intersects:
12811317
if id in selectionB:
12821318
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
12831319
tmpGeom = QgsGeometry( inFeatB.geometry() )
1320+
if tmpGeom.intersects( geom ):
1321+
found = True
1322+
if first:
1323+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
1324+
first = False
1325+
else:
1326+
try:
1327+
cur_geom = QgsGeometry( outFeat.geometry() )
1328+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
1329+
outFeat.setGeometry( QgsGeometry( new_geom ) )
1330+
except:
1331+
GEOS_EXCEPT = False
1332+
break
1333+
if found:
1334+
try:
1335+
cur_geom = QgsGeometry( outFeat.geometry() )
1336+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
1337+
if new_geom.wkbType() == 7:
1338+
int_com = QgsGeometry( geom.combine( cur_geom ) )
1339+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
1340+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
12841341
try:
1285-
if geom.intersects( tmpGeom ):
1286-
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
1342+
outFeat.setGeometry( new_geom )
1343+
outFeat.setAttributeMap( atMap )
1344+
writer.addFeature( outFeat )
12871345
except:
1288-
add = False
1289-
GEOS_EXCEPT = False
1290-
break
1291-
try:
1292-
outGeom = QgsGeometry( geom.difference( diff_geom ) )
1293-
outFeat.setGeometry( outGeom )
1294-
outFeat.setAttributeMap( atMap )
1295-
writer.addFeature( outFeat )
1296-
except:
1297-
GEOS_EXCEPT = False
1298-
break
1346+
FEAT_EXCEPT = False
1347+
continue
1348+
except:
1349+
GEOS_EXCEPT = False
1350+
continue
12991351
# we have no selection in overlay layer
13001352
else:
13011353
while vproviderA.nextFeature( inFeatA ):
13021354
nElement += 1
13031355
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
13041356
geom = QgsGeometry( inFeatA.geometry() )
1305-
diff_geom = QgsGeometry( geom )
13061357
atMap = inFeatA.attributeMap()
13071358
intersects = index.intersects( geom.boundingBox() )
1308-
for id in intersects:
1309-
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
1310-
tmpGeom = QgsGeometry( inFeatB.geometry() )
1311-
try:
1312-
if geom.intersects( tmpGeom ):
1313-
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
1314-
except:
1315-
add = False
1316-
GEOS_EXCEPT = False
1317-
break
1318-
try:
1319-
outGeom = QgsGeometry( geom.difference( diff_geom ) )
1320-
outFeat.setGeometry( outGeom )
1321-
outFeat.setAttributeMap( atMap )
1322-
writer.addFeature( outFeat )
1323-
except:
1324-
GEOS_EXCEPT = False
1325-
break
1359+
first = True
1360+
found = False
1361+
if len( intersects ) > 0:
1362+
for id in intersects:
1363+
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
1364+
tmpGeom = QgsGeometry( inFeatB.geometry() )
1365+
if tmpGeom.intersects( geom ):
1366+
found = True
1367+
if first:
1368+
outFeat.setGeometry( QgsGeometry( tmpGeom ) )
1369+
first = False
1370+
else:
1371+
try:
1372+
cur_geom = QgsGeometry( outFeat.geometry() )
1373+
new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) )
1374+
outFeat.setGeometry( QgsGeometry( new_geom ) )
1375+
except:
1376+
GEOS_EXCEPT = False
1377+
break
1378+
if found:
1379+
try:
1380+
cur_geom = QgsGeometry( outFeat.geometry() )
1381+
new_geom = QgsGeometry( geom.intersection( cur_geom ) )
1382+
if new_geom.wkbType() == 7:
1383+
int_com = QgsGeometry( geom.combine( cur_geom ) )
1384+
int_sym = QgsGeometry( geom.symDifference( cur_geom ) )
1385+
new_geom = QgsGeometry( int_com.difference( int_sym ) )
1386+
try:
1387+
outFeat.setGeometry( new_geom )
1388+
outFeat.setAttributeMap( atMap )
1389+
writer.addFeature( outFeat )
1390+
except:
1391+
FEAT_EXCEPT = False
1392+
continue
1393+
except:
1394+
GEOS_EXCEPT = False
1395+
continue
13261396
del writer
13271397
return GEOS_EXCEPT, FEATURE_EXCEPT, crs_match
13281398

0 commit comments

Comments
 (0)
Please sign in to comment.