QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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.h"
22#include "qgis_core.h"
23#include "qgis_sip.h"
24#include "qgsfeature.h"
25#include "qgsfeaturesink.h"
26#include "qgsfeedback.h"
28#include "qgstaskmanager.h"
29#include "qgsvectorlayer.h"
30
31#include <QPointer>
32
33class QProgressDialog;
35class QgsFields;
36
49class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
50{
51 public:
66 QgsVectorLayer *layer,
67 const QString &uri,
68 const QString &providerKey,
69 const QgsCoordinateReferenceSystem &destCRS,
70 bool onlySelected = false,
71 QString *errorMessage SIP_OUT = nullptr,
72 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
73 QgsFeedback *feedback = nullptr
74 );
75
76
83 struct CORE_EXPORT OutputField
84 {
88 OutputField( const QgsField &field, const QString &expression )
89 : field( field )
91 {}
92
96 QString expression;
97 };
98
105 class CORE_EXPORT ExportOptions
106 {
107 public:
117 void setSelectedOnly( bool selected ) { mSelectedOnly = selected; }
118
128 bool selectedOnly() const { return mSelectedOnly; }
129
135 void setTransformContext( const QgsCoordinateTransformContext &context );
136
142 QgsCoordinateTransformContext transformContext() const;
143
149 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
150
156 QgsCoordinateReferenceSystem destinationCrs() const;
157
167 void setExtent( const QgsReferencedRectangle &extent );
168
178 QgsReferencedRectangle extent() const;
179
192 void setFilterExpression( const QString &expression );
193
206 QString filterExpression() const;
207
213 void setExpressionContext( const QgsExpressionContext &context );
214
220 const QgsExpressionContext &expressionContext() const;
221
231 QList<QgsVectorLayerExporter::OutputField> outputFields() const;
232
242 void setOutputFields( const QList<QgsVectorLayerExporter::OutputField> &fields );
243
244 private:
245 bool mSelectedOnly = false;
246
247 QgsCoordinateReferenceSystem mDestinationCrs;
248
249 QgsCoordinateTransformContext mTransformContext;
250
252
253 QString mFilterExpression;
254 QgsExpressionContext mExpressionContext;
255
256 QList< QgsVectorLayerExporter::OutputField > mOutputFields;
257 };
258
273 static Qgis::VectorExportResult exportLayer(
274 QgsVectorLayer *layer,
275 const QString &uri,
276 const QString &providerKey,
277 const QgsVectorLayerExporter::ExportOptions &exportOptions,
278 QString *errorMessage SIP_OUT = nullptr,
279 const QMap<QString, QVariant> &providerOptions = QMap<QString, QVariant>(),
280 QgsFeedback *feedback = nullptr
281 );
282
296 const QString &uri,
297 const QString &provider,
298 const QgsFields &fields,
299 Qgis::WkbType geometryType,
301 bool overwrite = false,
302 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
304 );
305
308
315
321 QString errorMessage() const;
322
328 int errorCount() const { return mErrorCount; }
329
335 Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const;
336
338 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
339 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
340 QString lastError() const override;
341
345 ~QgsVectorLayerExporter() override;
346
347 bool flushBuffer() override;
348
349 private:
351 bool createSpatialIndex();
352
355 QString mErrorMessage;
356
357 int mErrorCount = 0;
358
359 QgsVectorDataProvider *mProvider = nullptr;
360
362 QMap<int, int> mOldToNewAttrIdx;
363 int mAttributeCount = -1;
364
365 QgsFeatureList mFeatureBuffer;
366 int mFeatureBufferMemoryUsage = 0;
367
368 bool mCreateSpatialIndex = true;
369
370#ifdef SIP_RUN
372#endif
373};
374
375
387class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
388{
389 Q_OBJECT
390
391 public:
399 QgsVectorLayer *layer,
400 const QString &uri,
401 const QString &providerKey,
402 const QgsCoordinateReferenceSystem &destinationCrs,
403 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
404 bool ownsLayer = false
405 );
406
417 QgsVectorLayer *layer,
418 const QString &uri,
419 const QString &providerKey,
420 const QgsVectorLayerExporter::ExportOptions &exportOptions,
421 const QMap<QString, QVariant> &providerOptions = QMap<QString, QVariant>(),
422 bool ownsLayer = false
423 );
424
433 const QString &uri,
434 const QString &providerKey,
435 const QgsCoordinateReferenceSystem &destinationCrs,
436 const QMap<QString, QVariant> &options = QMap<QString, QVariant>()
437 ) SIP_FACTORY;
438
439 void cancel() override;
440
441 signals:
442
447
452 void errorOccurred( Qgis::VectorExportResult error, const QString &errorMessage );
453
454 protected:
455 bool run() override;
456 void finished( bool result ) override;
457
458 private:
459 QPointer< QgsVectorLayer > mLayer = nullptr;
460 bool mOwnsLayer = false;
461
462 QString mDestUri;
463 QString mDestProviderKey;
465 QMap<QString, QVariant> mOptions;
466
467 std::unique_ptr< QgsFeedback > mOwnedFeedback;
468
470 QString mErrorMessage;
471};
472
473#endif // QGSVECTORLAYEREXPORTER_H
VectorExportResult
Vector layer export result codes.
Definition qgis.h:1078
@ Success
No errors were encountered.
Definition qgis.h:1079
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
Definition qgis.h:628
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An interface for objects which accept features via addFeature(s) methods.
QFlags< SinkFlag > SinkFlags
virtual bool flushBuffer()
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
QFlags< Flag > Flags
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
A QgsRectangle with associated coordinate reference system.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
Base class for vector data providers.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
QgsVectorLayerExporterTask(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), bool ownsLayer=false)
Constructor for QgsVectorLayerExporterTask.
void exportComplete()
Emitted when exporting the layer is successfully completed.
void errorOccurred(Qgis::VectorExportResult error, const QString &errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
void cancel() override
Notifies the task that it should terminate.
static QgsVectorLayerExporterTask * withLayerOwnership(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap< QString, QVariant > &options=QMap< QString, QVariant >())
Creates a new QgsVectorLayerExporterTask which has ownership over a source layer.
bool run() override
Performs the task's operation.
Encapsulates options for use with QgsVectorLayerExporter.
void setSelectedOnly(bool selected)
Sets whether the export should only include selected features.
bool selectedOnly() const
Returns whether the export will only include selected features.
A convenience class for exporting vector layers to a destination data provider.
QgsVectorLayerExporter & operator=(const QgsVectorLayerExporter &rh)=delete
static Qgis::VectorExportResult exportLayer(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destCRS, bool onlySelected=false, QString *errorMessage=nullptr, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), QgsFeedback *feedback=nullptr)
Writes the contents of vector layer to a different datasource.
int errorCount() const
Returns the number of error messages encountered during the export.
QString errorMessage() const
Returns any error message encountered during the export.
QgsVectorLayerExporter(const QgsVectorLayerExporter &rh)=delete
Qgis::VectorExportResult errorCode() const
Returns any encountered error code, or false if no error was encountered.
QgsVectorLayerExporter(const QString &uri, const QString &provider, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, bool overwrite=false, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags())
Constructor for QgsVectorLayerExporter.
Represents a vector layer which manages a vector based dataset.
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_FACTORY
Definition qgis_sip.h:83
QList< QgsFeature > QgsFeatureList
QString expression
The expression for the exported field from the source fields.
OutputField(const QgsField &field, const QString &expression)
Constructor for OutputField, with the specified field definition and source expression.
QgsField field
Destination field definition.