Skip to content

Commit 30fcaed

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.
1 parent e259e62 commit 30fcaed

File tree

8 files changed

+205
-1
lines changed

8 files changed

+205
-1
lines changed

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

+5
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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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
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+
provider = layer.dataProvider()
63+
64+
writer = self.getOutputFromName(
65+
self.OUTPUT_LAYER).getVectorWriter(
66+
layer.fields().toList(),
67+
provider.geometryType(),
68+
layer.crs())
69+
70+
features = vector.features(layer)
71+
total = 100.0 / len(features)
72+
73+
for current, inFeat in enumerate(features):
74+
outFeat = QgsFeature()
75+
attrs = inFeat.attributes()
76+
outFeat.setAttributes(attrs)
77+
78+
inGeom = inFeat.constGeometry()
79+
if inGeom:
80+
outGeom = inGeom.mergeLines()
81+
if outGeom is None:
82+
raise GeoAlgorithmExecutionException(
83+
self.tr('Error merging lines'))
84+
85+
outFeat.setGeometry(outGeom)
86+
87+
writer.addFeature(outFeat)
88+
progress.setPercentage(int(current * total))
89+
90+
del writer

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

+2-1
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:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>merge_lines</Name>
4+
<ElementPath>merge_lines</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>
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>
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>
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

+12
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,15 @@ tests:
392392
compare:
393393
geometry:
394394
precision: 7
395+
396+
- algorithm: qgis:mergelines
397+
name: Merge lines algorithm
398+
params:
399+
INPUT_LAYER:
400+
name: multilines.gml
401+
type: vector
402+
results:
403+
OUTPUT_LAYER:
404+
name: expected/merge_lines.gml
405+
type: vector
406+

0 commit comments

Comments
 (0)