Skip to content

Commit

Permalink
Add option to include 'layer' and 'path' attributes in Merge Vector L…
Browse files Browse the repository at this point in the history
…ayers tool (qgis#55397)

Add option to include layer name and path in 'Merge Vector Layers' tool.The option is enabled by default to maintain backward compatibility.This modification addresses issue qgis#55397 by providing users with greater control over the attributes included in the merged output, enhancing the flexibility and usability of the tool.

Fixes qgis#55397
  • Loading branch information
lanckmann authored Dec 9, 2024
1 parent d5b9335 commit 11dda0d
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/analysis/processing/qgsalgorithmmergevector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "qgsalgorithmmergevector.h"
#include "qgsvectorlayer.h"

#include "qgsprocessingparameters.h"
///@cond PRIVATE

QString QgsMergeVectorAlgorithm::name() const
Expand Down Expand Up @@ -50,6 +50,9 @@ void QgsMergeVectorAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ), Qgis::ProcessingSourceType::Vector ) );
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Destination CRS" ), QVariant(), true ) );
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Merged" ) ) );

// new boolean parameter to add source layer information
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "ADD_SOURCE_FIELDS" ), QObject::tr( "Add source layer information (layer name and path)" ), true ) );
}

QString QgsMergeVectorAlgorithm::shortDescription() const
Expand All @@ -62,7 +65,7 @@ QString QgsMergeVectorAlgorithm::shortHelpString() const
return QObject::tr( "This algorithm combines multiple vector layers of the same geometry type into a single one.\n\n"
"The attribute table of the resulting layer will contain the fields from all input layers. "
"If fields with the same name but different types are found then the exported field will be automatically converted into a string type field. "
"New fields storing the original layer name and source are also added.\n\n"
"Optionally, new fields storing the original layer name and source can be added.\n\n"
"If any input layers contain Z or M values, then the output layer will also contain these values. Similarly, "
"if any of the input layers are multi-part, the output layer will also be a multi-part layer.\n\n"
"Optionally, the destination coordinate reference system (CRS) for the merged layer can be set. If it is not set, the CRS will be "
Expand All @@ -83,6 +86,8 @@ QVariantMap QgsMergeVectorAlgorithm::processAlgorithm( const QVariantMap &parame
{
const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );

const bool addSourceFields = parameterAsBool( parameters, QStringLiteral( "ADD_SOURCE_FIELDS" ), context );

QgsFields outputFields;
long totalFeatureCount = 0;
Qgis::WkbType outputType = Qgis::WkbType::Unknown;
Expand Down Expand Up @@ -184,16 +189,20 @@ QVariantMap QgsMergeVectorAlgorithm::processAlgorithm( const QVariantMap &parame
}

bool addLayerField = false;
if ( outputFields.lookupField( QStringLiteral( "layer" ) ) < 0 )
{
outputFields.append( QgsField( QStringLiteral( "layer" ), QMetaType::Type::QString, QString() ) );
addLayerField = true;
}
bool addPathField = false;
if ( outputFields.lookupField( QStringLiteral( "path" ) ) < 0 )
if ( addSourceFields ) // add source layer information
{
outputFields.append( QgsField( QStringLiteral( "path" ), QMetaType::Type::QString, QString() ) );
addPathField = true;
if ( outputFields.lookupField( QStringLiteral( "layer" ) ) < 0 )
{
outputFields.append( QgsField( QStringLiteral( "layer" ), QMetaType::Type::QString, QString() ) );
addLayerField = true;
}

if ( outputFields.lookupField( QStringLiteral( "path" ) ) < 0 )
{
outputFields.append( QgsField( QStringLiteral( "path" ), QMetaType::Type::QString, QString() ) );
addPathField = true;
}
}

QString dest;
Expand Down

0 comments on commit 11dda0d

Please sign in to comment.