QGIS API Documentation  3.17.0-Master (a035f434f4)
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 class QProgressDialog;
31 class QgsFields;
32 
46 class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
47 {
48  public:
49 
52  {
53  NoError = 0,
65  };
66 
80  static ExportError exportLayer( QgsVectorLayer *layer,
81  const QString &uri,
82  const QString &providerKey,
83  const QgsCoordinateReferenceSystem &destCRS,
84  bool onlySelected = false,
85  QString *errorMessage SIP_OUT = nullptr,
86  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
87  QgsFeedback *feedback = nullptr
88  );
89 
102  QgsVectorLayerExporter( const QString &uri,
103  const QString &provider,
104  const QgsFields &fields,
105  QgsWkbTypes::Type geometryType,
107  bool overwrite = false,
108  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
109  QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
110 
112  QgsVectorLayerExporter( const QgsVectorLayerExporter &rh ) = delete;
114  QgsVectorLayerExporter &operator=( const QgsVectorLayerExporter &rh ) = delete;
115 
121  ExportError errorCode() const;
122 
128  QString errorMessage() const;
129 
135  int errorCount() const { return mErrorCount; }
136 
137  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
138  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
139  QString lastError() const override;
140 
144  ~QgsVectorLayerExporter() override;
145 
146  bool flushBuffer() override;
147 
148  private:
149 
151  bool createSpatialIndex();
152 
154  ExportError mError;
155  QString mErrorMessage;
156 
157  int mErrorCount;
158 
159  QgsVectorDataProvider *mProvider = nullptr;
160 
162  QMap<int, int> mOldToNewAttrIdx;
163  int mAttributeCount;
164 
165  QgsFeatureList mFeatureBuffer;
166  int mFeatureBufferMemoryUsage = 0;
167 
168  bool mCreateSpatialIndex = true;
169 
170 #ifdef SIP_RUN
172 #endif
173 
174 };
175 
176 
187 class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
188 {
189  Q_OBJECT
190 
191  public:
192 
200  const QString &uri,
201  const QString &providerKey,
202  const QgsCoordinateReferenceSystem &destinationCrs,
203  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
204  bool ownsLayer = false );
205 
212  static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
213  const QString &uri,
214  const QString &providerKey,
215  const QgsCoordinateReferenceSystem &destinationCrs,
216  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
217 
218  void cancel() override;
219 
220  signals:
221 
225  void exportComplete();
226 
231  void errorOccurred( int error, const QString &errorMessage );
232 
233  protected:
234 
235  bool run() override;
236  void finished( bool result ) override;
237 
238  private:
239 
240  QPointer< QgsVectorLayer > mLayer = nullptr;
241  bool mOwnsLayer = false;
242 
243  QString mDestUri;
244  QString mDestProviderKey;
246  QMap<QString, QVariant> mOptions;
247 
248  std::unique_ptr< QgsFeedback > mOwnedFeedback;
249 
251  QString mErrorMessage;
252 
253 };
254 
255 #endif // QGSVECTORLAYEREXPORTER_H
Source layer has an attribute type which could not be handled by destination.
Could not access newly created destination layer.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
An interface for objects which accept features via addFeature(s) methods.
Container of fields for a vector layer.
Definition: qgsfields.h:44
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
Could not find a matching provider key.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:43
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
Destination provider was unable to create an attribute.
Abstract base class for long running background tasks.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Could not create the destination data source.
An error occurred while writing a feature to the destination.
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsTask task which performs a QgsVectorLayerExporter layer export operation as a background task...
An error occurred while reprojecting features to destination CRS.
A convenience class for exporting vector layers to a destination data provider.
virtual bool flushBuffer()
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
virtual void cancel()
Notifies the task that it should terminate.
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
int errorCount() const
Returns the number of error messages encountered during the export.
#define SIP_OUT
Definition: qgis_sip.h:58
This class represents a coordinate reference system (CRS).
virtual void finished(bool result)
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
virtual bool run()=0
Performs the task&#39;s operation.
No errors were encountered.
Could not create destination layer.
This is the base class for vector data providers.
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
Represents a vector layer which manages a vector based data sets.
Provider does not support creation of empty layers.