QGIS API Documentation  3.0.2-Girona (307d082)
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 
102  QgsVectorLayerExporter( const QString &uri,
103  const QString &provider,
104  const QgsFields &fields,
105  QgsWkbTypes::Type geometryType,
106  const QgsCoordinateReferenceSystem &crs,
107  bool overwrite = false,
108  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() );
109 
111  QgsVectorLayerExporter( const QgsVectorLayerExporter &rh ) = delete;
113  QgsVectorLayerExporter &operator=( const QgsVectorLayerExporter &rh ) = delete;
114 
120  ExportError errorCode() const;
121 
127  QString errorMessage() const;
128 
134  int errorCount() const { return mErrorCount; }
135 
136  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
137  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
138 
142  ~QgsVectorLayerExporter() override;
143 
144  bool flushBuffer() override;
145 
146  private:
147 
149  bool createSpatialIndex();
150 
152  ExportError mError;
153  QString mErrorMessage;
154 
155  int mErrorCount;
156 
157  QgsVectorDataProvider *mProvider = nullptr;
158 
160  QMap<int, int> mOldToNewAttrIdx;
161  int mAttributeCount;
162 
163  QgsFeatureList mFeatureBuffer;
164 
165 #ifdef SIP_RUN
167 #endif
168 
169 };
170 
171 
182 class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
183 {
184  Q_OBJECT
185 
186  public:
187 
195  const QString &uri,
196  const QString &providerKey,
197  const QgsCoordinateReferenceSystem &destinationCrs,
198  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
199  bool ownsLayer = false );
200 
207  static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
208  const QString &uri,
209  const QString &providerKey,
210  const QgsCoordinateReferenceSystem &destinationCrs,
211  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
212 
213  void cancel() override;
214 
215  signals:
216 
220  void exportComplete();
221 
226  void errorOccurred( int error, const QString &errorMessage );
227 
228  protected:
229 
230  bool run() override;
231  void finished( bool result ) override;
232 
233  private:
234 
235  QPointer< QgsVectorLayer > mLayer = nullptr;
236  bool mOwnsLayer = false;
237 
238  QString mDestUri;
239  QString mDestProviderKey;
241  QMap<QString, QVariant> mOptions;
242 
243  std::unique_ptr< QgsFeedback > mOwnedFeedback;
244 
246  QString mErrorMessage;
247 
248 };
249 
250 #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:549
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:62
Could not find a matching provider key.
Base class for feedback objects to be used for cancelation 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:67
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.
int errorCount() const
Returns the number of error messages encountered during the export.
#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.