QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgis.h"
24 #include "qgsfeature.h"
25 #include "qgsfeaturesink.h"
26 #include "qgstaskmanager.h"
27 #include "qgsfeedback.h"
28 #include "qgsvectorlayer.h"
29 
30 #include <QPointer>
31 
32 class QProgressDialog;
34 class QgsFields;
35 
49 class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
50 {
51  public:
52 
53 
67  static Qgis::VectorExportResult exportLayer( QgsVectorLayer *layer,
68  const QString &uri,
69  const QString &providerKey,
70  const QgsCoordinateReferenceSystem &destCRS,
71  bool onlySelected = false,
72  QString *errorMessage SIP_OUT = nullptr,
73  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
74  QgsFeedback *feedback = nullptr );
75 
88  QgsVectorLayerExporter( const QString &uri,
89  const QString &provider,
90  const QgsFields &fields,
91  QgsWkbTypes::Type geometryType,
93  bool overwrite = false,
94  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
95  QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
96 
98  QgsVectorLayerExporter( const QgsVectorLayerExporter &rh ) = delete;
100  QgsVectorLayerExporter &operator=( const QgsVectorLayerExporter &rh ) = delete;
101 
107  Qgis::VectorExportResult errorCode() const;
108 
114  QString errorMessage() const;
115 
121  int errorCount() const { return mErrorCount; }
122 
123  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
124  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
125  QString lastError() const override;
126 
130  ~QgsVectorLayerExporter() override;
131 
132  bool flushBuffer() override;
133 
134  private:
135 
137  bool createSpatialIndex();
138 
141  QString mErrorMessage;
142 
143  int mErrorCount;
144 
145  QgsVectorDataProvider *mProvider = nullptr;
146 
148  QMap<int, int> mOldToNewAttrIdx;
149  int mAttributeCount;
150 
151  QgsFeatureList mFeatureBuffer;
152  int mFeatureBufferMemoryUsage = 0;
153 
154  bool mCreateSpatialIndex = true;
155 
156 #ifdef SIP_RUN
158 #endif
159 
160 };
161 
162 
173 class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
174 {
175  Q_OBJECT
176 
177  public:
178 
186  const QString &uri,
187  const QString &providerKey,
188  const QgsCoordinateReferenceSystem &destinationCrs,
189  const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
190  bool ownsLayer = false );
191 
198  static QgsVectorLayerExporterTask *withLayerOwnership( QgsVectorLayer *layer SIP_TRANSFER,
199  const QString &uri,
200  const QString &providerKey,
201  const QgsCoordinateReferenceSystem &destinationCrs,
202  const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
203 
204  void cancel() override;
205 
206  signals:
207 
211  void exportComplete();
212 
217  void errorOccurred( Qgis::VectorExportResult error, const QString &errorMessage );
218 
219  protected:
220 
221  bool run() override;
222  void finished( bool result ) override;
223 
224  private:
225 
226  QPointer< QgsVectorLayer > mLayer = nullptr;
227  bool mOwnsLayer = false;
228 
229  QString mDestUri;
230  QString mDestProviderKey;
232  QMap<QString, QVariant> mOptions;
233 
234  std::unique_ptr< QgsFeedback > mOwnedFeedback;
235 
236  Qgis::VectorExportResult mError = Qgis::VectorExportResult::Success;
237  QString mErrorMessage;
238 
239 };
240 
241 #endif // QGSVECTORLAYEREXPORTER_H
Qgis::VectorExportResult
VectorExportResult
Vector layer export result codes.
Definition: qgis.h:449
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
Container of fields for a vector layer.
Definition: qgsfields.h:44
qgsfeaturesink.h
QgsVectorLayerExporter::errorCount
int errorCount() const
Returns the number of error messages encountered during the export.
Definition: qgsvectorlayerexporter.h:121
qgsfeature.h
qgis.h
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
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:284
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
QgsVectorLayerExporterTask
QgsTask task which performs a QgsVectorLayerExporter layer export operation as a background task....
Definition: qgsvectorlayerexporter.h:173
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
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
qgsvectorlayer.h
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:391
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:49
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:58
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
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:33
QgsTask
Abstract base class for long running background tasks. Tasks can be controlled directly,...
Definition: qgstaskmanager.h:54