23QString QgsMergeVectorAlgorithm::name()
const
25 return QStringLiteral(
"mergevectorlayers" );
28QString QgsMergeVectorAlgorithm::displayName()
const
30 return QObject::tr(
"Merge vector layers" );
33QStringList QgsMergeVectorAlgorithm::tags()
const
35 return QObject::tr(
"vector,layers,collect,merge,combine" ).split(
',' );
38QString QgsMergeVectorAlgorithm::group()
const
40 return QObject::tr(
"Vector general" );
43QString QgsMergeVectorAlgorithm::groupId()
const
45 return QStringLiteral(
"vectorgeneral" );
48void QgsMergeVectorAlgorithm::initAlgorithm(
const QVariantMap & )
51 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"CRS" ), QObject::tr(
"Destination CRS" ), QVariant(),
true ) );
55QString QgsMergeVectorAlgorithm::shortDescription()
const
57 return QObject::tr(
"Combines multiple vector layers of the same geometry type into a single one." );
60QString QgsMergeVectorAlgorithm::shortHelpString()
const
62 return QObject::tr(
"This algorithm combines multiple vector layers of the same geometry type into a single one.\n\n"
63 "The attribute table of the resulting layer will contain the fields from all input layers. "
64 "If fields with the same name but different types are found then the exported field will be automatically converted into a string type field. "
65 "New fields storing the original layer name and source are also added.\n\n"
66 "If any input layers contain Z or M values, then the output layer will also contain these values. Similarly, "
67 "if any of the input layers are multi-part, the output layer will also be a multi-part layer.\n\n"
68 "Optionally, the destination coordinate reference system (CRS) for the merged layer can be set. If it is not set, the CRS will be "
69 "taken from the first input layer. All layers will all be reprojected to match this CRS." );
77QgsMergeVectorAlgorithm *QgsMergeVectorAlgorithm::createInstance()
const
79 return new QgsMergeVectorAlgorithm();
84 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
87 long totalFeatureCount = 0;
107 feedback->
pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
119 const QString layerName = vl->
name();
131 throw QgsProcessingException( QObject::tr(
"All layers must have same geometry type! Encountered a %1 layer when expecting a %2 layer." )
153 outputType = layerWkbType;
160 for (
const QgsField &sourceField : vl->fields() )
163 for (
QgsField &destField : outputFields )
165 if ( destField.name().compare( sourceField.name(), Qt::CaseInsensitive ) == 0 )
168 if ( destField.type() != sourceField.type() )
170 feedback->
pushWarning( QObject::tr(
"%1 field in layer %2 has different data type than the destination layer (%3 instead of %4). "
171 "%1 field will be converted to string type." )
172 .arg( sourceField.name(), layerName, sourceField.typeName(), destField.typeName() ) );
173 destField.setType( QMetaType::Type::QString );
174 destField.setSubType( QMetaType::Type::UnknownType );
175 destField.setLength( 0 );
176 destField.setPrecision( 0 );
183 outputFields.
append( sourceField );
187 bool addLayerField =
false;
188 if ( outputFields.
lookupField( QStringLiteral(
"layer" ) ) < 0 )
190 outputFields.
append(
QgsField( QStringLiteral(
"layer" ), QMetaType::Type::QString, QString() ) );
191 addLayerField =
true;
193 bool addPathField =
false;
194 if ( outputFields.
lookupField( QStringLiteral(
"path" ) ) < 0 )
196 outputFields.
append(
QgsField( QStringLiteral(
"path" ), QMetaType::Type::QString, QString() ) );
208 const double step = totalFeatureCount > 0 ? 100.0 / totalFeatureCount : 1;
221 const QString layerName = layer->name();
222 const QString layerSource = layer->publicSource();
225 feedback->
pushInfo( QObject::tr(
"Packaging layer %1/%2: %3" ).arg( layerNumber ).arg( layers.count() ).arg( layerName ) );
237 bool changed =
false;
260 for (
const QgsField &destField : outputFields )
262 if ( addLayerField && destField.name() == QLatin1String(
"layer" ) )
264 destAttributes.append( layerName );
267 else if ( addPathField && destField.name() == QLatin1String(
"path" ) )
269 destAttributes.append( layerSource );
273 QVariant destAttribute;
274 const int sourceIndex = layerFields.
lookupField( destField.name() );
275 if ( sourceIndex >= 0 )
277 destAttribute = f.
attributes().at( sourceIndex );
279 destAttributes.append( destAttribute );
294 outputs.insert( QStringLiteral(
"OUTPUT" ), 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.
This class 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.
This class 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.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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 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 data sets.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
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 bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
static 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 QString geometryDisplayString(Qgis::GeometryType type)
Returns a display string for a geometry type.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
const QgsCoordinateReferenceSystem & outputCrs