25using namespace Qt::StringLiterals;
29QString QgsMergeVectorAlgorithm::name()
const
31 return u
"mergevectorlayers"_s;
34QString QgsMergeVectorAlgorithm::displayName()
const
36 return QObject::tr(
"Merge vector layers" );
39QStringList QgsMergeVectorAlgorithm::tags()
const
41 return QObject::tr(
"vector,layers,collect,merge,combine" ).split(
',' );
44QString QgsMergeVectorAlgorithm::group()
const
46 return QObject::tr(
"Vector general" );
49QString QgsMergeVectorAlgorithm::groupId()
const
51 return u
"vectorgeneral"_s;
54void QgsMergeVectorAlgorithm::initAlgorithm(
const QVariantMap & )
61 addParameter(
new QgsProcessingParameterBoolean( u
"ADD_SOURCE_FIELDS"_s, QObject::tr(
"Add source layer information (layer name and path)" ),
true ) );
64QString QgsMergeVectorAlgorithm::shortDescription()
const
66 return QObject::tr(
"Combines multiple vector layers of the same geometry type into a single one." );
69QString QgsMergeVectorAlgorithm::shortHelpString()
const
71 return QObject::tr(
"This algorithm combines multiple vector layers of the same geometry type into a single one.\n\n"
72 "The attribute table of the resulting layer will contain the fields from all input layers. "
73 "If fields with the same name but different types are found then the exported field will be automatically converted into a string type field. "
74 "Optionally, new fields storing the original layer name and source can be added.\n\n"
75 "If any input layers contain Z or M values, then the output layer will also contain these values. Similarly, "
76 "if any of the input layers are multi-part, the output layer will also be a multi-part layer.\n\n"
77 "Optionally, the destination coordinate reference system (CRS) for the merged layer can be set. If it is not set, the CRS will be "
78 "taken from the first input layer. All layers will all be reprojected to match this CRS." );
86QgsMergeVectorAlgorithm *QgsMergeVectorAlgorithm::createInstance()
const
88 return new QgsMergeVectorAlgorithm();
93 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
95 const bool addSourceFields = parameterAsBool( parameters, u
"ADD_SOURCE_FIELDS"_s, context );
98 long totalFeatureCount = 0;
103 feedback->
pushInfo( QObject::tr(
"Using specified destination CRS %1" ).arg( outputCrs.
authid() ) );
105 bool errored =
false;
118 feedback->
pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
130 const QString layerName = vl->
name();
134 outputCrs = layerCrs;
135 feedback->
pushInfo( QObject::tr(
"Taking destination CRS %1 from layer" ).arg( outputCrs.
authid() ) );
142 throw QgsProcessingException( QObject::tr(
"All layers must have same geometry type! Encountered a %1 layer when expecting a %2 layer." )
163 outputType = layerWkbType;
173 for (
QgsField &destField : outputFields )
175 if ( destField.name().compare( sourceField.name(), Qt::CaseInsensitive ) == 0 )
178 if ( destField.type() != sourceField.type() )
180 feedback->
pushWarning( QObject::tr(
"%1 field in layer %2 has different data type than the destination layer (%3 instead of %4). "
181 "%1 field will be converted to string type." )
182 .arg( sourceField.name(), layerName, sourceField.typeName(), destField.typeName() ) );
183 destField.setType( QMetaType::Type::QString );
184 destField.setSubType( QMetaType::Type::UnknownType );
185 destField.setLength( 0 );
186 destField.setPrecision( 0 );
188 else if ( destField.type() == QMetaType::Type::QString && destField.length() < sourceField.length() )
190 feedback->
pushWarning( QObject::tr(
"%1 field in layer %2 has different field length than the destination layer (%3 vs %4). "
191 "%1 field length will be extended to match the larger of the two." )
192 .arg( sourceField.name(), layerName, QString::number( sourceField.length() ), QString::number( destField.length() ) ) );
193 destField.setLength( sourceField.length() );
195 else if ( destField.type() == QMetaType::Type::Double && destField.precision() < sourceField.precision() )
197 feedback->
pushWarning( QObject::tr(
"%1 field in layer %2 has different field precision than the destination layer (%3 vs %4). "
198 "%1 field precision will be extended to match the larger of the two." )
199 .arg( sourceField.name(), layerName, QString::number( sourceField.length() ), QString::number( destField.length() ) ) );
200 destField.setPrecision( sourceField.precision() );
207 outputFields.append( sourceField );
211 bool addLayerField =
false;
212 bool addPathField =
false;
213 if ( addSourceFields )
215 if ( outputFields.lookupField( u
"layer"_s ) < 0 )
217 outputFields.append(
QgsField( u
"layer"_s, QMetaType::Type::QString, QString() ) );
218 addLayerField =
true;
221 if ( outputFields.lookupField( u
"path"_s ) < 0 )
223 outputFields.append(
QgsField( u
"path"_s, QMetaType::Type::QString, QString() ) );
236 const double step = totalFeatureCount > 0 ? 100.0 / totalFeatureCount : 1;
249 const QString layerName = layer->name();
250 const QString layerSource = layer->publicSource();
253 feedback->
pushInfo( QObject::tr(
"Packaging layer %1/%2: %3" ).arg( layerNumber ).arg( layers.count() ).arg( layerName ) );
265 bool changed =
false;
288 for (
const QgsField &destField : outputFields )
290 if ( addLayerField && destField.name() ==
"layer"_L1 )
292 destAttributes.append( layerName );
295 else if ( addPathField && destField.name() ==
"path"_L1 )
297 destAttributes.append( layerSource );
301 QVariant destAttribute;
302 const int sourceIndex = layerFields.
lookupField( destField.name() );
303 if ( sourceIndex >= 0 )
305 destAttribute = f.
attributes().at( sourceIndex );
307 destAttributes.append( destAttribute );
324 outputs.insert( u
"OUTPUT"_s, dest );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
WkbType
The WKB type describes the number of dimensions a geometry has.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
Base class for all map layer types.
QgsCoordinateReferenceSystem crs
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
A boolean parameter for processing algorithms.
A coordinate reference system parameter for processing algorithms.
A feature sink output for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
Represents a vector layer which manages a vector based dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Q_INVOKABLE QString geometryDisplayString(Qgis::GeometryType type)
Returns a display string for a geometry type.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.