QgsVectorLayerExporter class

A convenience class for exporting vector layers to a destination data provider.

QgsVectorLayerExporter can be used in two ways:

  1. Using a static call to QgsVectorLayerExporter::exportLayer(...) which exports the entire layer to the destination provider.
  2. Create an instance of the class and issue calls to addFeature(...)

Base classes

class QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.

Public types

enum ExportError { NoError = 0, ErrCreateDataSource, ErrCreateLayer, ErrAttributeTypeUnsupported, ErrAttributeCreationFailed, ErrProjection, ErrFeatureWriteFailed, ErrInvalidLayer, ErrInvalidProvider, ErrProviderUnsupportedFeature, ErrConnectionFailed, ErrUserCanceled }
Error codes.

Public static functions

static auto 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) -> ExportError
Writes the contents of vector layer to a different datasource.

Constructors, destructors, conversion operators

QgsVectorLayerExporter(const QString& uri, const QString& provider, const QgsFields& fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem& crs, bool overwrite = false, const QMap<QString, QVariant>& options = QMap<QString, QVariant>(), QgsFeatureSink::SinkFlags sinkFlags = nullptr)
Constructor for QgsVectorLayerExporter.
QgsVectorLayerExporter(const QgsVectorLayerExporter& rh) deleted
QgsVectorLayerExporter cannot be copied.
~QgsVectorLayerExporter() override
Finalizes the export and closes the new created layer.

Public functions

auto addFeature(QgsFeature& feature, QgsFeatureSink::Flags flags = nullptr) -> bool override
Adds a single feature to the sink.
auto addFeatures(QgsFeatureList& features, QgsFeatureSink::Flags flags = nullptr) -> bool override
Adds a list of features to the sink.
auto errorCode() const -> ExportError
Returns any encountered error code, or false if no error was encountered.
auto errorCount() const -> int
Returns the number of error messages encountered during the export.
auto errorMessage() const -> QString
Returns any error message encountered during the export.
auto flushBuffer() -> bool override
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to the sink's destination.
auto operator=(const QgsVectorLayerExporter& rh) -> QgsVectorLayerExporter& deleted
QgsVectorLayerExporter cannot be copied.

Enum documentation

enum QgsVectorLayerExporter::ExportError

Error codes.

Enumerators
NoError

No errors were encountered.

ErrCreateDataSource

Could not create the destination data source.

ErrCreateLayer

Could not create destination layer.

ErrAttributeTypeUnsupported

Source layer has an attribute type which could not be handled by destination.

ErrAttributeCreationFailed

Destination provider was unable to create an attribute.

ErrProjection

An error occurred while reprojecting features to destination CRS.

ErrFeatureWriteFailed

An error occurred while writing a feature to the destination.

ErrInvalidLayer

Could not access newly created destination layer.

ErrInvalidProvider

Could not find a matching provider key.

ErrProviderUnsupportedFeature

Provider does not support creation of empty layers.

ErrConnectionFailed

Could not connect to destination.

ErrUserCanceled

User canceled the export.

Function documentation

static ExportError QgsVectorLayerExporter::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.

Parameters
layer source layer
uri URI for destination data source
providerKey string key for destination data provider
destCRS destination CRS, or an invalid (default constructed) CRS if not available
onlySelected set to true to export only selected features
errorMessage if non-null, will be set to any error messages
options optional provider dataset options
feedback optional feedback object to show progress and cancelation of export
Returns NoError for a successful export, or encountered error

QgsVectorLayerExporter::QgsVectorLayerExporter(const QString& uri, const QString& provider, const QgsFields& fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem& crs, bool overwrite = false, const QMap<QString, QVariant>& options = QMap<QString, QVariant>(), QgsFeatureSink::SinkFlags sinkFlags = nullptr)

Constructor for QgsVectorLayerExporter.

Parameters
uri URI for destination data source
provider string key for destination data provider
fields fields to include in created layer
geometryType destination geometry type
crs desired CRS, or an invalid (default constructed) CRS if not available
overwrite set to true to overwrite any existing data source
options optional provider dataset options
sinkFlags for how to add features

bool QgsVectorLayerExporter::addFeature(QgsFeature& feature, QgsFeatureSink::Flags flags = nullptr) override

Adds a single feature to the sink.

Returns true in case of success and false in case of failure

Feature addition behavior is controlled by the specified flags.

bool QgsVectorLayerExporter::addFeatures(QgsFeatureList& features, QgsFeatureSink::Flags flags = nullptr) override

Adds a list of features to the sink.

Returns true in case of success and false in case of failure

Feature addition behavior is controlled by the specified flags.

ExportError QgsVectorLayerExporter::errorCode() const

Returns any encountered error code, or false if no error was encountered.

int QgsVectorLayerExporter::errorCount() const

Returns the number of error messages encountered during the export.

QString QgsVectorLayerExporter::errorMessage() const

Returns any error message encountered during the export.

bool QgsVectorLayerExporter::flushBuffer() override

Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to the sink's destination.

Returns false if any buffered features could not be added to the sink.