QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
143  ~QgsVectorLayerExporter() override;
144 
145  bool flushBuffer() override;
146 
147  private:
148 
150  bool createSpatialIndex();
151 
153  ExportError mError;
154  QString mErrorMessage;
155 
156  int mErrorCount;
157 
158  QgsVectorDataProvider *mProvider = nullptr;
159 
161  QMap<int, int> mOldToNewAttrIdx;
162  int mAttributeCount;
163 
164  QgsFeatureList mFeatureBuffer;
165 
166 #ifdef SIP_RUN
168 #endif
169 
170 };
171 
172 
183 class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
184 {
185  Q_OBJECT
186 
187  public:
188 
196  const QString &uri,
197  const QString &providerKey,
198  const QgsCoordinateReferenceSystem &destinationCrs,
199  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
200  bool ownsLayer = false );
201 
208  static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
209  const QString &uri,
210  const QString &providerKey,
211  const QgsCoordinateReferenceSystem &destinationCrs,
212  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
213 
214  void cancel() override;
215 
216  signals:
217 
221  void exportComplete();
222 
227  void errorOccurred( int error, const QString &errorMessage );
228 
229  protected:
230 
231  bool run() override;
232  void finished( bool result ) override;
233 
234  private:
235 
236  QPointer< QgsVectorLayer > mLayer = nullptr;
237  bool mOwnsLayer = false;
238 
239  QString mDestUri;
240  QString mDestProviderKey;
242  QMap<QString, QVariant> mOptions;
243 
244  std::unique_ptr< QgsFeedback > mOwnedFeedback;
245 
247  QString mErrorMessage;
248 
249 };
250 
251 #endif // QGSVECTORLAYEREXPORTER_H
QgsVectorLayerExporter::ErrProviderUnsupportedFeature
@ ErrProviderUnsupportedFeature
Provider does not support creation of empty layers.
Definition: qgsvectorlayerexporter.h:62
qgstaskmanager.h
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsFeatureSink::addFeature
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
Definition: qgsfeaturesink.cpp:20
QgsFields
Definition: qgsfields.h:44
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
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
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
Definition: qgsvectorlayerexporter.h:183
QgsFeedback
Definition: qgsfeedback.h:43
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
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
Definition: qgscoordinatereferencesystem.h:206
qgsvectorlayer.h
QgsVectorLayerExporter::ExportError
ExportError
Error codes.
Definition: qgsvectorlayerexporter.h:51
QgsVectorLayerExporter::ErrAttributeTypeUnsupported
@ ErrAttributeTypeUnsupported
Source layer has an attribute type which could not be handled by destination.
Definition: qgsvectorlayerexporter.h:56
QgsVectorLayer
Definition: qgsvectorlayer.h:385
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:100
QgsVectorLayerExporter
Definition: qgsvectorlayerexporter.h:46
QgsFeatureSink::addFeatures
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
QgsVectorDataProvider
Definition: qgsvectordataprovider.h:58
QgsFeature
Definition: qgsfeature.h:55
QgsVectorLayerExporter::ErrProjection
@ ErrProjection
An error occurred while reprojecting features to destination CRS.
Definition: qgsvectorlayerexporter.h:58
qgsfeedback.h
QgsTask::run
virtual bool run()=0
Performs the task's operation.
QgsFeatureSink
Definition: qgsfeaturesink.h:33
QgsVectorLayerExporter::ErrAttributeCreationFailed
@ ErrAttributeCreationFailed
Destination provider was unable to create an attribute.
Definition: qgsvectorlayerexporter.h:57
QgsTask
Abstract base class for long running background tasks. Tasks can be controlled directly,...
Definition: qgstaskmanager.h:52