QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsalgorithmmergevector.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmmergevector.cpp
3 ------------------
4 begin : December 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsvectorlayer.h"
20
22
23QString QgsMergeVectorAlgorithm::name() const
24{
25 return QStringLiteral( "mergevectorlayers" );
26}
27
28QString QgsMergeVectorAlgorithm::displayName() const
29{
30 return QObject::tr( "Merge vector layers" );
31}
32
33QStringList QgsMergeVectorAlgorithm::tags() const
34{
35 return QObject::tr( "vector,layers,collect,merge,combine" ).split( ',' );
36}
37
38QString QgsMergeVectorAlgorithm::group() const
39{
40 return QObject::tr( "Vector general" );
41}
42
43QString QgsMergeVectorAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeneral" );
46}
47
48void QgsMergeVectorAlgorithm::initAlgorithm( const QVariantMap & )
49{
50 addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ), Qgis::ProcessingSourceType::Vector ) );
51 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Destination CRS" ), QVariant(), true ) );
52 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Merged" ) ) );
53}
54
55QString QgsMergeVectorAlgorithm::shortDescription() const
56{
57 return QObject::tr( "Combines multiple vector layers of the same geometry type into a single one." );
58}
59
60QString QgsMergeVectorAlgorithm::shortHelpString() const
61{
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." );
70}
71
72Qgis::ProcessingAlgorithmDocumentationFlags QgsMergeVectorAlgorithm::documentationFlags() const
73{
75}
76
77QgsMergeVectorAlgorithm *QgsMergeVectorAlgorithm::createInstance() const
78{
79 return new QgsMergeVectorAlgorithm();
80}
81
82QVariantMap QgsMergeVectorAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
83{
84 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
85
86 QgsFields outputFields;
87 long totalFeatureCount = 0;
89 QgsCoordinateReferenceSystem outputCrs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
90
91 if ( outputCrs.isValid() )
92 feedback->pushInfo( QObject::tr( "Using specified destination CRS %1" ).arg( outputCrs.authid() ) );
93
94 bool errored = false;
95
96 // loop through input layers and determine geometry type, crs, fields, total feature count,...
97 long i = 0;
98 for ( QgsMapLayer *layer : layers )
99 {
100 i++;
101
102 if ( feedback->isCanceled() )
103 break;
104
105 if ( !layer )
106 {
107 feedback->pushDebugInfo( QObject::tr( "Error retrieving map layer." ) );
108 errored = true;
109 continue;
110 }
111
112 if ( layer->type() != Qgis::LayerType::Vector )
113 throw QgsProcessingException( QObject::tr( "All layers must be vector layers!" ) );
114
115 QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer );
116
117 const Qgis::WkbType layerWkbType = vl->wkbType();
118 const QgsCoordinateReferenceSystem layerCrs = vl->crs();
119 const QString layerName = vl->name();
120
121 if ( !outputCrs.isValid() && layerCrs.isValid() )
122 {
123 outputCrs = layerCrs;
124 feedback->pushInfo( QObject::tr( "Taking destination CRS %1 from layer" ).arg( outputCrs.authid() ) );
125 }
126
127 // check wkb type
128 if ( outputType != Qgis::WkbType::Unknown && outputType != Qgis::WkbType::NoGeometry )
129 {
130 if ( QgsWkbTypes::geometryType( outputType ) != QgsWkbTypes::geometryType( layerWkbType ) )
131 throw QgsProcessingException( QObject::tr( "All layers must have same geometry type! Encountered a %1 layer when expecting a %2 layer." )
134
135 if ( QgsWkbTypes::hasM( layerWkbType ) && !QgsWkbTypes::hasM( outputType ) )
136 {
137 outputType = QgsWkbTypes::addM( outputType );
138 feedback->pushInfo( QObject::tr( "Found a layer with M values, upgrading output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
139 }
140 if ( QgsWkbTypes::hasZ( layerWkbType ) && !QgsWkbTypes::hasZ( outputType ) )
141 {
142 outputType = QgsWkbTypes::addZ( outputType );
143 feedback->pushInfo( QObject::tr( "Found a layer with Z values, upgrading output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
144 }
145 if ( QgsWkbTypes::isMultiType( layerWkbType ) && !QgsWkbTypes::isMultiType( outputType ) )
146 {
147 outputType = QgsWkbTypes::multiType( outputType );
148 feedback->pushInfo( QObject::tr( "Found a layer with multiparts, upgrading output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
149 }
150 }
151 else
152 {
153 outputType = layerWkbType;
154 feedback->pushInfo( QObject::tr( "Setting output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
155 }
156
157 totalFeatureCount += vl->featureCount();
158
159 // check field type
160 for ( const QgsField &sourceField : vl->fields() )
161 {
162 bool found = false;
163 for ( QgsField &destField : outputFields )
164 {
165 if ( destField.name().compare( sourceField.name(), Qt::CaseInsensitive ) == 0 )
166 {
167 found = true;
168 if ( destField.type() != sourceField.type() )
169 {
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 );
177 }
178 break;
179 }
180 }
181
182 if ( !found )
183 outputFields.append( sourceField );
184 }
185 }
186
187 bool addLayerField = false;
188 if ( outputFields.lookupField( QStringLiteral( "layer" ) ) < 0 )
189 {
190 outputFields.append( QgsField( QStringLiteral( "layer" ), QMetaType::Type::QString, QString() ) );
191 addLayerField = true;
192 }
193 bool addPathField = false;
194 if ( outputFields.lookupField( QStringLiteral( "path" ) ) < 0 )
195 {
196 outputFields.append( QgsField( QStringLiteral( "path" ), QMetaType::Type::QString, QString() ) );
197 addPathField = true;
198 }
199
200 QString dest;
201 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, outputType, outputCrs, QgsFeatureSink::RegeneratePrimaryKey ) );
202 if ( !sink )
203 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
204
205 const bool hasZ = QgsWkbTypes::hasZ( outputType );
206 const bool hasM = QgsWkbTypes::hasM( outputType );
207 const bool isMulti = QgsWkbTypes::isMultiType( outputType );
208 const double step = totalFeatureCount > 0 ? 100.0 / totalFeatureCount : 1;
209 i = 0;
210 int layerNumber = 0;
211 for ( QgsMapLayer *layer : layers )
212 {
213 layerNumber++;
214 if ( !layer )
215 continue;
216
217 QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer );
218 if ( !vl )
219 continue;
220
221 const QString layerName = layer->name();
222 const QString layerSource = layer->publicSource();
223 const QgsFields layerFields = vl->fields();
224
225 feedback->pushInfo( QObject::tr( "Packaging layer %1/%2: %3" ).arg( layerNumber ).arg( layers.count() ).arg( layerName ) );
226
227 QgsFeatureIterator it = vl->getFeatures( QgsFeatureRequest().setDestinationCrs( outputCrs, context.transformContext() ) );
228 QgsFeature f;
229 while ( it.nextFeature( f ) )
230 {
231 if ( feedback->isCanceled() )
232 break;
233
234 // ensure feature geometry is of correct type
235 if ( f.hasGeometry() )
236 {
237 bool changed = false;
238 QgsGeometry g = f.geometry();
239 if ( hasZ && !g.constGet()->is3D() )
240 {
241 g.get()->addZValue( 0 );
242 changed = true;
243 }
244 if ( hasM && !g.constGet()->isMeasure() )
245 {
246 g.get()->addMValue( 0 );
247 changed = true;
248 }
249 if ( isMulti && !g.isMultipart() )
250 {
252 changed = true;
253 }
254 if ( changed )
255 f.setGeometry( g );
256 }
257
258 // process feature attributes
259 QgsAttributes destAttributes;
260 for ( const QgsField &destField : outputFields )
261 {
262 if ( addLayerField && destField.name() == QLatin1String( "layer" ) )
263 {
264 destAttributes.append( layerName );
265 continue;
266 }
267 else if ( addPathField && destField.name() == QLatin1String( "path" ) )
268 {
269 destAttributes.append( layerSource );
270 continue;
271 }
272
273 QVariant destAttribute;
274 const int sourceIndex = layerFields.lookupField( destField.name() );
275 if ( sourceIndex >= 0 )
276 {
277 destAttribute = f.attributes().at( sourceIndex );
278 }
279 destAttributes.append( destAttribute );
280 }
281 f.setAttributes( destAttributes );
282
283 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
284 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
285 i += 1;
286 feedback->setProgress( i * step );
287 }
288 }
289
290 if ( errored )
291 throw QgsProcessingException( QObject::tr( "Error obtained while merging one or more layers." ) );
292
293 sink->finalize();
294
295 QVariantMap outputs;
296 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
297 return outputs;
298}
299
@ 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.
@ Vector
Vector layer.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3367
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ NoGeometry
No geometry.
@ Unknown
Unknown.
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.
A vector of attributes.
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...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
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.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
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.
Definition qgsmaplayer.h:76
QString name
Definition qgsmaplayer.h:80
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
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