QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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"
29 
30 #include <QPointer>
31 
32 class QProgressDialog;
34 class QgsFields;
35 
49 class 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 
88  QgsVectorLayerExporter( const QString &uri,
89  const QString &provider,
90  const QgsFields &fields,
91  QgsWkbTypes::Type geometryType,
93  bool overwrite = false,
94  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
95  QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
96 
101 
107  Qgis::VectorExportResult errorCode() const;
108 
114  QString errorMessage() const;
115 
121  int errorCount() const { return mErrorCount; }
122 
123  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
124  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
125  QString lastError() const override;
126 
130  ~QgsVectorLayerExporter() override;
131 
132  bool flushBuffer() override;
133 
134  private:
135 
137  bool createSpatialIndex();
138 
141  QString mErrorMessage;
142 
143  int mErrorCount;
144 
145  QgsVectorDataProvider *mProvider = nullptr;
146 
148  QMap<int, int> mOldToNewAttrIdx;
149  int mAttributeCount;
150 
151  QgsFeatureList mFeatureBuffer;
152  int mFeatureBufferMemoryUsage = 0;
153 
154  bool mCreateSpatialIndex = true;
155 
156 #ifdef SIP_RUN
158 #endif
159 
160 };
161 
162 
173 class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
174 {
175  Q_OBJECT
176 
177  public:
178 
186  const QString &uri,
187  const QString &providerKey,
188  const QgsCoordinateReferenceSystem &destinationCrs,
189  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
190  bool ownsLayer = false );
191 
198  static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
199  const QString &uri,
200  const QString &providerKey,
201  const QgsCoordinateReferenceSystem &destinationCrs,
202  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
203 
204  void cancel() override;
205 
206  signals:
207 
212 
217  void errorOccurred( Qgis::VectorExportResult error, const QString &errorMessage );
218 
219  protected:
220 
221  bool run() override;
222  void finished( bool result ) override;
223 
224  private:
225 
226  QPointer< QgsVectorLayer > mLayer = nullptr;
227  bool mOwnsLayer = false;
228 
229  QString mDestUri;
230  QString mDestProviderKey;
232  QMap<QString, QVariant> mOptions;
233 
234  std::unique_ptr< QgsFeedback > mOwnedFeedback;
235 
236  Qgis::VectorExportResult mError = Qgis::VectorExportResult::Success;
237  QString mErrorMessage;
238 
239 };
240 
241 #endif // QGSVECTORLAYEREXPORTER_H
VectorExportResult
Vector layer export result codes.
Definition: qgis.h:361
This class represents a coordinate reference system (CRS).
An interface for objects which accept features via addFeature(s) methods.
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.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Container of fields for a vector layer.
Definition: qgsfields.h:45
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).
A convenience class for exporting vector layers to a destination data provider.
int errorCount() const
Returns the number of error messages encountered during the export.
QgsVectorLayerExporter & operator=(const QgsVectorLayerExporter &rh)=delete
QgsVectorLayerExporter cannot be copied.
QgsVectorLayerExporter(const QgsVectorLayerExporter &rh)=delete
QgsVectorLayerExporter cannot be copied.
Represents a vector layer which manages a vector based data sets.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
#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
Definition: qgsfeature.h:882
const QgsCoordinateReferenceSystem & crs