|
| 1 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 2 | +import os.path |
| 3 | +from PyQt4 import QtGui |
| 4 | +from PyQt4.QtCore import * |
| 5 | +from PyQt4.QtGui import * |
| 6 | +from qgis.core import * |
| 7 | +from sextante.parameters.ParameterVector import ParameterVector |
| 8 | +from sextante.core.QGisLayers import QGisLayers |
| 9 | +from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException |
| 10 | +from sextante.outputs.OutputVector import OutputVector |
| 11 | +from sextante.ftools import ftools_utils |
| 12 | +from sextante.core.SextanteLog import SextanteLog |
| 13 | +from sextante.parameters.ParameterBoolean import ParameterBoolean |
| 14 | + |
| 15 | +class Clip(GeoAlgorithm): |
| 16 | + |
| 17 | + INPUT = "INPUT" |
| 18 | + INPUT2 = "INPUT2" |
| 19 | + OUTPUT = "OUTPUT" |
| 20 | + USE_SELECTED = "USE_SELECTED" |
| 21 | + USE_SELECTED2 = "USE_SELECTED2" |
| 22 | + |
| 23 | + def getIcon(self): |
| 24 | + return QtGui.QIcon(os.path.dirname(__file__) + "/icons/clip.png") |
| 25 | + |
| 26 | + def processAlgorithm(self, progress): |
| 27 | + settings = QSettings() |
| 28 | + systemEncoding = settings.value( "/UI/encoding", "System" ).toString() |
| 29 | + output = self.getOutputValue(Clip.OUTPUT) |
| 30 | + useSelection = self.getParameterValue(Clip.USE_SELECTED) |
| 31 | + useSelection2 = self.getParameterValue(Clip.USE_SELECTED2) |
| 32 | + vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Clip.INPUT)) |
| 33 | + vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Clip.INPUT2)) |
| 34 | + GEOS_EXCEPT = True |
| 35 | + FEATURE_EXCEPT = True |
| 36 | + vproviderA = self.vlayerA.dataProvider() |
| 37 | + allAttrsA = vproviderA.attributeIndexes() |
| 38 | + vproviderA.select( allAttrsA ) |
| 39 | + vproviderB = self.vlayerB.dataProvider() |
| 40 | + allAttrsB = vproviderB.attributeIndexes() |
| 41 | + vproviderB.select( allAttrsB ) |
| 42 | + # check for crs compatibility |
| 43 | + crsA = vproviderA.crs() |
| 44 | + crsB = vproviderB.crs() |
| 45 | + if not crsA.isValid() or not crsB.isValid(): |
| 46 | + SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Invalid CRS. Results might be unexpected") |
| 47 | + else: |
| 48 | + if not crsA != crsB: |
| 49 | + SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Non-matching CRSs. Results might be unexpected") |
| 50 | + fields = vproviderA.fields() |
| 51 | + writer = QgsVectorFileWriter( output, systemEncoding,fields, vproviderA.geometryType(), vproviderA.crs() ) |
| 52 | + |
| 53 | + inFeatA = QgsFeature() |
| 54 | + inFeatB = QgsFeature() |
| 55 | + outFeat = QgsFeature() |
| 56 | + index = ftools_utils.createIndex( vproviderB ) |
| 57 | + vproviderA.rewind() |
| 58 | + nElement = 0 |
| 59 | + # there is selection in input layer |
| 60 | + if useSelection: |
| 61 | + nFeat = vlayerA.selectedFeatureCount() |
| 62 | + selectionA = self.vlayerA.selectedFeatures() |
| 63 | + # we have selection in overlay layer |
| 64 | + if useSelection2: |
| 65 | + selectionB = self.vlayerB.selectedFeaturesIds() |
| 66 | + for inFeatA in selectionA: |
| 67 | + nElement += 1 |
| 68 | + progress.setPercentage(int(nElement/nFeat * 100)) |
| 69 | + geom = QgsGeometry( inFeatA.geometry() ) |
| 70 | + int_geom = QgsGeometry( geom ) |
| 71 | + atMap = inFeatA.attributeMap() |
| 72 | + intersects = index.intersects( geom.boundingBox() ) |
| 73 | + found = False |
| 74 | + first = True |
| 75 | + for id in intersects: |
| 76 | + if id in selectionB: |
| 77 | + vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB ) |
| 78 | + tmpGeom = QgsGeometry( inFeatB.geometry() ) |
| 79 | + if tmpGeom.intersects( geom ): |
| 80 | + found = True |
| 81 | + if first: |
| 82 | + outFeat.setGeometry( QgsGeometry( tmpGeom ) ) |
| 83 | + first = False |
| 84 | + else: |
| 85 | + try: |
| 86 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 87 | + new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) ) |
| 88 | + outFeat.setGeometry( QgsGeometry( new_geom ) ) |
| 89 | + except: |
| 90 | + GEOS_EXCEPT = False |
| 91 | + break |
| 92 | + if found: |
| 93 | + try: |
| 94 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 95 | + new_geom = QgsGeometry( geom.intersection( cur_geom ) ) |
| 96 | + if new_geom.wkbType() == 7: |
| 97 | + int_com = QgsGeometry( geom.combine( cur_geom ) ) |
| 98 | + int_sym = QgsGeometry( geom.symDifference( cur_geom ) ) |
| 99 | + new_geom = QgsGeometry( int_com.difference( int_sym ) ) |
| 100 | + try: |
| 101 | + outFeat.setGeometry( new_geom ) |
| 102 | + outFeat.setAttributeMap( atMap ) |
| 103 | + writer.addFeature( outFeat ) |
| 104 | + except: |
| 105 | + FEAT_EXCEPT = False |
| 106 | + continue |
| 107 | + except: |
| 108 | + GEOS_EXCEPT = False |
| 109 | + continue |
| 110 | + # we have no selection in overlay layer |
| 111 | + else: |
| 112 | + for inFeatA in selectionA: |
| 113 | + nElement += 1 |
| 114 | + progress.setPercentage(int(nElement/nFeat * 100)) |
| 115 | + geom = QgsGeometry( inFeatA.geometry() ) |
| 116 | + atMap = inFeatA.attributeMap() |
| 117 | + intersects = index.intersects( geom.boundingBox() ) |
| 118 | + found = False |
| 119 | + first = True |
| 120 | + for id in intersects: |
| 121 | + vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB ) |
| 122 | + tmpGeom = QgsGeometry( inFeatB.geometry() ) |
| 123 | + if tmpGeom.intersects( geom ): |
| 124 | + found = True |
| 125 | + if first: |
| 126 | + outFeat.setGeometry( QgsGeometry( tmpGeom ) ) |
| 127 | + first = False |
| 128 | + else: |
| 129 | + try: |
| 130 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 131 | + new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) ) |
| 132 | + outFeat.setGeometry( QgsGeometry( new_geom ) ) |
| 133 | + except: |
| 134 | + GEOS_EXCEPT = False |
| 135 | + break |
| 136 | + if found: |
| 137 | + try: |
| 138 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 139 | + new_geom = QgsGeometry( geom.intersection( cur_geom ) ) |
| 140 | + if new_geom.wkbType() == 7: |
| 141 | + int_com = QgsGeometry( geom.combine( cur_geom ) ) |
| 142 | + int_sym = QgsGeometry( geom.symDifference( cur_geom ) ) |
| 143 | + new_geom = QgsGeometry( int_com.difference( int_sym ) ) |
| 144 | + try: |
| 145 | + outFeat.setGeometry( new_geom ) |
| 146 | + outFeat.setAttributeMap( atMap ) |
| 147 | + writer.addFeature( outFeat ) |
| 148 | + except: |
| 149 | + FEAT_EXCEPT = False |
| 150 | + continue |
| 151 | + except: |
| 152 | + GEOS_EXCEPT = False |
| 153 | + continue |
| 154 | + # there is no selection in input layer |
| 155 | + else: |
| 156 | + nFeat = vproviderA.featureCount() |
| 157 | + # we have selection in overlay layer |
| 158 | + if useSelection2: |
| 159 | + selectionB = self.vlayerB.selectedFeaturesIds() |
| 160 | + while vproviderA.nextFeature( inFeatA ): |
| 161 | + nElement += 1 |
| 162 | + progress.setPercentage(int(nElement/nFeat * 100)) |
| 163 | + geom = QgsGeometry( inFeatA.geometry() ) |
| 164 | + atMap = inFeatA.attributeMap() |
| 165 | + intersects = index.intersects( geom.boundingBox() ) |
| 166 | + found = False |
| 167 | + first = True |
| 168 | + for id in intersects: |
| 169 | + if id in selectionB: |
| 170 | + vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB ) |
| 171 | + tmpGeom = QgsGeometry( inFeatB.geometry() ) |
| 172 | + if tmpGeom.intersects( geom ): |
| 173 | + found = True |
| 174 | + if first: |
| 175 | + outFeat.setGeometry( QgsGeometry( tmpGeom ) ) |
| 176 | + first = False |
| 177 | + else: |
| 178 | + try: |
| 179 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 180 | + new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) ) |
| 181 | + outFeat.setGeometry( QgsGeometry( new_geom ) ) |
| 182 | + except: |
| 183 | + GEOS_EXCEPT = False |
| 184 | + break |
| 185 | + if found: |
| 186 | + try: |
| 187 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 188 | + new_geom = QgsGeometry( geom.intersection( cur_geom ) ) |
| 189 | + if new_geom.wkbType() == 7: |
| 190 | + int_com = QgsGeometry( geom.combine( cur_geom ) ) |
| 191 | + int_sym = QgsGeometry( geom.symDifference( cur_geom ) ) |
| 192 | + new_geom = QgsGeometry( int_com.difference( int_sym ) ) |
| 193 | + try: |
| 194 | + outFeat.setGeometry( new_geom ) |
| 195 | + outFeat.setAttributeMap( atMap ) |
| 196 | + writer.addFeature( outFeat ) |
| 197 | + except: |
| 198 | + FEAT_EXCEPT = False |
| 199 | + continue |
| 200 | + except: |
| 201 | + GEOS_EXCEPT = False |
| 202 | + continue |
| 203 | + # we have no selection in overlay layer |
| 204 | + else: |
| 205 | + while vproviderA.nextFeature( inFeatA ): |
| 206 | + nElement += 1 |
| 207 | + progress.setPercentage(int(nElement/nFeat * 100)) |
| 208 | + geom = QgsGeometry( inFeatA.geometry() ) |
| 209 | + atMap = inFeatA.attributeMap() |
| 210 | + intersects = index.intersects( geom.boundingBox() ) |
| 211 | + first = True |
| 212 | + found = False |
| 213 | + if len( intersects ) > 0: |
| 214 | + for id in intersects: |
| 215 | + vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB ) |
| 216 | + tmpGeom = QgsGeometry( inFeatB.geometry() ) |
| 217 | + if tmpGeom.intersects( geom ): |
| 218 | + found = True |
| 219 | + if first: |
| 220 | + outFeat.setGeometry( QgsGeometry( tmpGeom ) ) |
| 221 | + first = False |
| 222 | + else: |
| 223 | + try: |
| 224 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 225 | + new_geom = QgsGeometry( cur_geom.combine( tmpGeom ) ) |
| 226 | + outFeat.setGeometry( QgsGeometry( new_geom ) ) |
| 227 | + except: |
| 228 | + GEOS_EXCEPT = False |
| 229 | + break |
| 230 | + if found: |
| 231 | + try: |
| 232 | + cur_geom = QgsGeometry( outFeat.geometry() ) |
| 233 | + new_geom = QgsGeometry( geom.intersection( cur_geom ) ) |
| 234 | + if new_geom.wkbType() == 7: |
| 235 | + int_com = QgsGeometry( geom.combine( cur_geom ) ) |
| 236 | + int_sym = QgsGeometry( geom.symDifference( cur_geom ) ) |
| 237 | + new_geom = QgsGeometry( int_com.difference( int_sym ) ) |
| 238 | + try: |
| 239 | + outFeat.setGeometry( new_geom ) |
| 240 | + outFeat.setAttributeMap( atMap ) |
| 241 | + writer.addFeature( outFeat ) |
| 242 | + except: |
| 243 | + FEAT_EXCEPT = False |
| 244 | + continue |
| 245 | + except: |
| 246 | + GEOS_EXCEPT = False |
| 247 | + continue |
| 248 | + del writer |
| 249 | + if not GEOS_EXCEPT: |
| 250 | + SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing clip") |
| 251 | + if not FEATURE_EXCEPT: |
| 252 | + SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing clip") |
| 253 | + |
| 254 | + def defineCharacteristics(self): |
| 255 | + self.name = "Clip" |
| 256 | + self.group = "Geoprocessing tools" |
| 257 | + self.addParameter(ParameterVector(Clip.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY)) |
| 258 | + self.addParameter(ParameterVector(Clip.INPUT2, "Clip layer", ParameterVector.VECTOR_TYPE_ANY)) |
| 259 | + self.addParameter(ParameterBoolean(Clip.USE_SELECTED, "Use selected features (input)", False)) |
| 260 | + self.addParameter(ParameterBoolean(Clip.USE_SELECTED2, "Use selected features (clip)", False)) |
| 261 | + self.addOutput(OutputVector(Clip.OUTPUT, "Clipped")) |
0 commit comments