|
26 | 26 | from PyQt4.QtCore import *
|
27 | 27 | from qgis.core import *
|
28 | 28 | from processing.core.GeoAlgorithm import GeoAlgorithm
|
29 |
| -from processing.tools import dataobjects, vector |
30 | 29 | from processing.core.ProcessingLog import ProcessingLog
|
31 | 30 | from processing.parameters.ParameterVector import ParameterVector
|
32 | 31 | from processing.outputs.OutputVector import OutputVector
|
| 32 | +from processing.tools import dataobjects, vector |
33 | 33 | from processing.tools import vector as utils
|
34 | 34 |
|
35 | 35 | class Clip(GeoAlgorithm):
|
36 | 36 |
|
| 37 | + |
37 | 38 | INPUT = "INPUT"
|
38 | 39 | OVERLAY = "OVERLAY"
|
39 | 40 | OUTPUT = "OUTPUT"
|
@@ -66,33 +67,44 @@ def processAlgorithm(self, progress):
|
66 | 67 | for inFeatA in selectionA:
|
67 | 68 | geom = QgsGeometry(inFeatA.geometry())
|
68 | 69 | attrs = inFeatA.attributes()
|
69 |
| - intersections = index.intersects(geom.boundingBox()) |
| 70 | + intersects = index.intersects(geom.boundingBox()) |
70 | 71 | first = True
|
71 | 72 | found = False
|
72 |
| - if len(intersections) > 0: |
73 |
| - for i in intersections: |
74 |
| - request = QgsFeatureRequest().setFilterFid(i) |
75 |
| - inFeatB = layerB.getFeatures(request).next() |
| 73 | + if len(intersects) > 0: |
| 74 | + for i in intersects: |
| 75 | + layerB.getFeatures(QgsFeatureRequest().setFilterFid(i)).nextFeature(inFeatB) |
76 | 76 | tmpGeom = QgsGeometry(inFeatB.geometry())
|
77 | 77 | if tmpGeom.intersects(geom):
|
78 | 78 | found = True
|
79 | 79 | if first:
|
80 | 80 | outFeat.setGeometry(QgsGeometry(tmpGeom))
|
81 | 81 | first = False
|
82 | 82 | else:
|
83 |
| - cur_geom = QgsGeometry(outFeat.geometry()) |
84 |
| - new_geom = QgsGeometry(cur_geom.combine(tmpGeom)) |
85 |
| - outFeat.setGeometry(QgsGeometry(new_geom)) |
86 |
| - if found: |
87 |
| - cur_geom = QgsGeometry(outFeat.geometry()) |
88 |
| - new_geom = QgsGeometry(geom.intersection(cur_geom)) |
89 |
| - if new_geom.wkbType() == QGis.WKBNoGeometry : |
90 |
| - int_com = QgsGeometry(geom.combine(cur_geom)) |
91 |
| - int_sym = QgsGeometry(geom.symDifference(cur_geom)) |
92 |
| - new_geom = QgsGeometry(int_com.difference(int_sym)) |
93 |
| - outFeat.setGeometry(new_geom) |
94 |
| - outFeat.setAttributes(attrs) |
95 |
| - writer.addFeature(outFeat) |
| 83 | + try: |
| 84 | + cur_geom = QgsGeometry(outFeat.geometry()) |
| 85 | + new_geom = QgsGeometry(cur_geom.combine(tmpGeom)) |
| 86 | + outFeat.setGeometry(QgsGeometry(new_geom)) |
| 87 | + except: |
| 88 | + ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "GEOS geoprocessing error: One or more input features have invalid geometry.") |
| 89 | + break |
| 90 | + if found: |
| 91 | + try: |
| 92 | + cur_geom = QgsGeometry(outFeat.geometry()) |
| 93 | + new_geom = QgsGeometry(geom.intersection(cur_geom)) |
| 94 | + if new_geom.wkbType() == 0: |
| 95 | + int_com = QgsGeometry(geom.combine(cur_geom)) |
| 96 | + int_sym = QgsGeometry(geom.symDifference(cur_geom)) |
| 97 | + new_geom = QgsGeometry(int_com.difference(int_sym)) |
| 98 | + try: |
| 99 | + outFeat.setGeometry(new_geom) |
| 100 | + outFeat.setAttributes(attrs) |
| 101 | + writer.addFeature(outFeat) |
| 102 | + except: |
| 103 | + ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Feature geometry error: One or more output features ignored due to invalid geometry.") |
| 104 | + continue |
| 105 | + except: |
| 106 | + ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "GEOS geoprocessing error: One or more input features have invalid geometry.") |
| 107 | + continue |
96 | 108 |
|
97 | 109 | current += 1
|
98 | 110 | progress.setPercentage(int(current * total))
|
|
0 commit comments