Skip to content
Permalink
Browse files

Port merge alg to new API

  • Loading branch information
nyalldawson committed Jun 11, 2017
1 parent ea18e8e commit 5ba0b5cd83a071b5e93c1cd2604d2f1f619bb6cf
@@ -30,7 +30,12 @@
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsFields,
QgsProcessingUtils)
QgsProcessingUtils,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer,
QgsMapLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
@@ -53,11 +58,12 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterMultipleInput(self.LAYERS,
self.tr('Layers to merge'),
datatype=dataobjects.TYPE_VECTOR_ANY))
self.addParameter(QgsProcessingParameterMultipleLayers(self.LAYERS,
self.tr('Layers to merge'),
QgsProcessingParameterDefinition.TypeVectorAny))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Merged')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Merged')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Merged')))

def name(self):
return 'mergevectorlayers'
@@ -66,13 +72,15 @@ def displayName(self):
return self.tr('Merge vector layers')

def processAlgorithm(self, parameters, context, feedback):
inLayers = self.getParameterValue(self.LAYERS)
input_layers = self.parameterAsLayerList(parameters, self.LAYERS, context)

layers = []
fields = QgsFields()
totalFeatureCount = 0
for layerSource in inLayers.split(';'):
layer = QgsProcessingUtils.mapLayerFromString(layerSource, context)
for layer in input_layers:
if layer.type() != QgsMapLayer.VectorLayer:
raise GeoAlgorithmExecutionException(
self.tr('All layers must be vector layers!'))

if (len(layers) > 0):
if (layer.wkbType() != layers[0].wkbType()):
@@ -96,12 +104,15 @@ def processAlgorithm(self, parameters, context, feedback):
fields.append(sfield)

total = 100.0 / totalFeatureCount
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layers[0].wkbType(),
layers[0].crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, layers[0].wkbType(), layers[0].crs())

featureCount = 0
for layer in layers:
for feature in layer.getFeatures():
if feedback.isCanceled():
break

sattributes = feature.attributes()
dattributes = []
for dindex, dfield in enumerate(fields):
@@ -123,8 +134,8 @@ def processAlgorithm(self, parameters, context, feedback):
dattributes.append(dattribute)

feature.setAttributes(dattributes)
writer.addFeature(feature)
sink.addFeature(feature)
featureCount += 1
feedback.setProgress(int(featureCount * total))

del writer
return {self.OUTPUT: dest_id}
@@ -98,7 +98,7 @@
# from .HubDistancePoints import HubDistancePoints
# from .HubDistanceLines import HubDistanceLines
# from .HubLines import HubLines
# from .Merge import Merge
from .Merge import Merge
# from .GeometryConvert import GeometryConvert
# from .ConcaveHull import ConcaveHull
# from .RasterLayerStatistics import RasterLayerStatistics
@@ -219,7 +219,7 @@ def getAlgs(self):
# DeleteDuplicateGeometries(), TextToFloat(),
# ExtractByAttribute(), SelectByAttribute(),
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(), Merge(),
# HubDistanceLines(), HubLines(),
# GeometryConvert(), FieldsCalculator(),
# SaveSelectedFeatures(), JoinAttributes(),
# Explode(), FieldsPyculator(),
@@ -270,7 +270,8 @@ def getAlgs(self):
DeleteColumn(),
ExtentFromLayer(),
ExtractByExpression(),
GridPolygon()
GridPolygon(),
Merge()
]

if hasPlotly:

0 comments on commit 5ba0b5c

Please sign in to comment.
You can’t perform that action at this time.