Skip to content

Commit 33aa798

Browse files
committed
Make 'merge' algorithm store the original layer name and source
1 parent 00ead63 commit 33aa798

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ qgis:mergelines: >
312312
qgis:mergevectorlayers: >
313313
This algorithm combines multiple vector layers of the same geometry type into a single one.
314314

315-
If attributes tables are different, the attribute table of the resulting layer will contain the attributes from both input layers.
315+
If attributes tables are different, the attribute table of the resulting layer will contain the attributes from all input layers. New attributes will be added for the original layer name and source.
316316

317317
The layers will all be reprojected to match the coordinate reference system of the first input layer.
318318

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from qgis.PyQt.QtGui import QIcon
3131
from qgis.PyQt.QtCore import QVariant
3232
from qgis.core import (QgsFields,
33+
QgsField,
3334
QgsFeatureRequest,
3435
QgsProcessingUtils,
3536
QgsProcessingParameterMultipleLayers,
@@ -104,6 +105,15 @@ def processAlgorithm(self, parameters, context, feedback):
104105
if not found:
105106
fields.append(sfield)
106107

108+
add_layer_field = False
109+
if fields.lookupField('layer') < 0:
110+
fields.append(QgsField('layer', QVariant.String, '', 100))
111+
add_layer_field = True
112+
add_path_field = False
113+
if fields.lookupField('path') < 0:
114+
fields.append(QgsField('path', QVariant.String, '', 200))
115+
add_path_field = True
116+
107117
total = 100.0 / totalFeatureCount
108118
dest_crs = layers[0].crs()
109119
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
@@ -118,6 +128,13 @@ def processAlgorithm(self, parameters, context, feedback):
118128
sattributes = feature.attributes()
119129
dattributes = []
120130
for dindex, dfield in enumerate(fields):
131+
if add_layer_field and dfield.name() == 'layer':
132+
dattributes.append(layer.name())
133+
continue
134+
if add_path_field and dfield.name() == 'path':
135+
dattributes.append(layer.publicSource())
136+
continue
137+
121138
if (dfield.type() == QVariant.Int, QVariant.UInt, QVariant.LongLong, QVariant.ULongLong):
122139
dattribute = 0
123140
elif (dfield.type() == QVariant.Double):

0 commit comments

Comments
 (0)