Skip to content

Commit 5888597

Browse files
author
cfarmer
committedFeb 8, 2009
1) geoprocessing tools better handle geos exceptions
2) fix osx bug regarding about dialog 3) update version information git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10127 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

4 files changed

+215
-141
lines changed

4 files changed

+215
-141
lines changed
 

‎python/plugins/fTools/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def description():
2121
return "Tools for vector data analysis and management"
2222

2323
def version():
24-
return "0.5.5"
24+
return "0.5.6"
2525

2626
def qgisMinimumVersion():
2727
return "1.0.0"
2828

2929
def authorName():
30-
return "Carson Farmer"
30+
return "Carson J. Q. Farmer"
3131

3232
def classFactory( iface ):
3333
from fTools import fToolsPlugin

‎python/plugins/fTools/doAbout.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self, iface):
3232
QObject.connect(self.btnWeb, SIGNAL("clicked()"), self.openWeb)
3333
QObject.connect(self.btnHelp, SIGNAL("clicked()"), self.openHelp)
3434
self.fToolsLogo.setPixmap(QPixmap(":/icons/default/ftools_logo.png"))
35-
self.label_3.setText("fTools 0.5.2")
35+
self.label_3.setText("fTools 0.5.6")
3636
self.textEdit.setText(self.getText())
3737

3838
def getText(self):
@@ -71,6 +71,8 @@ def getText(self):
7171
aknowledgeString.append("Paolo Cavallini\n")
7272
aknowledgeString.append("Aaron Racicot\n")
7373
aknowledgeString.append("Colin Robertson\n")
74+
aknowledgeString.append("Agustin Lobo\n")
75+
aknowledgeString.append("Jurgen E. Fischer\n")
7476
aknowledgeString.append("QGis developer and user communities\n")
7577
aknowledgeString.append("Folks on #qgis at freenode.net\n")
7678
aknowledgeString.append("All those who have reported bugs/fixes/suggestions/comments/etc.")

‎python/plugins/fTools/fTools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def initGui( self ):
170170
QCoreApplication.translate( "fTools", "Split vector layer" ), self.iface.mainWindow() )
171171
self.dataManageMenu.addActions( [ self.project, self.define, self.joinAttr, self.spatJoin, self.splitVect ] )
172172

173-
self.about_ftools = QAction( QIcon( self.getThemeIcon( "ftools_logo.png" ) ),
173+
self.ftools_about = QAction( QIcon( self.getThemeIcon( "ftools_logo.png" ) ),
174174
QCoreApplication.translate( "fTools", "About fTools" ), self.iface.mainWindow() )
175175

176176
self.menu.addMenu( self.analysisMenu )
@@ -179,7 +179,7 @@ def initGui( self ):
179179
self.menu.addMenu( self.conversionMenu )
180180
self.menu.addMenu( self.dataManageMenu )
181181
self.menu.addSeparator()
182-
self.menu.addAction( self.about_ftools )
182+
self.menu.addAction( self.ftools_about )
183183

184184
menuBar = self.iface.mainWindow().menuBar()
185185
actions = menuBar.actions()
@@ -226,7 +226,7 @@ def initGui( self ):
226226
QObject.connect( self.spatJoin, SIGNAL("triggered()"), self.dospatJoin )
227227
QObject.connect( self.splitVect, SIGNAL("triggered()"), self.dosplitVect )
228228

229-
QObject.connect( self.about_ftools, SIGNAL("triggered()"), self.doabout )
229+
QObject.connect( self.ftools_about, SIGNAL("triggered()"), self.doabout )
230230

231231
def unload( self ):
232232
pass

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

Lines changed: 207 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from qgis.gui import *
55
from frmGeoprocessing import Ui_Dialog
66
import ftools_utils
7+
import sys
78

89
class GeoprocessingDialog( QDialog, Ui_Dialog ):
910
def __init__( self, iface, function ):
@@ -161,19 +162,24 @@ def runFinishedFromThread( self, results ):
161162
self.testThread.stop()
162163
self.cancel_close.setText( "Close" )
163164
QObject.disconnect( self.cancel_close, SIGNAL( "clicked()" ), self.cancelThread )
164-
if results[0]:
165-
if not results[1]:
166-
out_text = self.tr( "\n\nWarning: Different input coordinate reference systems detected, results may not be as expected.")
167-
else:
168-
out_text = ""
169-
addToTOC = QMessageBox.question( self, "Geoprocessing", self.tr( "Created output shapefile:" ) + "\n" +
170-
unicode( self.shapefileName ) + "\n\n" + self.tr( "Would you like to add the new layer to the TOC?" )
171-
+ out_text, QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton )
172-
if addToTOC == QMessageBox.Yes:
173-
ftools_utils.addShapeToCanvas( unicode( self.shapefileName ) )
165+
if not results[2] or not results[1] or not results [0]:
166+
out_text = self.tr( "\nWarnings:" )
167+
end_text = self.tr( "\nSome output geometries may be missing or invalid.\n\nWould you like to add the new layer anyway?" )
174168
else:
175-
QMessageBox.warning( self, "Geoprocessing", self.tr( "GEOS geoprocessing error, please check that all features have a valid geometry" ) )
176-
QObject.disconnect( self.cancel_close, SIGNAL( "clicked()" ), self.cancelThread )
169+
out_text = "\n"
170+
end_text = self.tr( "\n\nWould you like to add the new layer to the TOC?" )
171+
if not results[2]:
172+
out_text = out_text + self.tr( "\nInput CRS error: Different input coordinate reference systems detected, results may not be as expected.")
173+
if not results[1]:
174+
out_text = out_text + self.tr( "\nFeature geometry error: One or more output features ignored due to invalid geometry.")
175+
if not results[0]:
176+
out_text = out_text + self.tr( "\nGEOS geoprocessing error: One or more input features have invalid geometry.")
177+
else:
178+
out_text = ""
179+
addToTOC = QMessageBox.question( self, "Geoprocessing", self.tr( "Created output shapefile:" ) + "\n"
180+
+ unicode( self.shapefileName ) + out_text + end_text, QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton )
181+
if addToTOC == QMessageBox.Yes:
182+
ftools_utils.addShapeToCanvas( unicode( self.shapefileName ) )
177183

178184
def runStatusFromThread( self, status ):
179185
self.progressBar.setValue( status )
@@ -202,31 +208,32 @@ def run( self ):
202208
( self.myParam, useField ) = self.checkParameter( self.vlayerA, self.myParam )
203209
if not self.myParam is None:
204210
if self.myFunction == 1:
205-
success, match = self.buffering( useField )
211+
geos, feature, match = self.buffering( useField )
206212
elif self.myFunction == 2:
207-
success, match = self.convex_hull( useField )
213+
geos, feature, match = self.convex_hull( useField )
208214
elif self.myFunction == 4:
209-
success, match = self.dissolve( useField )
215+
geos, feature, match = self.dissolve( useField )
210216
else:
211217
self.vlayerB = ftools_utils.getVectorLayerByName( self.myLayerB )
212218
if self.myFunction == 3:
213-
success, match = self.difference()
219+
geos, feature, match = self.difference()
214220
elif self.myFunction == 5:
215-
success, match = self.intersect()
221+
geos, feature, match = self.intersect()
216222
elif self.myFunction == 6:
217-
success, match = self.union()
223+
geos, feature, match = self.union()
218224
elif self.myFunction == 7:
219-
success, match = self.symetrical_difference()
225+
geos, feature, match = self.symetrical_difference()
220226
elif self.myFunction == 8:
221-
success, match = self.clip()
222-
self.emit( SIGNAL( "runFinished(PyQt_PyObject)" ), (success, match) )
227+
geos, feature, match = self.clip()
228+
self.emit( SIGNAL( "runFinished(PyQt_PyObject)" ), (geos, feature, match) )
223229
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
224230

225231
def stop(self):
226232
self.running = False
227233

228234
def buffering( self, useField ):
229235
GEOS_EXCEPT = True
236+
FEATURE_EXCEPT = True
230237
vproviderA = self.vlayerA.dataProvider()
231238
allAttrs = vproviderA.attributeIndexes()
232239
vproviderA.select( allAttrs )
@@ -244,9 +251,8 @@ def buffering( self, useField ):
244251
if self.myMerge:
245252
first = True
246253
vproviderA.rewind()
254+
add = True
247255
while vproviderA.nextFeature( inFeat ):
248-
nElement += 1
249-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
250256
atMap = inFeat.attributeMap()
251257
if useField:
252258
value = atMap[ self.myParam ].toDouble()[ 0 ]
@@ -255,27 +261,28 @@ def buffering( self, useField ):
255261
inGeom = QgsGeometry( inFeat.geometry() )
256262
try:
257263
outGeom = inGeom.buffer( float( value ), 5 )
264+
if first:
265+
tempGeom = QgsGeometry( outGeom )
266+
first = False
267+
else:
268+
try:
269+
tempGeom = tempGeom.combine( outGeom )
270+
except:
271+
GEOS_EXCEPT = False
272+
continue
258273
except:
259-
outGeom = QgsGeometry()
260274
GEOS_EXCEPT = False
261-
break
262-
if first:
263-
tempGeom = QgsGeometry( outGeom )
264-
first = False
265-
else:
266-
try:
267-
tempGeom = tempGeom.combine( outGeom )
268-
except:
269-
tempGeom = QgsGeometry()
270-
GEOS_EXCEPT = False
271-
break
272-
outFeat.setGeometry( tempGeom )
273-
writer.addFeature( outFeat )
275+
continue
276+
nElement += 1
277+
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
278+
try:
279+
outFeat.setGeometry( tempGeom )
280+
writer.addFeature( outFeat )
281+
except:
282+
FEATURE_EXCEPT = False
274283
else:
275284
vproviderA.rewind()
276285
while vproviderA.nextFeature( inFeat ):
277-
nElement += 1
278-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
279286
atMap = inFeat.attributeMap()
280287
if useField:
281288
value = atMap[ self.myParam ].toDouble()[ 0 ]
@@ -284,18 +291,24 @@ def buffering( self, useField ):
284291
inGeom = QgsGeometry( inFeat.geometry() )
285292
try:
286293
outGeom = inGeom.buffer( float( value ), 5 )
294+
try:
295+
outFeat.setGeometry( outGeom )
296+
outFeat.setAttributeMap( atMap )
297+
writer.addFeature( outFeat )
298+
except:
299+
FEATURE_EXCEPT = False
300+
continue
287301
except:
288-
outGeom = QgsGeometry()
289302
GEOS_EXCEPT = False
290303
continue
291-
outFeat.setGeometry( outGeom )
292-
outFeat.setAttributeMap( atMap )
293-
writer.addFeature( outFeat )
304+
nElement += 1
305+
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
294306
del writer
295-
return GEOS_EXCEPT, True
307+
return GEOS_EXCEPT, FEATURE_EXCEPT, True
296308

297309
def convex_hull(self, useField ):
298310
GEOS_EXCEPT = True
311+
FEATURE_EXCEPT = True
299312
vproviderA = self.vlayerA.dataProvider()
300313
allAttrsA = vproviderA.attributeIndexes()
301314
fields = vproviderA.fields()
@@ -318,8 +331,6 @@ def convex_hull(self, useField ):
318331
vproviderA.select( allAttrsA )
319332
vproviderA.rewind()
320333
while vproviderA.nextFeature( inFeat ):
321-
nElement += 1
322-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
323334
atMap = inFeat.attributeMap()
324335
idVar = atMap[ self.myParam ]
325336
if idVar.toString().trimmed() == i.toString().trimmed():
@@ -329,19 +340,21 @@ def convex_hull(self, useField ):
329340
inGeom = QgsGeometry( inFeat.geometry() )
330341
points = ftools_utils.extractPoints( inGeom )
331342
hull.extend( points )
343+
nElement += 1
344+
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
332345
if len( hull ) >= 3:
333346
tmpGeom = QgsGeometry( outGeom.fromMultiPoint( hull ) )
334347
try:
335348
outGeom = tmpGeom.convexHull()
349+
outFeat.setGeometry( outGeom )
350+
(area, perim) = self.simpleMeasure( outGeom )
351+
outFeat.addAttribute( 0, QVariant( outID ) )
352+
outFeat.addAttribute( 1, QVariant( area ) )
353+
outFeat.addAttribute( 2, QVariant( perim ) )
354+
writer.addFeature( outFeat )
336355
except:
337-
outGeom = QgsGeometry()
338356
GEOS_EXCEPT = False
339-
outFeat.setGeometry( outGeom )
340-
(area, perim) = self.simpleMeasure( outGeom )
341-
outFeat.addAttribute( 0, QVariant( outID ) )
342-
outFeat.addAttribute( 1, QVariant( area ) )
343-
outFeat.addAttribute( 2, QVariant( perim ) )
344-
writer.addFeature( outFeat )
357+
continue
345358
else:
346359
hull = []
347360
vproviderA.rewind()
@@ -350,24 +363,24 @@ def convex_hull(self, useField ):
350363
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
351364
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
352365
while vproviderA.nextFeature( inFeat ):
353-
nElement += 1
354-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
355366
inGeom = QgsGeometry( inFeat.geometry() )
356367
points = ftools_utils.extractPoints( inGeom )
357368
hull.extend( points )
369+
nElement += 1
370+
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
358371
tmpGeom = QgsGeometry( outGeom.fromMultiPoint( hull ) )
359372
try:
360373
outGeom = tmpGeom.convexHull()
374+
outFeat.setGeometry( outGeom )
375+
writer.addFeature( outFeat )
361376
except:
362-
outGeom = QgsGeometry()
363377
GEOS_EXCEPT = False
364-
outFeat.setGeometry( outGeom )
365-
writer.addFeature( outFeat )
366378
del writer
367-
return GEOS_EXCEPT, True
379+
return GEOS_EXCEPT, FEATURE_EXCEPT, True
368380

369381
def dissolve( self, useField ):
370382
GEOS_EXCEPT = True
383+
FEATURE_EXCEPT = True
371384
vproviderA = self.vlayerA.dataProvider()
372385
allAttrsA = vproviderA.attributeIndexes()
373386
fields = vproviderA.fields()
@@ -382,6 +395,7 @@ def dissolve( self, useField ):
382395
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0)
383396
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
384397
first = True
398+
add = True
385399
while vproviderA.nextFeature( inFeat ):
386400
nElement += 1
387401
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
@@ -394,11 +408,11 @@ def dissolve( self, useField ):
394408
tmpInGeom = QgsGeometry( inFeat.geometry() )
395409
tmpOutGeom = QgsGeometry( outFeat.geometry() )
396410
try:
397-
tmpOutGeom = tmpOutGeom.combine( tmpInGeom )
411+
tmpOutGeom = QgsGeometry( tmpOutGeom.combine( tmpInGeom ) )
412+
outFeat.setGeometry( tmpOutGeom )
398413
except:
399-
tmpOutGeom = QgsGeometry()
400414
GEOS_EXCEPT = False
401-
outFeat.setGeometry( tmpOutGeom )
415+
continue
402416
outFeat.setAttributeMap( attrs )
403417
writer.addFeature( outFeat )
404418
else:
@@ -408,6 +422,7 @@ def dissolve( self, useField ):
408422
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
409423
for item in unique:
410424
first = True
425+
add = True
411426
vproviderA.select( allAttrsA )
412427
vproviderA.rewind()
413428
while vproviderA.nextFeature( inFeat ):
@@ -426,18 +441,20 @@ def dissolve( self, useField ):
426441
tmpInGeom = QgsGeometry( inFeat.geometry() )
427442
tmpOutGeom = QgsGeometry( outFeat.geometry() )
428443
try:
429-
tmpOutGeom = tmpOutGeom.combine( tmpInGeom )
444+
tmpOutGeom = QgsGeometry( tmpOutGeom.combine( tmpInGeom ) )
445+
outFeat.setGeometry( tmpOutGeom )
430446
except:
431-
tmpOutGeom = QgsGeometry()
432447
GEOS_EXCEPT = False
433-
outFeat.setGeometry( tmpOutGeom )
434-
outFeat.setAttributeMap( attrs )
435-
writer.addFeature( outFeat )
448+
add = False
449+
if add:
450+
outFeat.setAttributeMap( attrs )
451+
writer.addFeature( outFeat )
436452
del writer
437-
return GEOS_EXCEPT, True
453+
return GEOS_EXCEPT, FEATURE_EXCEPT, True
438454

439455
def difference( self ):
440456
GEOS_EXCEPT = True
457+
FEATURE_EXCEPT = True
441458
vproviderA = self.vlayerA.dataProvider()
442459
allAttrsA = vproviderA.attributeIndexes()
443460
vproviderA.select( allAttrsA )
@@ -460,28 +477,36 @@ def difference( self ):
460477
vproviderA.rewind()
461478
while vproviderA.nextFeature( inFeatA ):
462479
nElement += 1
480+
add = True
463481
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
464482
geom = QgsGeometry( inFeatA.geometry() )
483+
diff_geom = QgsGeometry( geom )
465484
atMap = inFeatA.attributeMap()
466485
intersects = index.intersects( geom.boundingBox() )
467486
for id in intersects:
468487
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
469488
tmpGeom = QgsGeometry( inFeatB.geometry() )
470489
try:
471-
if geom.intersects( tmpGeom ):
472-
geom = geom.difference( tmpGeom )
490+
if diff_geom.intersects( tmpGeom ):
491+
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
473492
except:
474-
geom = QgsGeometry()
475493
GEOS_EXCEPT = False
494+
add = False
476495
break
477-
outFeat.setGeometry( geom )
478-
outFeat.setAttributeMap( atMap )
479-
writer.addFeature( outFeat )
496+
if add:
497+
try:
498+
outFeat.setGeometry( diff_geom )
499+
outFeat.setAttributeMap( atMap )
500+
writer.addFeature( outFeat )
501+
except:
502+
FEATURE_EXCEPT = False
503+
continue
480504
del writer
481-
return GEOS_EXCEPT, crs_match
505+
return GEOS_EXCEPT, FEATURE_EXCEPT, crs_match
482506

483507
def intersect( self ):
484508
GEOS_EXCEPT = True
509+
FEATURE_EXCEPT = True
485510
vproviderA = self.vlayerA.dataProvider()
486511
allAttrsA = vproviderA.attributeIndexes()
487512
vproviderA.select( allAttrsA )
@@ -514,22 +539,27 @@ def intersect( self ):
514539
try:
515540
if geom.intersects( tmpGeom ):
516541
atMapB = inFeatB.attributeMap()
517-
int_geom = geom.intersection( tmpGeom )
542+
int_geom = QgsGeometry( geom.intersection( tmpGeom ) )
518543
if int_geom.wkbType() == 7:
519544
int_com = geom.combine( tmpGeom )
520545
int_sym = geom.symDifference( tmpGeom )
521-
int_geom = int_com.difference( int_sym )
522-
outFeat.setGeometry( int_geom )
523-
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
524-
writer.addFeature( outFeat )
546+
int_geom = QgsGeometry( int_com.difference( int_sym ) )
547+
try:
548+
outFeat.setGeometry( int_geom )
549+
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
550+
writer.addFeature( outFeat )
551+
except:
552+
FEATURE_EXCEPT = False
553+
continue
525554
except:
526555
GEOS_EXCEPT = False
527-
continue
556+
break
528557
del writer
529-
return True, crs_match
558+
return GEOS_EXCEPT, FEATURE_EXCEPT, crs_match
530559

531560
def union( self ):
532561
GEOS_EXCEPT = True
562+
FEATURE_EXCEPT = True
533563
vproviderA = self.vlayerA.dataProvider()
534564
allAttrsA = vproviderA.attributeIndexes()
535565
vproviderA.select( allAttrsA )
@@ -552,76 +582,99 @@ def union( self ):
552582
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
553583
vproviderA.rewind()
554584
while vproviderA.nextFeature( inFeatA ):
555-
nElement += 1
556585
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
586+
nElement += 1
557587
found = False
558588
geom = QgsGeometry( inFeatA.geometry() )
559589
diffGeom = QgsGeometry( inFeatA.geometry() )
560590
atMapA = inFeatA.attributeMap()
561591
intersects = indexA.intersects( geom.boundingBox() )
562592
if len( intersects ) <= 0:
563-
outFeat.setGeometry( geom )
564-
outFeat.setAttributeMap( atMapA )
565-
writer.addFeature( outFeat )
593+
try:
594+
outFeat.setGeometry( geom )
595+
outFeat.setAttributeMap( atMapA )
596+
writer.addFeature( outFeat )
597+
except:
598+
FEATURE_EXCEPT = False
599+
continue
566600
else:
567601
for id in intersects:
568602
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
569603
atMapB = inFeatB.attributeMap()
570604
tmpGeom = QgsGeometry( inFeatB.geometry() )
571-
if geom.intersects( tmpGeom ):
572-
found = True
573-
try:
574-
diff_geom = diff_geom.difference( tmpGeom )
575-
int_geom = geom.intersection( tmpGeom )
605+
try:
606+
if geom.intersects( tmpGeom ):
607+
found = True
608+
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
609+
int_geom = QgsGeometry( geom.intersection( tmpGeom ) )
576610
if int_geom.wkbType() == 7:
577611
int_com = geom.combine( tmpGeom )
578612
int_sym = geom.symDifference( tmpGeom )
579-
int_geom = int_com.difference( int_sym )
580-
except:
581-
int_geom = QgsGeometry()
582-
GEOS_EXCEPT = False
583-
found = False
584-
break
585-
outFeat.setGeometry( int_geom )
586-
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
587-
writer.addFeature( outFeat )
613+
int_geom = QgsGeometry( int_com.difference( int_sym ) )
614+
try:
615+
outFeat.setGeometry( int_geom )
616+
outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
617+
writer.addFeature( outFeat )
618+
except:
619+
FEATURE_EXCEPT = False
620+
continue
621+
except:
622+
GEOS_EXCEPT = False
623+
found = False
624+
continue
588625
if found:
589-
outFeat.setGeometry( diff_geom )
590-
outFeat.setAttributeMap( atMapA )
591-
writer.addFeature( outFeat )
626+
try:
627+
outFeat.setGeometry( diff_geom )
628+
outFeat.setAttributeMap( atMapA )
629+
writer.addFeature( outFeat )
630+
except:
631+
FEATURE_EXCEPT = False
632+
continue
592633
length = len( vproviderA.fields().values() )
593634
vproviderB.rewind()
594635
while vproviderB.nextFeature( inFeatA ):
595-
nElement += 1
596-
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
636+
add = True
597637
geom = QgsGeometry( inFeatA.geometry() )
638+
diff_geom = QgsGeometry( geom )
598639
atMap = inFeatA.attributeMap().values()
599640
atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
600641
intersects = indexB.intersects( geom.boundingBox() )
601642
if len(intersects) <= 0:
602-
outFeat.setGeometry( geom )
603-
outFeat.setAttributeMap( atMapA )
604-
writer.addFeature( outFeat )
643+
try:
644+
outFeat.setGeometry( diff_geom )
645+
outFeat.setAttributeMap( atMapA )
646+
writer.addFeature( outFeat )
647+
except:
648+
FEATURE_EXCEPT = False
649+
continue
605650
else:
606651
for id in intersects:
607652
vproviderA.featureAtId( int( id ), inFeatB , True, allAttrsA )
608653
atMapB = inFeatB.attributeMap()
609654
tmpGeom = QgsGeometry( inFeatB.geometry() )
610655
try:
611-
if geom.intersects( tmpGeom ):
612-
geom = geom.difference( tmpGeom )
656+
if diff_geom.intersects( tmpGeom ):
657+
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
613658
except:
614-
geom = QgsGeometry()
659+
add = False
615660
GEOS_EXCEPT = False
661+
break
662+
if add:
663+
try:
664+
outFeat.setGeometry( diff_geom )
665+
outFeat.setAttributeMap( atMap )
666+
writer.addFeature( outFeat )
667+
except:
668+
FEATURE_EXCEPT = False
616669
continue
617-
outFeat.setGeometry( geom )
618-
outFeat.setAttributeMap( atMap )
619-
writer.addFeature( outFeat )
670+
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
671+
nElement += 1
620672
del writer
621-
return True, crs_match
673+
return GEOS_EXCEPT, FEATURE_EXCEPT, crs_match
622674

623675
def symetrical_difference( self ):
624676
GEOS_EXCEPT = True
677+
FEATURE_EXCEPT = True
625678
vproviderA = self.vlayerA.dataProvider()
626679
allAttrsA = vproviderA.attributeIndexes()
627680
vproviderA.select( allAttrsA )
@@ -646,49 +699,64 @@ def symetrical_difference( self ):
646699
while vproviderA.nextFeature( inFeatA ):
647700
nElement += 1
648701
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
702+
add = True
649703
geom = QgsGeometry( inFeatA.geometry() )
704+
diff_geom = QgsGeometry( geom )
650705
atMapA = inFeatA.attributeMap()
651706
intersects = indexA.intersects( geom.boundingBox() )
652707
for id in intersects:
653708
vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
654709
tmpGeom = QgsGeometry( inFeatB.geometry() )
655710
try:
656-
if geom.intersects( tmpGeom ):
657-
geom = geom.difference( tmpGeom )
711+
if diff_geom.intersects( tmpGeom ):
712+
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
658713
except:
659-
geom = QgsGeometry()
714+
add = False
660715
GEOS_EXCEPT = False
716+
break
717+
if add:
718+
try:
719+
outFeat.setGeometry( diff_geom )
720+
outFeat.setAttributeMap( atMapA )
721+
writer.addFeature( outFeat )
722+
except:
723+
FEATURE_EXCEPT = False
661724
continue
662-
outFeat.setGeometry( geom )
663-
outFeat.setAttributeMap( atMapA )
664-
writer.addFeature( outFeat )
665725
length = len( vproviderA.fields().values() )
666726
vproviderB.rewind()
667727
while vproviderB.nextFeature( inFeatA ):
668728
nElement += 1
669729
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
730+
add = True
670731
geom = QgsGeometry( inFeatA.geometry() )
732+
diff_geom = QgsGeometry( geom )
671733
atMap = inFeatA.attributeMap().values()
672734
atMap = dict( zip( range( length, length + len( atMap ) ), atMap ) )
673735
intersects = indexB.intersects( geom.boundingBox() )
674736
for id in intersects:
675737
vproviderA.featureAtId( int( id ), inFeatB , True, allAttrsA )
676738
tmpGeom = QgsGeometry( inFeatB.geometry() )
677739
try:
678-
if geom.intersects( tmpGeom ):
679-
geom = geom.difference( tmpGeom )
740+
if diff_geom.intersects( tmpGeom ):
741+
diff_geom = QgsGeometry( diff_geom.difference( tmpGeom ) )
680742
except:
681-
geom = QgsGeometry()
743+
add = False
682744
GEOS_EXCEPT = False
745+
break
746+
if add:
747+
try:
748+
outFeat.setGeometry( diff_geom )
749+
outFeat.setAttributeMap( atMap )
750+
writer.addFeature( outFeat )
751+
except:
752+
FEATURE_EXCEPT = False
683753
continue
684-
outFeat.setGeometry( geom )
685-
outFeat.setAttributeMap( atMap )
686-
writer.addFeature( outFeat )
687754
del writer
688-
return GEOS_EXCEPT, crs_match
755+
return GEOS_EXCEPT, FEATURE_EXCEPT, crs_match
689756

690757
def clip( self ):
691758
GEOS_EXCEPT = True
759+
FEATURE_EXCEPT = True
692760
vproviderA = self.vlayerA.dataProvider()
693761
allAttrsA = vproviderA.attributeIndexes()
694762
vproviderA.select( allAttrsA )
@@ -720,19 +788,23 @@ def clip( self ):
720788
tmpGeom = QgsGeometry( inFeatB.geometry() )
721789
try:
722790
if geom.intersects( tmpGeom ):
723-
int_geom = geom.intersection( tmpGeom )
791+
int_geom = QgsGeometry( geom.intersection( tmpGeom ) )
724792
if int_geom.wkbType() == 7:
725793
int_com = geom.combine( tmpGeom )
726794
int_sym = geom.symDifference( tmpGeom )
727-
int_geom = int_com.difference( int_sym )
728-
outFeat.setGeometry( int_geom )
729-
outFeat.setAttributeMap( atMap )
730-
writer.addFeature( outFeat )
795+
int_geom = QgsGeometry( int_com.difference( int_sym ) )
796+
try:
797+
outFeat.setGeometry( int_geom )
798+
outFeat.setAttributeMap( atMap )
799+
writer.addFeature( outFeat )
800+
except:
801+
FEATURE_EXCEPT = False
802+
continue
731803
except:
732804
GEOS_EXCEPT = False
733-
continue
805+
break
734806
del writer
735-
return GEOS_EXCEPT, crs_match
807+
return GEOS_EXCEPT, FEATURE_EXCEPT, crs_match
736808

737809
def checkParameter( self, layer, param ):
738810
if self.myFunction == 1:

0 commit comments

Comments
 (0)
Please sign in to comment.