Skip to content

Commit a2c656e

Browse files
author
Bernhard Ströbl
committed
[processing] [feature] split lines with lines
1 parent 0fa40a6 commit a2c656e

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-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: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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
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)