Skip to content

Commit

Permalink
Port merge lines algorithm to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 27, 2017
1 parent 95be6d1 commit 5585805
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 52 deletions.
57 changes: 19 additions & 38 deletions python/plugins/processing/algs/qgis/MergeLines.py
Expand Up @@ -27,23 +27,18 @@

import os

from qgis.core import QgsFeature, QgsFeatureSink, QgsProcessingUtils
from qgis.core import (QgsProcessing,
QgsWkbTypes)

from qgis.PyQt.QtGui import QIcon

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

class MergeLines(QgisAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
class MergeLines(QgisFeatureBasedAlgorithm):

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'to_lines.png'))
Expand All @@ -57,41 +52,27 @@ def group(self):
def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE]))
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Merged'), datatype=[dataobjects.TYPE_VECTOR_LINE]))

def name(self):
return 'mergelines'

def displayName(self):
return self.tr('Merge lines')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0

for current, inFeat in enumerate(features):
outFeat = QgsFeature()
attrs = inFeat.attributes()
outFeat.setAttributes(attrs)
def outputName(self):
return self.tr('Merged')

inGeom = inFeat.geometry()
if inGeom:
outGeom = inGeom.mergeLines()
if outGeom is None:
raise GeoAlgorithmExecutionException(
self.tr('Error merging lines'))
def outputType(self):
return QgsProcessing.TypeVectorLine

outFeat.setGeometry(outGeom)
def outputWkbType(self, input_wkb):
return QgsWkbTypes.MultiLineString

writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
def processFeature(self, feature, feedback):
input_geometry = feature.geometry()
if input_geometry:
output_geometry = input_geometry.mergeLines()
if not output_geometry:
feedback.reportError(self.tr('Error merging lines for feature {}').format(feature.id()))

del writer
feature.setGeometry(output_geometry)
return feature
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -73,6 +73,7 @@
from .LinesToPolygons import LinesToPolygons
from .MeanCoords import MeanCoords
from .Merge import Merge
from .MergeLines import MergeLines
from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
from .OffsetLine import OffsetLine
from .Orthogonalize import Orthogonalize
Expand Down Expand Up @@ -158,7 +159,6 @@
# from .DefineProjection import DefineProjection
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
# from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
# from .MergeLines import MergeLines
# from .PointsAlongGeometry import PointsAlongGeometry
# from .Relief import Relief
# from .IdwInterpolation import IdwInterpolation
Expand Down Expand Up @@ -213,7 +213,7 @@ def getAlgs(self):
# OrientedMinimumBoundingBox(),
# SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
# RectanglesOvalsDiamondsFixed(),
# PointsAlongGeometry(),
# Relief(),
# IdwInterpolation(), TinInterpolation(),
Expand Down Expand Up @@ -258,6 +258,7 @@ def getAlgs(self):
LinesToPolygons(),
MeanCoords(),
Merge(),
MergeLines(),
NearestNeighbourAnalysis(),
OffsetLine(),
Orthogonalize(),
Expand Down
24 changes: 12 additions & 12 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -389,18 +389,18 @@ tests:
# compare:
# geometry:
# precision: 7
#
# - algorithm: qgis:mergelines
# name: Merge lines algorithm
# params:
# INPUT_LAYER:
# name: multilines.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/merge_lines.gml
# type: vector
#

- algorithm: qgis:mergelines
name: Merge lines algorithm
params:
INPUT:
name: multilines.gml
type: vector
results:
OUTPUT:
name: expected/merge_lines.gml
type: vector

- algorithm: native:multiparttosingleparts
name: Multiparts to singleparts
params:
Expand Down

0 comments on commit 5585805

Please sign in to comment.