QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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.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 
47 class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
48 {
49  public:
50 
53  {
54  NoError = 0,
66  };
67 
81  static ExportError exportLayer( QgsVectorLayer *layer,
82  const QString &uri,
83  const QString &providerKey,
84  const QgsCoordinateReferenceSystem &destCRS,
85  bool onlySelected = false,
86  QString *errorMessage SIP_OUT = nullptr,
87  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
88  QgsFeedback *feedback = nullptr
89  );
90 
103  QgsVectorLayerExporter( const QString &uri,
104  const QString &provider,
105  const QgsFields &fields,
106  QgsWkbTypes::Type geometryType,
108  bool overwrite = false,
109  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
110  QgsFeatureSink::SinkFlags sinkFlags = nullptr );
111 
113  QgsVectorLayerExporter( const QgsVectorLayerExporter &rh ) = delete;
115  QgsVectorLayerExporter &operator=( const QgsVectorLayerExporter &rh ) = delete;
116 
122  ExportError errorCode() const;
123 
129  QString errorMessage() const;
130 
136  int errorCount() const { return mErrorCount; }
137 
138  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
139  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) 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 
167 #ifdef SIP_RUN
169 #endif
170 
171 };
172 
173 
184 class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
185 {
186  Q_OBJECT
187 
188  public:
189 
197  const QString &uri,
198  const QString &providerKey,
199  const QgsCoordinateReferenceSystem &destinationCrs,
200  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
201  bool ownsLayer = false );
202 
209  static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
210  const QString &uri,
211  const QString &providerKey,
212  const QgsCoordinateReferenceSystem &destinationCrs,
213  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
214 
215  void cancel() override;
216 
217  signals:
218 
222  void exportComplete();
223 
228  void errorOccurred( int error, const QString &errorMessage );
229 
230  protected:
231 
232  bool run() override;
233  void finished( bool result ) override;
234 
235  private:
236 
237  QPointer< QgsVectorLayer > mLayer = nullptr;
238  bool mOwnsLayer = false;
239 
240  QString mDestUri;
241  QString mDestProviderKey;
243  QMap<QString, QVariant> mOptions;
244 
245  std::unique_ptr< QgsFeedback > mOwnedFeedback;
246 
248  QString mErrorMessage;
249 
250 };
251 
252 #endif // QGSVECTORLAYEREXPORTER_H
Source layer has an attribute type which could not be handled by destination.
Could not access newly created destination layer.
int errorCount() const
Returns the number of error messages encountered during the export.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
An interface for objects which accept features via addFeature(s) methods.
Container of fields for a vector layer.
Definition: qgsfields.h:42
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:44
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
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.
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr)
Adds a single feature to the sink.
#define SIP_FACTORY
Definition: qgis_sip.h:69
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 addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr)=0
Adds a list of features to the sink.
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.
#define SIP_OUT
Definition: qgis_sip.h:51
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.
Represents a vector layer which manages a vector based data sets.
Provider does not support creation of empty layers.