Skip to content

Commit 4980dba

Browse files
committedDec 8, 2014
Merge pull request #1719 from bstroebl/splitLines
[processing] [feature] split lines with lines
2 parents 535661d + da17656 commit 4980dba

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
 

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
from SetRasterStyle import SetRasterStyle
119119
from SelectByExpression import SelectByExpression
120120
from HypsometricCurves import HypsometricCurves
121+
from SplitLinesWithLines import SplitLinesWithLines
121122
# from VectorLayerHistogram import VectorLayerHistogram
122123
# from VectorLayerScatterplot import VectorLayerScatterplot
123124
# from MeanAndStdDevPlot import MeanAndStdDevPlot
@@ -169,6 +170,7 @@ def __init__(self):
169170
PostGISExecuteSQL(), ImportIntoPostGIS(),
170171
SetVectorStyle(), SetRasterStyle(),
171172
SelectByExpression(), HypsometricCurves(),
173+
SplitLinesWithLines()
172174
# ------ raster ------
173175
# CreateConstantRaster(),
174176
# ------ graphics ------
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.core.ProcessingLog import ProcessingLog
35+
from processing.tools import dataobjects
36+
from processing.tools import vector
37+
38+
39+
class SplitLinesWithLines(GeoAlgorithm):
40+
41+
INPUT_A = 'INPUT_A'
42+
INPUT_B = 'INPUT_B'
43+
44+
OUTPUT = 'OUTPUT'
45+
46+
def defineCharacteristics(self):
47+
self.name = 'Split lines with lines'
48+
self.group = 'Vector overlay tools'
49+
self.addParameter(ParameterVector(self.INPUT_A, 'Input layer',
50+
[ParameterVector.VECTOR_TYPE_LINE]))
51+
self.addParameter(ParameterVector(self.INPUT_B, 'Split layer',
52+
[ParameterVector.VECTOR_TYPE_LINE]))
53+
54+
self.addOutput(OutputVector(self.OUTPUT, 'Split lines'))
55+
56+
def processAlgorithm(self, progress):
57+
layerA = dataobjects.getObjectFromUri(
58+
self.getParameterValue(self.INPUT_A))
59+
layerB = dataobjects.getObjectFromUri(
60+
self.getParameterValue(self.INPUT_B))
61+
62+
fieldList = layerA.pendingFields()
63+
64+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
65+
QGis.WKBLineString, layerA.dataProvider().crs())
66+
67+
spatialIndex = vector.spatialindex(layerB)
68+
69+
inFeatA = QgsFeature()
70+
inFeatB = QgsFeature()
71+
outFeat = QgsFeature()
72+
inGeom = QgsGeometry()
73+
splitGeom = QgsGeometry()
74+
75+
features = vector.features(layerA)
76+
current = 0
77+
total = 100.0 / float(len(features))
78+
79+
for inFeatA in features:
80+
inGeom = inFeatA.geometry()
81+
attrsA = inFeatA.attributes()
82+
outFeat.setAttributes(attrsA)
83+
inLines = [inGeom]
84+
lines = spatialIndex.intersects(inGeom.boundingBox())
85+
86+
if len(lines) > 0: #hasIntersections
87+
splittingLines = []
88+
89+
for i in lines:
90+
request = QgsFeatureRequest().setFilterFid(i)
91+
inFeatB = layerB.getFeatures(request).next()
92+
splitGeom = QgsGeometry(inFeatB.geometry())
93+
94+
if inGeom.intersects(splitGeom):
95+
splittingLines.append(splitGeom)
96+
97+
if len(splittingLines) > 0:
98+
for splitGeom in splittingLines:
99+
splitterPList = vector.extractPoints(splitGeom)
100+
outLines = []
101+
102+
while len(inLines) > 0:
103+
inGeom = inLines.pop()
104+
inPoints = vector.extractPoints(inGeom)
105+
106+
if inGeom.intersects(splitGeom):
107+
try:
108+
result, newGeometries, topoTestPoints = inGeom.splitGeometry(splitterPList, False)
109+
except:
110+
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
111+
'Geometry exception while splitting')
112+
result = 1
113+
114+
# splitGeometry: If there are several intersections
115+
# between geometry and splitLine, only the first one is considered.
116+
if result == 0: #split occured
117+
118+
if inPoints == vector.extractPoints(inGeom):
119+
# bug in splitGeometry: sometimes it returns 0 but
120+
# the geometry is unchanged
121+
outLines.append(inGeom)
122+
else:
123+
inLines.append(inGeom)
124+
125+
for aNewGeom in newGeometries:
126+
inLines.append(aNewGeom)
127+
else:
128+
outLines.append(inGeom)
129+
else:
130+
outLines.append(inGeom)
131+
132+
inLines = outLines
133+
134+
135+
for aLine in inLines:
136+
outFeat.setGeometry(aLine)
137+
writer.addFeature(outFeat)
138+
139+
current += 1
140+
progress.setPercentage(int(current * total))
141+
142+
del writer
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
SPLIT LINES WITH LINES
2+
=======================
3+
4+
Description
5+
-----------
6+
This algorithm splits the features of a line layer with the lines of another line layer.
7+
8+
Parameters
9+
----------
10+
11+
- ``Input layer[Vector]``:line layer
12+
- ``Split layer[Vector]``:line layer
13+
14+
Outputs
15+
-------
16+
17+
- ``Output layer[Vector]``: resulting layer
18+
19+
See also
20+
---------
21+
22+
23+
Console usage
24+
-------------
25+
26+
27+
::
28+
29+
processing.runalg('qgis:splitlineswithlines', input_lines, split_lines, output)

0 commit comments

Comments
 (0)
Please sign in to comment.