Skip to content

Commit

Permalink
Make 'merge' algorithm store the original layer name and source
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 22, 2017
1 parent 00ead63 commit 33aa798
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -312,7 +312,7 @@ qgis:mergelines: >
qgis:mergevectorlayers: >
This algorithm combines multiple vector layers of the same geometry type into a single one.

If attributes tables are different, the attribute table of the resulting layer will contain the attributes from both input layers.
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.

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

Expand Down
17 changes: 17 additions & 0 deletions python/plugins/processing/algs/qgis/Merge.py
Expand Up @@ -30,6 +30,7 @@
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsFields,
QgsField,
QgsFeatureRequest,
QgsProcessingUtils,
QgsProcessingParameterMultipleLayers,
Expand Down Expand Up @@ -104,6 +105,15 @@ def processAlgorithm(self, parameters, context, feedback):
if not found:
fields.append(sfield)

add_layer_field = False
if fields.lookupField('layer') < 0:
fields.append(QgsField('layer', QVariant.String, '', 100))
add_layer_field = True
add_path_field = False
if fields.lookupField('path') < 0:
fields.append(QgsField('path', QVariant.String, '', 200))
add_path_field = True

total = 100.0 / totalFeatureCount
dest_crs = layers[0].crs()
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
Expand All @@ -118,6 +128,13 @@ def processAlgorithm(self, parameters, context, feedback):
sattributes = feature.attributes()
dattributes = []
for dindex, dfield in enumerate(fields):
if add_layer_field and dfield.name() == 'layer':
dattributes.append(layer.name())
continue
if add_path_field and dfield.name() == 'path':
dattributes.append(layer.publicSource())
continue

if (dfield.type() == QVariant.Int, QVariant.UInt, QVariant.LongLong, QVariant.ULongLong):
dattribute = 0
elif (dfield.type() == QVariant.Double):
Expand Down

0 comments on commit 33aa798

Please sign in to comment.