QGIS API Documentation 3.43.0-Master (3ee7834ace6)
qgsvectorlayerexporter.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerexporter.h
3 -------------------
4 begin : Thu Aug 25 2011
5 copyright : (C) 2011 by Giuseppe Sucameli
6 email : brush.tyler at gmail.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
18#ifndef QGSVECTORLAYEREXPORTER_H
19#define QGSVECTORLAYEREXPORTER_H
20
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgis.h"
24#include "qgsfeature.h"
25#include "qgsfeaturesink.h"
26#include "qgstaskmanager.h"
27#include "qgsfeedback.h"
28#include "qgsvectorlayer.h"
30
31#include <QPointer>
32
33class QProgressDialog;
35class QgsFields;
36
49class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
50{
51 public:
52
53
67 static Qgis::VectorExportResult exportLayer( QgsVectorLayer *layer,
68 const QString &uri,
69 const QString &providerKey,
70 const QgsCoordinateReferenceSystem &destCRS,
71 bool onlySelected = false,
72 QString *errorMessage SIP_OUT = nullptr,
73 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
74 QgsFeedback *feedback = nullptr );
75
76
83 struct CORE_EXPORT OutputField
84 {
85
89 OutputField( const QgsField &field, const QString &expression )
90 : field( field )
91 , expression( expression )
92 {}
93
97 QString expression;
98 };
99
106 class CORE_EXPORT ExportOptions
107 {
108 public:
109
119 void setSelectedOnly( bool selected ) { mSelectedOnly = selected; }
120
130 bool selectedOnly() const { return mSelectedOnly; }
131
137 void setTransformContext( const QgsCoordinateTransformContext &context );
138
144 QgsCoordinateTransformContext transformContext() const;
145
151 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
152
158 QgsCoordinateReferenceSystem destinationCrs() const;
159
169 void setExtent( const QgsReferencedRectangle &extent );
170
180 QgsReferencedRectangle extent() const;
181
194 void setFilterExpression( const QString &expression );
195
208 QString filterExpression() const;
209
215 void setExpressionContext( const QgsExpressionContext &context );
216
222 const QgsExpressionContext &expressionContext() const;
223
233 QList<QgsVectorLayerExporter::OutputField> outputFields() const;
234
244 void setOutputFields( const QList<QgsVectorLayerExporter::OutputField> &fields );
245
246 private:
247
248 bool mSelectedOnly = false;
249
250 QgsCoordinateReferenceSystem mDestinationCrs;
251
252 QgsCoordinateTransformContext mTransformContext;
253
255
256 QString mFilterExpression;
257 QgsExpressionContext mExpressionContext;
258
259 QList< QgsVectorLayerExporter::OutputField > mOutputFields;
260
261 };
262
277 static Qgis::VectorExportResult exportLayer( QgsVectorLayer *layer,
278 const QString &uri,
279 const QString &providerKey,
280 const QgsVectorLayerExporter::ExportOptions &exportOptions,
281 QString *errorMessage SIP_OUT = nullptr,
282 const QMap<QString, QVariant> &providerOptions = QMap<QString, QVariant>(),
283 QgsFeedback *feedback = nullptr );
284
297 QgsVectorLayerExporter( const QString &uri,
298 const QString &provider,
299 const QgsFields &fields,
300 Qgis::WkbType geometryType,
302 bool overwrite = false,
303 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
305
308
314 Qgis::VectorExportResult errorCode() const;
315
321 QString errorMessage() const;
322
328 int errorCount() const { return mErrorCount; }
329
335 Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const;
336
337 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
338 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
339 QString lastError() const override;
340
344 ~QgsVectorLayerExporter() override;
345
346 bool flushBuffer() override;
347
348 private:
349
351 bool createSpatialIndex();
352
355 QString mErrorMessage;
356
357 int mErrorCount;
358
359 QgsVectorDataProvider *mProvider = nullptr;
360
362 QMap<int, int> mOldToNewAttrIdx;
363 int mAttributeCount;
364
365 QgsFeatureList mFeatureBuffer;
366 int mFeatureBufferMemoryUsage = 0;
367
368 bool mCreateSpatialIndex = true;
369
370#ifdef SIP_RUN
372#endif
373
374};
375
376
386class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
387{
388 Q_OBJECT
389
390 public:
391
399 const QString &uri,
400 const QString &providerKey,
401 const QgsCoordinateReferenceSystem &destinationCrs,
402 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
403 bool ownsLayer = false );
404
415 const QString &uri,
416 const QString &providerKey,
417 const QgsVectorLayerExporter::ExportOptions &exportOptions,
418 const QMap<QString, QVariant> &providerOptions = QMap<QString, QVariant>(),
419 bool ownsLayer = false );
420
427 static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
428 const QString &uri,
429 const QString &providerKey,
430 const QgsCoordinateReferenceSystem &destinationCrs,
431 const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
432
433 void cancel() override;
434
435 signals:
436
441
446 void errorOccurred( Qgis::VectorExportResult error, const QString &errorMessage );
447
448 protected:
449
450 bool run() override;
451 void finished( bool result ) override;
452
453 private:
454
455 QPointer< QgsVectorLayer > mLayer = nullptr;
456 bool mOwnsLayer = false;
457
458 QString mDestUri;
459 QString mDestProviderKey;
461 QMap<QString, QVariant> mOptions;
462
463 std::unique_ptr< QgsFeedback > mOwnedFeedback;
464
466 QString mErrorMessage;
467
468};
469
470#endif // QGSVECTORLAYEREXPORTER_H
VectorExportResult
Vector layer export result codes.
Definition qgis.h:1016
@ Success
No errors were encountered.
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
Definition qgis.h:566
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An interface for objects which accept features via addFeature(s) methods.
QFlags< SinkFlag > SinkFlags
virtual bool flushBuffer()
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
QFlags< Flag > Flags
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
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
A QgsRectangle with associated coordinate reference system.
Abstract base class for long running background tasks.
This is the base class for vector data providers.
QgsTask task which performs a QgsVectorLayerExporter layer export operation as a background task.
void exportComplete()
Emitted when exporting the layer is successfully completed.
void errorOccurred(Qgis::VectorExportResult error, const QString &errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
Encapsulates options for use with QgsVectorLayerExporter.
void setSelectedOnly(bool selected)
Sets whether the export should only include selected features.
bool selectedOnly() const
Returns whether the export will only include selected features.
A convenience class for exporting vector layers to a destination data provider.
QgsVectorLayerExporter & operator=(const QgsVectorLayerExporter &rh)=delete
int errorCount() const
Returns the number of error messages encountered during the export.
QgsVectorLayerExporter(const QgsVectorLayerExporter &rh)=delete
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:76
QList< QgsFeature > QgsFeatureList
const QgsCoordinateReferenceSystem & crs
Encapsulates output field definition.
QString expression
The expression for the exported field from the source fields.
OutputField(const QgsField &field, const QString &expression)
Constructor for OutputField, with the specified field definition and source expression.
QgsField field
Destination field definition.