Skip to content

Commit

Permalink
Port merge alg to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 11, 2017
1 parent ea18e8e commit 5ba0b5c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
35 changes: 23 additions & 12 deletions python/plugins/processing/algs/qgis/Merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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()):
Expand All @@ -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):
Expand All @@ -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}
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -270,7 +270,8 @@ def getAlgs(self):
DeleteColumn(),
ExtentFromLayer(),
ExtractByExpression(),
GridPolygon()
GridPolygon(),
Merge()
]

if hasPlotly:
Expand Down

0 comments on commit 5ba0b5c

Please sign in to comment.