-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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)
- Loading branch information
1 parent
4e41f1a
commit 299037a
Showing
7 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
Smooth.py | ||
--------- | ||
Date : July 2016 | ||
Copyright : (C) 2016 by Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
__author__ = 'Nyall Dawson' | ||
__date__ = 'July 2016' | ||
__copyright__ = '(C) 2016, Nyall Dawson' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive323 | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
import os | ||
|
||
from qgis.core import QgsFeature, QgsGeometry | ||
|
||
from qgis.PyQt.QtGui import QIcon | ||
|
||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException | ||
from processing.core.parameters import ParameterVector | ||
from processing.core.outputs import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] | ||
|
||
|
||
class MergeLines(GeoAlgorithm): | ||
|
||
INPUT_LAYER = 'INPUT_LAYER' | ||
OUTPUT_LAYER = 'OUTPUT_LAYER' | ||
|
||
def getIcon(self): | ||
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png')) | ||
|
||
def defineCharacteristics(self): | ||
self.name, self.i18n_name = self.trAlgorithm('Merge lines') | ||
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools') | ||
|
||
self.addParameter(ParameterVector(self.INPUT_LAYER, | ||
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_LINE])) | ||
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Merged'))) | ||
|
||
def processAlgorithm(self, progress): | ||
layer = dataobjects.getObjectFromUri( | ||
self.getParameterValue(self.INPUT_LAYER)) | ||
|
||
writer = self.getOutputFromName( | ||
self.OUTPUT_LAYER).getVectorWriter( | ||
layer.fields().toList(), | ||
layer.wkbType(), | ||
layer.crs()) | ||
|
||
features = vector.features(layer) | ||
total = 100.0 / len(features) | ||
|
||
for current, inFeat in enumerate(features): | ||
outFeat = QgsFeature() | ||
attrs = inFeat.attributes() | ||
outFeat.setAttributes(attrs) | ||
|
||
inGeom = QgsGeometry(inFeat.constGeometry()) | ||
if inGeom: | ||
outGeom = inGeom.mergeLines() | ||
if outGeom is None: | ||
raise GeoAlgorithmExecutionException( | ||
self.tr('Error merging lines')) | ||
|
||
outFeat.setGeometry(outGeom) | ||
|
||
writer.addFeature(outFeat) | ||
progress.setPercentage(int(current * total)) | ||
|
||
del writer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
python/plugins/processing/tests/testdata/expected/merge_lines.gml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ogr:FeatureCollection | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://ogr.maptools.org/ merge_lines.xsd" | ||
xmlns:ogr="http://ogr.maptools.org/" | ||
xmlns:gml="http://www.opengis.net/gml"> | ||
<gml:boundedBy> | ||
<gml:Box> | ||
<gml:coord><gml:X>-1</gml:X><gml:Y>-1</gml:Y></gml:coord> | ||
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord> | ||
</gml:Box> | ||
</gml:boundedBy> | ||
|
||
<gml:featureMember> | ||
<ogr:merge_lines fid="lines.1"> | ||
<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> | ||
</ogr:merge_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:merge_lines fid="lines.2"> | ||
<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> | ||
</ogr:merge_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:merge_lines fid="lines.3"> | ||
</ogr:merge_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:merge_lines fid="lines.4"> | ||
<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> | ||
</ogr:merge_lines> | ||
</gml:featureMember> | ||
</ogr:FeatureCollection> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<GMLFeatureClassList> | ||
<GMLFeatureClass> | ||
<Name>multilines</Name> | ||
<ElementPath>multilines</ElementPath> | ||
<GeometryType>5</GeometryType> | ||
<SRSName>EPSG:4326</SRSName> | ||
<DatasetSpecificInfo> | ||
<FeatureCount>4</FeatureCount> | ||
<ExtentXMin>-1.00000</ExtentXMin> | ||
<ExtentXMax>5.58042</ExtentXMax> | ||
<ExtentYMin>-1.00000</ExtentYMin> | ||
<ExtentYMax>4.11977</ExtentYMax> | ||
</DatasetSpecificInfo> | ||
</GMLFeatureClass> | ||
</GMLFeatureClassList> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ogr:FeatureCollection | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="" | ||
xmlns:ogr="http://ogr.maptools.org/" | ||
xmlns:gml="http://www.opengis.net/gml"> | ||
<gml:boundedBy> | ||
<gml:Box> | ||
<gml:coord><gml:X>-1</gml:X><gml:Y>-1</gml:Y></gml:coord> | ||
<gml:coord><gml:X>5.58042226487524</gml:X><gml:Y>4.119769673704415</gml:Y></gml:coord> | ||
</gml:Box> | ||
</gml:boundedBy> | ||
|
||
<gml:featureMember> | ||
<ogr:multilines fid="lines.1"> | ||
<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> | ||
</ogr:multilines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:multilines fid="lines.2"> | ||
<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> | ||
</ogr:multilines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:multilines fid="lines.3"> | ||
</ogr:multilines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:multilines fid="lines.4"> | ||
<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> | ||
</ogr:multilines> | ||
</gml:featureMember> | ||
</ogr:FeatureCollection> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters