|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + SplitWithLines.py |
| 6 | + --------------------- |
| 7 | + Date : November 2014 |
| 8 | + Revised : November 2016 |
| 9 | + Copyright : (C) 2014 by Bernhard Ströbl |
| 10 | + Email : bernhard dot stroebl at jena dot de |
| 11 | +*************************************************************************** |
| 12 | +* * |
| 13 | +* This program is free software; you can redistribute it and/or modify * |
| 14 | +* it under the terms of the GNU General Public License as published by * |
| 15 | +* the Free Software Foundation; either version 2 of the License, or * |
| 16 | +* (at your option) any later version. * |
| 17 | +* * |
| 18 | +*************************************************************************** |
| 19 | +""" |
| 20 | + |
| 21 | +__author__ = 'Bernhard Ströbl' |
| 22 | +__date__ = 'November 2014' |
| 23 | +__copyright__ = '(C) 2014, Bernhard Ströbl' |
| 24 | + |
| 25 | +# This will get replaced with a git SHA1 when you do a git archive |
| 26 | + |
| 27 | +__revision__ = '$Format:%H$' |
| 28 | + |
| 29 | +from qgis.core import QgsFeatureRequest, QgsFeature, QgsGeometry, QgsSpatialIndex, QgsWkbTypes, QgsMessageLog |
| 30 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 31 | +from processing.core.parameters import ParameterVector |
| 32 | +from processing.core.outputs import OutputVector |
| 33 | +from processing.core.ProcessingLog import ProcessingLog |
| 34 | +from processing.tools import dataobjects |
| 35 | +from processing.tools import vector |
| 36 | + |
| 37 | + |
| 38 | +class SplitWithLines(GeoAlgorithm): |
| 39 | + |
| 40 | + INPUT_A = 'INPUT_A' |
| 41 | + INPUT_B = 'INPUT_B' |
| 42 | + |
| 43 | + OUTPUT = 'OUTPUT' |
| 44 | + |
| 45 | + def defineCharacteristics(self): |
| 46 | + self.name, self.i18n_name = self.trAlgorithm('Split with lines') |
| 47 | + self.group, self.i18n_group = self.trAlgorithm('Vector overlay tools') |
| 48 | + self.addParameter(ParameterVector(self.INPUT_A, |
| 49 | + self.tr('Input layer, single geometries only'), [dataobjects.TYPE_VECTOR_POLYGON, |
| 50 | + dataobjects.TYPE_VECTOR_LINE])) |
| 51 | + self.addParameter(ParameterVector(self.INPUT_B, |
| 52 | + self.tr('Split layer'), [dataobjects.TYPE_VECTOR_LINE])) |
| 53 | + |
| 54 | + self.addOutput(OutputVector(self.OUTPUT, self.tr('Split'))) |
| 55 | + |
| 56 | + def processAlgorithm(self, progress): |
| 57 | + layerA = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_A)) |
| 58 | + splitLayer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_B)) |
| 59 | + |
| 60 | + sameLayer = self.getParameterValue(self.INPUT_A) == self.getParameterValue(self.INPUT_B) |
| 61 | + fieldList = layerA.fields() |
| 62 | + |
| 63 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, |
| 64 | + layerA.wkbType(), layerA.crs()) |
| 65 | + |
| 66 | + spatialIndex = QgsSpatialIndex() |
| 67 | + splitGeoms = {} |
| 68 | + request = QgsFeatureRequest() |
| 69 | + request.setSubsetOfAttributes([]) |
| 70 | + |
| 71 | + for aSplitFeature in vector.features(splitLayer, request): |
| 72 | + splitGeoms[aSplitFeature.id()] = aSplitFeature.geometry() |
| 73 | + spatialIndex.insertFeature(aSplitFeature) |
| 74 | + # honor the case that user has selection on split layer and has setting "use selection" |
| 75 | + |
| 76 | + outFeat = QgsFeature() |
| 77 | + features = vector.features(layerA) |
| 78 | + |
| 79 | + if len(features) == 0: |
| 80 | + total = 100 |
| 81 | + else: |
| 82 | + total = 100.0 / float(len(features)) |
| 83 | + |
| 84 | + multiGeoms = 0 # how many multi geometries were encountered |
| 85 | + |
| 86 | + for current, inFeatA in enumerate(features): |
| 87 | + inGeom = inFeatA.geometry() |
| 88 | + |
| 89 | + if inGeom.isMultipart(): |
| 90 | + multiGeoms += 1 |
| 91 | + # MultiGeometries are not allowed because the result of a splitted part cannot be clearly defined: |
| 92 | + # 1) add both new parts as new features |
| 93 | + # 2) store one part as a new feature and the other one as part of the multi geometry |
| 94 | + # 2a) which part should be which, seems arbitrary |
| 95 | + else: |
| 96 | + attrsA = inFeatA.attributes() |
| 97 | + outFeat.setAttributes(attrsA) |
| 98 | + inGeoms = [inGeom] |
| 99 | + lines = spatialIndex.intersects(inGeom.boundingBox()) |
| 100 | + |
| 101 | + if len(lines) > 0: # has intersection of bounding boxes |
| 102 | + splittingLines = [] |
| 103 | + |
| 104 | + for i in lines: |
| 105 | + try: |
| 106 | + splitGeom = splitGeoms[i] |
| 107 | + except: |
| 108 | + continue |
| 109 | + |
| 110 | + # check if trying to self-intersect |
| 111 | + if sameLayer: |
| 112 | + if inFeatA.id() == i: |
| 113 | + continue |
| 114 | + |
| 115 | + engine = QgsGeometry.createGeometryEngine(inGeom.geometry()) |
| 116 | + engine.prepareGeometry() |
| 117 | + |
| 118 | + if engine.intersects(splitGeom.geometry()): |
| 119 | + splittingLines.append(splitGeom) |
| 120 | + |
| 121 | + if len(splittingLines) > 0: |
| 122 | + for splitGeom in splittingLines: |
| 123 | + splitterPList = None |
| 124 | + outGeoms = [] |
| 125 | + |
| 126 | + while len(inGeoms) > 0: |
| 127 | + inGeom = inGeoms.pop() |
| 128 | + engine = QgsGeometry.createGeometryEngine(inGeom.geometry()) |
| 129 | + engine.prepareGeometry() |
| 130 | + inPoints = vector.extractPoints(inGeom) |
| 131 | + |
| 132 | + if engine.intersects(splitGeom.geometry()): |
| 133 | + if splitterPList == None: |
| 134 | + splitterPList = vector.extractPoints(splitGeom) |
| 135 | + |
| 136 | + try: |
| 137 | + result, newGeometries, topoTestPoints = inGeom.splitGeometry(splitterPList, False) |
| 138 | + except: |
| 139 | + ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, |
| 140 | + self.tr('Geometry exception while splitting')) |
| 141 | + result = 1 |
| 142 | + |
| 143 | + # splitGeometry: If there are several intersections |
| 144 | + # between geometry and splitLine, only the first one is considered. |
| 145 | + if result == 0: # split occurred |
| 146 | + |
| 147 | + if inPoints == vector.extractPoints(inGeom): |
| 148 | + # bug in splitGeometry: sometimes it returns 0 but |
| 149 | + # the geometry is unchanged |
| 150 | + QgsMessageLog.logMessage("appending") |
| 151 | + outGeoms.append(inGeom) |
| 152 | + else: |
| 153 | + inGeoms.append(inGeom) |
| 154 | + |
| 155 | + for aNewGeom in newGeometries: |
| 156 | + inGeoms.append(aNewGeom) |
| 157 | + else: |
| 158 | + QgsMessageLog.logMessage("appending else") |
| 159 | + outGeoms.append(inGeom) |
| 160 | + else: |
| 161 | + outGeoms.append(inGeom) |
| 162 | + |
| 163 | + inGeoms = outGeoms |
| 164 | + |
| 165 | + for aGeom in inGeoms: |
| 166 | + passed = True |
| 167 | + |
| 168 | + if QgsWkbTypes.geometryType( aGeom.wkbType() ) == QgsWkbTypes.LineGeometry \ |
| 169 | + and not QgsWkbTypes.isMultiType( aGeom.wkbType() ): |
| 170 | + passed = len(aGeom.asPolyline()) > 2 |
| 171 | + |
| 172 | + if not passed: |
| 173 | + passed = (len(aGeom.asPolyline()) == 2 and |
| 174 | + aGeom.asPolyline()[0] != aGeom.asPolyline()[1]) |
| 175 | + # sometimes splitting results in lines of zero length |
| 176 | + |
| 177 | + if passed: |
| 178 | + outFeat.setGeometry(aGeom) |
| 179 | + writer.addFeature(outFeat) |
| 180 | + |
| 181 | + progress.setPercentage(int(current * total)) |
| 182 | + |
| 183 | + if multiGeoms > 0: |
| 184 | + ProcessingLog.addToLog(ProcessingLog.LOG_INFO, |
| 185 | + self.tr('Feature geometry error: %s input features ignored due to multi-geometry.') % str(multiGeoms)) |
| 186 | + |
| 187 | + del writer |
0 commit comments