22 QString QgsMergeVectorAlgorithm::name()
const 24 return QStringLiteral(
"mergevectorlayers" );
27 QString QgsMergeVectorAlgorithm::displayName()
const 29 return QObject::tr(
"Merge vector layers" );
32 QStringList QgsMergeVectorAlgorithm::tags()
const 34 return QObject::tr(
"vector,layers,collect,merge,combine" ).split(
',' );
37 QString QgsMergeVectorAlgorithm::group()
const 39 return QObject::tr(
"Vector general" );
42 QString QgsMergeVectorAlgorithm::groupId()
const 44 return QStringLiteral(
"vectorgeneral" );
47 void QgsMergeVectorAlgorithm::initAlgorithm(
const QVariantMap & )
50 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"CRS" ), QObject::tr(
"Destination CRS" ), QVariant(),
true ) );
54 QString QgsMergeVectorAlgorithm::shortHelpString()
const 56 return QObject::tr(
"This algorithm combines multiple vector layers of the same geometry type into a single one.\n\n" 57 "If attributes tables are different, the attribute table of the resulting layer will contain the attributes " 58 "from all input layers. New attributes will be added for the original layer name and source.\n\n" 59 "If any input layers contain Z or M values, then the output layer will also contain these values. Similarly, " 60 "if any of the input layers are multi-part, the output layer will also be a multi-part layer.\n\n" 61 "Optionally, the destination coordinate reference system (CRS) for the merged layer can be set. If it is not set, the CRS will be " 62 "taken from the first input layer. All layers will all be reprojected to match this CRS." );
65 QgsMergeVectorAlgorithm *QgsMergeVectorAlgorithm::createInstance()
const 67 return new QgsMergeVectorAlgorithm();
72 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
75 long totalFeatureCount = 0;
80 feedback->
pushInfo( QObject::tr(
"Using specified destination CRS %1" ).arg( outputCrs.
authid() ) );
95 feedback->
pushDebugInfo( QObject::tr(
"Error retrieving map layer." ) );
107 outputCrs = vl->
crs();
108 feedback->
pushInfo( QObject::tr(
"Taking destination CRS %1 from layer" ).arg( outputCrs.
authid() ) );
115 throw QgsProcessingException( QObject::tr(
"All layers must have same geometry type! Encountered a %1 layer when expecting a %2 layer." )
147 for (
const QgsField &destField : outputFields )
149 if ( destField.name().compare( sourceField.name(), Qt::CaseInsensitive ) == 0 )
152 if ( destField.type() != sourceField.type() )
154 throw QgsProcessingException( QObject::tr(
"%1 field in layer %2 has different data type than in other layers (%3 instead of %4)" )
155 .arg( sourceField.name(), vl->
name(), sourceField.typeName(), destField.typeName() ) );
162 outputFields.append( sourceField );
166 bool addLayerField =
false;
167 if ( outputFields.lookupField( QStringLiteral(
"layer" ) ) < 0 )
169 outputFields.append(
QgsField( QStringLiteral(
"layer" ), QVariant::String, QString(), 100 ) );
170 addLayerField =
true;
172 bool addPathField =
false;
173 if ( outputFields.lookupField( QStringLiteral(
"path" ) ) < 0 )
175 outputFields.append(
QgsField( QStringLiteral(
"path" ), QVariant::String, QString(), 200 ) );
180 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, outputFields, outputType, outputCrs ) );
185 double step = totalFeatureCount > 0 ? 100.0 / totalFeatureCount : 1;
196 feedback->
pushInfo( QObject::tr(
"Packaging layer %1/%2: %3" ).arg( i ).arg( layers.count() ).arg( layer->name() ) );
208 bool changed =
false;
231 for (
const QgsField &destField : outputFields )
233 if ( addLayerField && destField.name() == QLatin1String(
"layer" ) )
235 destAttributes.append( layer->name() );
238 else if ( addPathField && destField.name() == QLatin1String(
"path" ) )
240 destAttributes.append( layer->publicSource() );
244 QVariant destAttribute;
246 if ( sourceIndex >= 0 )
248 destAttribute = f.
attributes().at( sourceIndex );
250 destAttributes.append( destAttribute );
264 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
bool isMeasure() const
Returns true if the geometry contains m values.
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
Wrapper for iterator of features from vector data provider or vector layer.
A parameter for processing algorithms which accepts multiple map layers.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for all map layer types.
static Type multiType(Type type)
Returns the multi type for a WKB type.
Base class for providing feedback from a processing algorithm.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
void setProgress(double progress)
Sets the current progress for the feedback object.
Container of fields for a vector layer.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
A feature sink output for processing algorithms.
Type
The WKB type describes the number of dimensions a geometry has.
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
QgsFields fields() const override
Returns the list of fields of this layer.
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
static QString geometryDisplayString(GeometryType type)
Return a display string for a geometry type.
QgsWkbTypes::Type wkbType() const override
Returns the WKBType or WKBUnknown in case of error.
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsCoordinateReferenceSystem crs() const
Returns the layer's spatial reference system.
Encapsulate a field in an attribute table or data source.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
A coordinate reference system parameter for processing algorithms.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
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 convertToMultiType()
Converts single type geometry into multitype geometry e.g.
bool isCanceled() const
Tells whether the operation has been canceled already.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
This class represents a coordinate reference system (CRS).
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
static bool hasM(Type type)
Tests whether a WKB type contains m values.
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
QString authid() const
Returns the authority identifier for the CRS.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.