QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 "qgsfeature.h"
24 #include "qgsfeaturesink.h"
25 #include "qgstaskmanager.h"
26 #include "qgsfeedback.h"
27 #include "qgsvectorlayer.h"
28 
29 #include <QPointer>
30 
31 class QProgressDialog;
33 class QgsFields;
34 
48 class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
49 {
50  public:
51 
54  {
55  NoError = 0,
67  };
68 
82  static ExportError exportLayer( QgsVectorLayer *layer,
83  const QString &uri,
84  const QString &providerKey,
85  const QgsCoordinateReferenceSystem &destCRS,
86  bool onlySelected = false,
87  QString *errorMessage SIP_OUT = nullptr,
88  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
89  QgsFeedback *feedback = nullptr
90  );
91 
104  QgsVectorLayerExporter( const QString &uri,
105  const QString &provider,
106  const QgsFields &fields,
107  QgsWkbTypes::Type geometryType,
109  bool overwrite = false,
110  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
111  QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
112 
117 
123  ExportError errorCode() const;
124 
130  QString errorMessage() const;
131 
137  int errorCount() const { return mErrorCount; }
138 
139  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
140  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
141  QString lastError() const override;
142 
146  ~QgsVectorLayerExporter() override;
147 
148  bool flushBuffer() override;
149 
150  private:
151 
153  bool createSpatialIndex();
154 
156  ExportError mError;
157  QString mErrorMessage;
158 
159  int mErrorCount;
160 
161  QgsVectorDataProvider *mProvider = nullptr;
162 
164  QMap<int, int> mOldToNewAttrIdx;
165  int mAttributeCount;
166 
167  QgsFeatureList mFeatureBuffer;
168  int mFeatureBufferMemoryUsage = 0;
169 
170  bool mCreateSpatialIndex = true;
171 
172 #ifdef SIP_RUN
174 #endif
175 
176 };
177 
178 
189 class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
190 {
191  Q_OBJECT
192 
193  public:
194 
202  const QString &uri,
203  const QString &providerKey,
204  const QgsCoordinateReferenceSystem &destinationCrs,
205  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
206  bool ownsLayer = false );
207 
214  static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
215  const QString &uri,
216  const QString &providerKey,
217  const QgsCoordinateReferenceSystem &destinationCrs,
218  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
219 
220  void cancel() override;
221 
222  signals:
223 
228 
233  void errorOccurred( int error, const QString &errorMessage );
234 
235  protected:
236 
237  bool run() override;
238  void finished( bool result ) override;
239 
240  private:
241 
242  QPointer< QgsVectorLayer > mLayer = nullptr;
243  bool mOwnsLayer = false;
244 
245  QString mDestUri;
246  QString mDestProviderKey;
248  QMap<QString, QVariant> mOptions;
249 
250  std::unique_ptr< QgsFeedback > mOwnedFeedback;
251 
253  QString mErrorMessage;
254 
255 };
256 
257 #endif // QGSVECTORLAYEREXPORTER_H
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 id, geometry and a list of field/values...
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(int 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.
@ ErrUserCanceled
User canceled the export.
@ ErrCreateDataSource
Could not create the destination data source.
@ ErrInvalidProvider
Could not find a matching provider key.
@ ErrInvalidLayer
Could not access newly created destination layer.
@ ErrAttributeCreationFailed
Destination provider was unable to create an attribute.
@ ErrCreateLayer
Could not create destination layer.
@ ErrConnectionFailed
Could not connect to destination.
@ ErrProviderUnsupportedFeature
Provider does not support creation of empty layers.
@ ErrProjection
An error occurred while reprojecting features to destination CRS.
@ NoError
No errors were encountered.
@ ErrFeatureWriteFailed
An error occurred while writing a feature to the destination.
@ ErrAttributeTypeUnsupported
Source layer has an attribute type which could not be handled by destination.
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:614
const QgsCoordinateReferenceSystem & crs