Make 'merge' algorithm store the original layer name and source

This commit is contained in:
Nyall Dawson 2017-06-22 18:18:22 +10:00
parent 00ead638ee
commit 33aa798c2a
2 changed files with 18 additions and 1 deletions

View File

@ -312,7 +312,7 @@ qgis:mergelines: >
qgis:mergevectorlayers: > qgis:mergevectorlayers: >
This algorithm combines multiple vector layers of the same geometry type into a single one. 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. The layers will all be reprojected to match the coordinate reference system of the first input layer.

View File

@ -30,6 +30,7 @@ import os
from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsFields, from qgis.core import (QgsFields,
QgsField,
QgsFeatureRequest, QgsFeatureRequest,
QgsProcessingUtils, QgsProcessingUtils,
QgsProcessingParameterMultipleLayers, QgsProcessingParameterMultipleLayers,
@ -104,6 +105,15 @@ class Merge(QgisAlgorithm):
if not found: if not found:
fields.append(sfield) 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 total = 100.0 / totalFeatureCount
dest_crs = layers[0].crs() dest_crs = layers[0].crs()
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
@ -118,6 +128,13 @@ class Merge(QgisAlgorithm):
sattributes = feature.attributes() sattributes = feature.attributes()
dattributes = [] dattributes = []
for dindex, dfield in enumerate(fields): 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): if (dfield.type() == QVariant.Int, QVariant.UInt, QVariant.LongLong, QVariant.ULongLong):
dattribute = 0 dattribute = 0
elif (dfield.type() == QVariant.Double): elif (dfield.type() == QVariant.Double):