Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to include 'layer' and 'path' attributes in Merge Vector Layers tool (#55397) #59704

Merged
merged 10 commits into from
Dec 9, 2024
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 @@ -185,16 +190,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
Loading