Skip to content

Commit 299037a

Browse files
committed
[FEATURE][processing] New algorithm for merging connected lines
This algorithm joins all connected parts of MultiLineString geometries into single LineString geometries. If any parts of the input MultiLineString geometries are not connected, the resultant geometry will be a MultiLineString containing any lines which could be merged and any non-connected line parts. (cherry-picked from 30fcaed)
1 parent 4e41f1a commit 299037a

File tree

7 files changed

+189
-1
lines changed

7 files changed

+189
-1
lines changed

python/plugins/processing/algs/help/qgis.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ qgis:meancoordinates: >
225225

226226
If an attribute is selected in the <Unique ID field> parameters, features will be grouped according to values in this field. Instead of a single point with the center of mass of the whole layer, the output layer will contain a center of mass for the features in each category.
227227

228+
qgis:mergelines: >
229+
This algorithm joins all connected parts of MultiLineString geometries into single LineString geometries.
230+
231+
If any parts of the input MultiLineString geometries are not connected, the resultant geometry will be a MultiLineString containing any lines which could be merged and any non-connected line parts.
232+
228233
qgis:mergevectorlayers: >
229234
This algorithm combines two vector layer of the same geometry type into a single one.
230235

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Smooth.py
6+
---------
7+
Date : July 2016
8+
Copyright : (C) 2016 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
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__ = 'Nyall Dawson'
21+
__date__ = 'July 2016'
22+
__copyright__ = '(C) 2016, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from qgis.core import QgsFeature, QgsGeometry
31+
32+
from qgis.PyQt.QtGui import QIcon
33+
34+
from processing.core.GeoAlgorithm import GeoAlgorithm
35+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
36+
from processing.core.parameters import ParameterVector
37+
from processing.core.outputs import OutputVector
38+
from processing.tools import dataobjects, vector
39+
40+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
41+
42+
43+
class MergeLines(GeoAlgorithm):
44+
45+
INPUT_LAYER = 'INPUT_LAYER'
46+
OUTPUT_LAYER = 'OUTPUT_LAYER'
47+
48+
def getIcon(self):
49+
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png'))
50+
51+
def defineCharacteristics(self):
52+
self.name, self.i18n_name = self.trAlgorithm('Merge lines')
53+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
54+
55+
self.addParameter(ParameterVector(self.INPUT_LAYER,
56+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE]))
57+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Merged')))
58+
59+
def processAlgorithm(self, progress):
60+
layer = dataobjects.getObjectFromUri(
61+
self.getParameterValue(self.INPUT_LAYER))
62+
63+
writer = self.getOutputFromName(
64+
self.OUTPUT_LAYER).getVectorWriter(
65+
layer.fields().toList(),
66+
layer.wkbType(),
67+
layer.crs())
68+
69+
features = vector.features(layer)
70+
total = 100.0 / len(features)
71+
72+
for current, inFeat in enumerate(features):
73+
outFeat = QgsFeature()
74+
attrs = inFeat.attributes()
75+
outFeat.setAttributes(attrs)
76+
77+
inGeom = QgsGeometry(inFeat.constGeometry())
78+
if inGeom:
79+
outGeom = inGeom.mergeLines()
80+
if outGeom is None:
81+
raise GeoAlgorithmExecutionException(
82+
self.tr('Error merging lines'))
83+
84+
outFeat.setGeometry(outGeom)
85+
86+
writer.addFeature(outFeat)
87+
progress.setPercentage(int(current * total))
88+
89+
del writer

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
from .DefineProjection import DefineProjection
147147
from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
148148
from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
149+
from .MergeLines import MergeLines
149150

150151
pluginPath = os.path.normpath(os.path.join(
151152
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -197,7 +198,7 @@ def __init__(self):
197198
CheckValidity(), OrientedMinimumBoundingBox(), Smooth(),
198199
ReverseLineDirection(), SpatialIndex(), DefineProjection(),
199200
RectanglesOvalsDiamondsVariable(),
200-
RectanglesOvalsDiamondsFixed()
201+
RectanglesOvalsDiamondsFixed(), MergeLines()
201202
]
202203

203204
if hasMatplotlib:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ogr.maptools.org/ merge_lines.xsd"
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>-1</gml:X><gml:Y>-1</gml:Y></gml:coord>
10+
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:merge_lines fid="lines.1">
16+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
17+
</ogr:merge_lines>
18+
</gml:featureMember>
19+
<gml:featureMember>
20+
<ogr:merge_lines fid="lines.2">
21+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>3,1 5,1 5.024184261036468,2.414779270633399</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
22+
</ogr:merge_lines>
23+
</gml:featureMember>
24+
<gml:featureMember>
25+
<ogr:merge_lines fid="lines.3">
26+
</ogr:merge_lines>
27+
</gml:featureMember>
28+
<gml:featureMember>
29+
<ogr:merge_lines fid="lines.4">
30+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>2,0 2,2 3,2 3,3 5.58042226487524,2.946833013435702</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>2.944337811900192,4.04721689059501 5.459500959692898,4.119769673704415</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
31+
</ogr:merge_lines>
32+
</gml:featureMember>
33+
</ogr:FeatureCollection>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>multilines</Name>
4+
<ElementPath>multilines</ElementPath>
5+
<GeometryType>5</GeometryType>
6+
<SRSName>EPSG:4326</SRSName>
7+
<DatasetSpecificInfo>
8+
<FeatureCount>4</FeatureCount>
9+
<ExtentXMin>-1.00000</ExtentXMin>
10+
<ExtentXMax>5.58042</ExtentXMax>
11+
<ExtentYMin>-1.00000</ExtentYMin>
12+
<ExtentYMax>4.11977</ExtentYMax>
13+
</DatasetSpecificInfo>
14+
</GMLFeatureClass>
15+
</GMLFeatureClassList>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>-1</gml:X><gml:Y>-1</gml:Y></gml:coord>
10+
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:multilines fid="lines.1">
16+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>-1,-1 1,-1</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
17+
</ogr:multilines>
18+
</gml:featureMember>
19+
<gml:featureMember>
20+
<ogr:multilines fid="lines.2">
21+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>3,1 5,1</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>5.024184261036468,2.414779270633399 5,1</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
22+
</ogr:multilines>
23+
</gml:featureMember>
24+
<gml:featureMember>
25+
<ogr:multilines fid="lines.3">
26+
</ogr:multilines>
27+
</gml:featureMember>
28+
<gml:featureMember>
29+
<ogr:multilines fid="lines.4">
30+
<ogr:geometryProperty><gml:MultiLineString srsName="EPSG:4326"><gml:lineStringMember><gml:LineString><gml:coordinates>2,0 2,2 3,2 3,3</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>2.944337811900192,4.04721689059501 5.459500959692898,4.119769673704415</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>3,3 5.58042226487524,2.946833013435702</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString></ogr:geometryProperty>
31+
</ogr:multilines>
32+
</gml:featureMember>
33+
</ogr:FeatureCollection>

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,15 @@ tests:
306306
compare:
307307
geometry:
308308
precision: 7
309+
310+
- algorithm: qgis:mergelines
311+
name: Merge lines algorithm
312+
params:
313+
INPUT_LAYER:
314+
name: multilines.gml
315+
type: vector
316+
results:
317+
OUTPUT_LAYER:
318+
name: expected/merge_lines.gml
319+
type: vector
320+

0 commit comments

Comments
 (0)