QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
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 
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
QgsVectorLayerExporter::ErrProviderUnsupportedFeature
@ ErrProviderUnsupportedFeature
Provider does not support creation of empty layers.
Definition: qgsvectorlayerexporter.h:62
QgsVectorLayerExporterTask::errorOccurred
void errorOccurred(int error, const QString &errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
qgstaskmanager.h
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsFeatureSink::addFeature
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
Definition: qgsfeaturesink.cpp:20
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
qgsfeaturesink.h
QgsVectorLayerExporter::errorCount
int errorCount() const
Returns the number of error messages encountered during the export.
Definition: qgsvectorlayerexporter.h:135
qgsfeature.h
QgsVectorLayerExporter::ErrFeatureWriteFailed
@ ErrFeatureWriteFailed
An error occurred while writing a feature to the destination.
Definition: qgsvectorlayerexporter.h:59
QgsVectorLayerExporterTask::exportComplete
void exportComplete()
Emitted when exporting the layer is successfully completed.
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
QgsVectorLayerExporter::NoError
@ NoError
No errors were encountered.
Definition: qgsvectorlayerexporter.h:53
QgsTask::finished
virtual void finished(bool result)
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
Definition: qgstaskmanager.h:280
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition: qgstaskmanager.cpp:91
QgsVectorLayerExporter::ErrCreateDataSource
@ ErrCreateDataSource
Could not create the destination data source.
Definition: qgsvectorlayerexporter.h:54
QgsVectorLayerExporterTask
QgsTask task which performs a QgsVectorLayerExporter layer export operation as a background task.
Definition: qgsvectorlayerexporter.h:188
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsVectorLayerExporter::QgsVectorLayerExporter
QgsVectorLayerExporter(const QgsVectorLayerExporter &rh)=delete
QgsVectorLayerExporter cannot be copied.
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
QgsVectorLayerExporter::ErrInvalidLayer
@ ErrInvalidLayer
Could not access newly created destination layer.
Definition: qgsvectorlayerexporter.h:60
QgsVectorLayerExporter::ErrUserCanceled
@ ErrUserCanceled
User canceled the export.
Definition: qgsvectorlayerexporter.h:64
QgsVectorLayerExporter::ErrCreateLayer
@ ErrCreateLayer
Could not create destination layer.
Definition: qgsvectorlayerexporter.h:55
QgsVectorLayerExporter::ErrInvalidProvider
@ ErrInvalidProvider
Could not find a matching provider key.
Definition: qgsvectorlayerexporter.h:61
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
qgsvectorlayer.h
QgsVectorLayerExporter::ExportError
ExportError
Error codes.
Definition: qgsvectorlayerexporter.h:52
QgsVectorLayerExporter::ErrAttributeTypeUnsupported
@ ErrAttributeTypeUnsupported
Source layer has an attribute type which could not be handled by destination.
Definition: qgsvectorlayerexporter.h:56
QgsFeatureSink::lastError
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
Definition: qgsfeaturesink.h:115
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsVectorLayerExporter::ErrConnectionFailed
@ ErrConnectionFailed
Could not connect to destination.
Definition: qgsvectorlayerexporter.h:63
QgsFeatureSink::flushBuffer
virtual bool flushBuffer()
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
Definition: qgsfeaturesink.h:108
QgsVectorLayerExporter
A convenience class for exporting vector layers to a destination data provider.
Definition: qgsvectorlayerexporter.h:47
QgsFeatureSink::addFeatures
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
QgsVectorDataProvider
This is the base class for vector data providers.
Definition: qgsvectordataprovider.h:59
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsVectorLayerExporter::ErrProjection
@ ErrProjection
An error occurred while reprojecting features to destination CRS.
Definition: qgsvectorlayerexporter.h:58
QgsVectorLayerExporter::operator=
QgsVectorLayerExporter & operator=(const QgsVectorLayerExporter &rh)=delete
QgsVectorLayerExporter cannot be copied.
qgsfeedback.h
QgsTask::run
virtual bool run()=0
Performs the task's operation.
QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.
Definition: qgsfeaturesink.h:34
QgsVectorLayerExporter::ErrAttributeCreationFailed
@ ErrAttributeCreationFailed
Destination provider was unable to create an attribute.
Definition: qgsvectorlayerexporter.h:57
QgsTask
Abstract base class for long running background tasks.
Definition: qgstaskmanager.h:53