|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + SplitLines.py |
| 6 | + --------------------- |
| 7 | + Date : November 2014 |
| 8 | + Copyright : (C) 2014 by Bernhard Ströbl |
| 9 | + Email : bernhard dot stroebl at jena dot de |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Bernhard Ströbl' |
| 21 | +__date__ = 'November 2014' |
| 22 | +__copyright__ = '(C) 2014, Bernhard Ströbl' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +from PyQt4.QtCore import * |
| 29 | +from PyQt4.QtGui import * |
| 30 | +from qgis.core import * |
| 31 | +from processing.core.GeoAlgorithm import GeoAlgorithm |
| 32 | +from processing.core.parameters import ParameterVector |
| 33 | +from processing.core.outputs import OutputVector |
| 34 | +from processing.tools import dataobjects |
| 35 | +from processing.tools import vector |
| 36 | + |
| 37 | + |
| 38 | +class SplitLinesWithLines(GeoAlgorithm): |
| 39 | + |
| 40 | + INPUT_A = 'INPUT_A' |
| 41 | + INPUT_B = 'INPUT_B' |
| 42 | + |
| 43 | + OUTPUT = 'OUTPUT' |
| 44 | + |
| 45 | + def defineCharacteristics(self): |
| 46 | + self.name = 'Split lines with lines' |
| 47 | + self.group = 'Vector overlay tools' |
| 48 | + self.addParameter(ParameterVector(self.INPUT_A, 'Input layer', |
| 49 | + [ParameterVector.VECTOR_TYPE_LINE])) |
| 50 | + self.addParameter(ParameterVector(self.INPUT_B, 'Split layer', |
| 51 | + [ParameterVector.VECTOR_TYPE_LINE])) |
| 52 | + |
| 53 | + self.addOutput(OutputVector(self.OUTPUT, 'Split lines')) |
| 54 | + |
| 55 | + def processAlgorithm(self, progress): |
| 56 | + layerA = dataobjects.getObjectFromUri( |
| 57 | + self.getParameterValue(self.INPUT_A)) |
| 58 | + layerB = dataobjects.getObjectFromUri( |
| 59 | + self.getParameterValue(self.INPUT_B)) |
| 60 | + |
| 61 | + fieldList = layerA.pendingFields() |
| 62 | + |
| 63 | + writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, |
| 64 | + QGis.WKBLineString, layerA.dataProvider().crs()) |
| 65 | + |
| 66 | + spatialIndex = vector.spatialindex(layerB) |
| 67 | + |
| 68 | + inFeatA = QgsFeature() |
| 69 | + inFeatB = QgsFeature() |
| 70 | + outFeat = QgsFeature() |
| 71 | + inGeom = QgsGeometry() |
| 72 | + splitGeom = QgsGeometry() |
| 73 | + |
| 74 | + features = vector.features(layerA) |
| 75 | + current = 0 |
| 76 | + total = 100.0 / float(len(features)) |
| 77 | + |
| 78 | + for inFeatA in features: |
| 79 | + inGeom = inFeatA.geometry() |
| 80 | + attrsA = inFeatA.attributes() |
| 81 | + outFeat.setAttributes(attrsA) |
| 82 | + inLines = [inGeom] |
| 83 | + lines = spatialIndex.intersects(inGeom.boundingBox()) |
| 84 | + |
| 85 | + if len(lines) > 0: #hasIntersections |
| 86 | + splittingLines = [] |
| 87 | + |
| 88 | + for i in lines: |
| 89 | + request = QgsFeatureRequest().setFilterFid(i) |
| 90 | + inFeatB = layerB.getFeatures(request).next() |
| 91 | + splitGeom = QgsGeometry(inFeatB.geometry()) |
| 92 | + |
| 93 | + if inGeom.intersects(splitGeom): |
| 94 | + splittingLines.append(splitGeom) |
| 95 | + |
| 96 | + if len(splittingLines) > 0: |
| 97 | + for splitGeom in splittingLines: |
| 98 | + splitterPList = vector.extractPoints(splitGeom) |
| 99 | + outLines = [] |
| 100 | + |
| 101 | + while len(inLines) > 0: |
| 102 | + inGeom = inLines.pop() |
| 103 | + inPoints = vector.extractPoints(inGeom) |
| 104 | + |
| 105 | + if inGeom.intersects(splitGeom): |
| 106 | + try: |
| 107 | + result, newGeometries, topoTestPoints = inGeom.splitGeometry(splitterPList, False) |
| 108 | + except: |
| 109 | + QgsMessageLog.logMessage("exception") |
| 110 | + result = 1 |
| 111 | + |
| 112 | + # splitGeometry: If there are several intersections |
| 113 | + # between geometry and splitLine, only the first one is considered. |
| 114 | + if result == 0: #split occured |
| 115 | + |
| 116 | + if inPoints == vector.extractPoints(inGeom): |
| 117 | + # bug in splitGeometry: sometimes it returns 0 but |
| 118 | + # the geometry is unchanged |
| 119 | + outLines.append(inGeom) |
| 120 | + else: |
| 121 | + inLines.append(inGeom) |
| 122 | + |
| 123 | + for aNewGeom in newGeometries: |
| 124 | + inLines.append(aNewGeom) |
| 125 | + else: |
| 126 | + outLines.append(inGeom) |
| 127 | + else: |
| 128 | + outLines.append(inGeom) |
| 129 | + |
| 130 | + inLines = outLines |
| 131 | + |
| 132 | + |
| 133 | + for aLine in inLines: |
| 134 | + outFeat.setGeometry(aLine) |
| 135 | + writer.addFeature(outFeat) |
| 136 | + |
| 137 | + current += 1 |
| 138 | + progress.setPercentage(int(current * total)) |
| 139 | + |
| 140 | + del writer |
0 commit comments