QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
21#include "qgsvectorlayer.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsMergeVectorAlgorithm::name() const
30{
31 return u"mergevectorlayers"_s;
32}
33
34QString QgsMergeVectorAlgorithm::displayName() const
35{
36 return QObject::tr( "Merge vector layers" );
37}
38
39QStringList QgsMergeVectorAlgorithm::tags() const
40{
41 return QObject::tr( "vector,layers,collect,merge,combine" ).split( ',' );
42}
43
44QString QgsMergeVectorAlgorithm::group() const
45{
46 return QObject::tr( "Vector general" );
47}
48
49QString QgsMergeVectorAlgorithm::groupId() const
50{
51 return u"vectorgeneral"_s;
52}
53
54void QgsMergeVectorAlgorithm::initAlgorithm( const QVariantMap & )
55{
56 addParameter( new QgsProcessingParameterMultipleLayers( u"LAYERS"_s, QObject::tr( "Input layers" ), Qgis::ProcessingSourceType::Vector ) );
57 addParameter( new QgsProcessingParameterCrs( u"CRS"_s, QObject::tr( "Destination CRS" ), QVariant(), true ) );
58 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Merged" ) ) );
59
60 // new boolean parameter to add source layer information
61 addParameter( new QgsProcessingParameterBoolean( u"ADD_SOURCE_FIELDS"_s, QObject::tr( "Add source layer information (layer name and path)" ), true ) );
62}
63
64QString QgsMergeVectorAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Combines multiple vector layers of the same geometry type into a single one." );
67}
68
69QString QgsMergeVectorAlgorithm::shortHelpString() const
70{
71 return QObject::tr(
72 "This algorithm combines multiple vector layers of the same geometry type into a single one.\n\n"
73 "The attribute table of the resulting layer will contain the fields from all input layers. "
74 "If fields with the same name but different types are found then the exported field will be automatically converted into a string type field. "
75 "Optionally, new fields storing the original layer name and source can be added.\n\n"
76 "If any input layers contain Z or M values, then the output layer will also contain these values. Similarly, "
77 "if any of the input layers are multi-part, the output layer will also be a multi-part layer.\n\n"
78 "Optionally, the destination coordinate reference system (CRS) for the merged layer can be set. If it is not set, the CRS will be "
79 "taken from the first input layer. All layers will all be reprojected to match this CRS."
80 );
81}
82
83Qgis::ProcessingAlgorithmDocumentationFlags QgsMergeVectorAlgorithm::documentationFlags() const
84{
86}
87
88QgsMergeVectorAlgorithm *QgsMergeVectorAlgorithm::createInstance() const
89{
90 return new QgsMergeVectorAlgorithm();
91}
92
93QVariantMap QgsMergeVectorAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
94{
95 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u"LAYERS"_s, context );
96
97 const bool addSourceFields = parameterAsBool( parameters, u"ADD_SOURCE_FIELDS"_s, context );
98
99 QgsFields outputFields;
100 long totalFeatureCount = 0;
102 QgsCoordinateReferenceSystem outputCrs = parameterAsCrs( parameters, u"CRS"_s, context );
103
104 if ( outputCrs.isValid() )
105 feedback->pushInfo( QObject::tr( "Using specified destination CRS %1" ).arg( outputCrs.authid() ) );
106
107 bool errored = false;
108
109 // loop through input layers and determine geometry type, crs, fields, total feature count,...
110 long i = 0;
111 for ( QgsMapLayer *layer : layers )
112 {
113 i++;
114
115 if ( feedback->isCanceled() )
116 break;
117
118 if ( !layer )
119 {
120 feedback->pushDebugInfo( QObject::tr( "Error retrieving map layer." ) );
121 errored = true;
122 continue;
123 }
124
125 if ( layer->type() != Qgis::LayerType::Vector )
126 throw QgsProcessingException( QObject::tr( "All layers must be vector layers!" ) );
127
128 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
129
130 const Qgis::WkbType layerWkbType = vl->wkbType();
131 const QgsCoordinateReferenceSystem layerCrs = vl->crs();
132 const QString layerName = vl->name();
133
134 if ( !outputCrs.isValid() && layerCrs.isValid() )
135 {
136 outputCrs = layerCrs;
137 feedback->pushInfo( QObject::tr( "Taking destination CRS %1 from layer" ).arg( outputCrs.authid() ) );
138 }
139
140 // check wkb type
141 if ( outputType != Qgis::WkbType::Unknown && outputType != Qgis::WkbType::NoGeometry )
142 {
143 if ( QgsWkbTypes::geometryType( outputType ) != QgsWkbTypes::geometryType( layerWkbType ) )
145 QObject::tr( "All layers must have same geometry type! Encountered a %1 layer when expecting a %2 layer." )
147 );
148
149 if ( QgsWkbTypes::hasM( layerWkbType ) && !QgsWkbTypes::hasM( outputType ) )
150 {
151 outputType = QgsWkbTypes::addM( outputType );
152 feedback->pushInfo( QObject::tr( "Found a layer with M values, upgrading output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
153 }
154 if ( QgsWkbTypes::hasZ( layerWkbType ) && !QgsWkbTypes::hasZ( outputType ) )
155 {
156 outputType = QgsWkbTypes::addZ( outputType );
157 feedback->pushInfo( QObject::tr( "Found a layer with Z values, upgrading output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
158 }
159 if ( QgsWkbTypes::isMultiType( layerWkbType ) && !QgsWkbTypes::isMultiType( outputType ) )
160 {
161 outputType = QgsWkbTypes::multiType( outputType );
162 feedback->pushInfo( QObject::tr( "Found a layer with multiparts, upgrading output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
163 }
164 }
165 else
166 {
167 outputType = layerWkbType;
168 feedback->pushInfo( QObject::tr( "Setting output type to %1" ).arg( QgsWkbTypes::displayString( outputType ) ) );
169 }
170
171 totalFeatureCount += vl->featureCount();
172
173 // check field type
174 for ( const QgsField &sourceField : vl->fields() )
175 {
176 bool found = false;
177 for ( QgsField &destField : outputFields )
178 {
179 if ( destField.name().compare( sourceField.name(), Qt::CaseInsensitive ) == 0 )
180 {
181 found = true;
182 if ( destField.type() != sourceField.type() )
183 {
184 feedback->pushWarning(
185 QObject::tr(
186 "%1 field in layer %2 has different data type than the destination layer (%3 instead of %4). "
187 "%1 field will be converted to string type."
188 )
189 .arg( sourceField.name(), layerName, sourceField.typeName(), destField.typeName() )
190 );
191 destField.setType( QMetaType::Type::QString );
192 destField.setSubType( QMetaType::Type::UnknownType );
193 destField.setLength( 0 );
194 destField.setPrecision( 0 );
195 }
196 else if ( destField.type() == QMetaType::Type::QString && destField.length() < sourceField.length() )
197 {
198 feedback->pushWarning(
199 QObject::tr(
200 "%1 field in layer %2 has different field length than the destination layer (%3 vs %4). "
201 "%1 field length will be extended to match the larger of the two."
202 )
203 .arg( sourceField.name(), layerName, QString::number( sourceField.length() ), QString::number( destField.length() ) )
204 );
205 destField.setLength( sourceField.length() );
206 }
207 else if ( destField.type() == QMetaType::Type::Double && destField.precision() < sourceField.precision() )
208 {
209 feedback->pushWarning(
210 QObject::tr(
211 "%1 field in layer %2 has different field precision than the destination layer (%3 vs %4). "
212 "%1 field precision will be extended to match the larger of the two."
213 )
214 .arg( sourceField.name(), layerName, QString::number( sourceField.length() ), QString::number( destField.length() ) )
215 );
216 destField.setPrecision( sourceField.precision() );
217 }
218 break;
219 }
220 }
221
222 if ( !found )
223 outputFields.append( sourceField );
224 }
225 }
226
227 bool addLayerField = false;
228 bool addPathField = false;
229 if ( addSourceFields ) // add source layer information
230 {
231 if ( outputFields.lookupField( u"layer"_s ) < 0 )
232 {
233 outputFields.append( QgsField( u"layer"_s, QMetaType::Type::QString, QString() ) );
234 addLayerField = true;
235 }
236
237 if ( outputFields.lookupField( u"path"_s ) < 0 )
238 {
239 outputFields.append( QgsField( u"path"_s, QMetaType::Type::QString, QString() ) );
240 addPathField = true;
241 }
242 }
243
244 QString dest;
245 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, outputFields, outputType, outputCrs, QgsFeatureSink::RegeneratePrimaryKey ) );
246 if ( !sink )
247 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
248
249 const bool hasZ = QgsWkbTypes::hasZ( outputType );
250 const bool hasM = QgsWkbTypes::hasM( outputType );
251 const bool isMulti = QgsWkbTypes::isMultiType( outputType );
252 const double step = totalFeatureCount > 0 ? 100.0 / totalFeatureCount : 1;
253 i = 0;
254 int layerNumber = 0;
255 for ( QgsMapLayer *layer : layers )
256 {
257 layerNumber++;
258 if ( !layer )
259 continue;
260
261 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
262 if ( !vl )
263 continue;
264
265 const QString layerName = layer->name();
266 const QString layerSource = layer->publicSource();
267 const QgsFields layerFields = vl->fields();
268
269 feedback->pushInfo( QObject::tr( "Packaging layer %1/%2: %3" ).arg( layerNumber ).arg( layers.count() ).arg( layerName ) );
270
271 QgsFeatureIterator it = vl->getFeatures( QgsFeatureRequest().setDestinationCrs( outputCrs, context.transformContext() ) );
272 QgsFeature f;
273 while ( it.nextFeature( f ) )
274 {
275 if ( feedback->isCanceled() )
276 break;
277
278 // ensure feature geometry is of correct type
279 if ( f.hasGeometry() )
280 {
281 bool changed = false;
282 QgsGeometry g = f.geometry();
283 if ( hasZ && !g.constGet()->is3D() )
284 {
285 g.get()->addZValue( 0 );
286 changed = true;
287 }
288 if ( hasM && !g.constGet()->isMeasure() )
289 {
290 g.get()->addMValue( 0 );
291 changed = true;
292 }
293 if ( isMulti && !g.isMultipart() )
294 {
296 changed = true;
297 }
298 if ( changed )
299 f.setGeometry( g );
300 }
301
302 // process feature attributes
303 QgsAttributes destAttributes;
304 for ( const QgsField &destField : outputFields )
305 {
306 if ( addLayerField && destField.name() == "layer"_L1 )
307 {
308 destAttributes.append( layerName );
309 continue;
310 }
311 else if ( addPathField && destField.name() == "path"_L1 )
312 {
313 destAttributes.append( layerSource );
314 continue;
315 }
316
317 QVariant destAttribute;
318 const int sourceIndex = layerFields.lookupField( destField.name() );
319 if ( sourceIndex >= 0 )
320 {
321 destAttribute = f.attributes().at( sourceIndex );
322 }
323 destAttributes.append( destAttribute );
324 }
325 f.setAttributes( destAttributes );
326
327 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
328 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
329 i += 1;
330 feedback->setProgress( i * step );
331 }
332 }
333
334 if ( errored )
335 throw QgsProcessingException( QObject::tr( "Error obtained while merging one or more layers." ) );
336
337 sink->finalize();
338
339 QVariantMap outputs;
340 outputs.insert( u"OUTPUT"_s, dest );
341 return outputs;
342}
343
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3653
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
@ Vector
Vector layer.
Definition qgis.h:207
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3745
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ NoGeometry
No geometry.
Definition qgis.h:312
@ Unknown
Unknown.
Definition qgis.h:295
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.
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...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
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:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
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:83
QString name
Definition qgsmaplayer.h:87
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
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.