QGIS API Documentation  3.3.0-Master (c515a66)
Public Types | Public Member Functions | Static Public Member Functions | List of all members
QgsVectorLayerExporter Class Reference

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

#include <qgsvectorlayerexporter.h>

Inheritance diagram for QgsVectorLayerExporter:
Inheritance graph
[legend]

Public Types

enum  ExportError {
  NoError = 0, ErrCreateDataSource, ErrCreateLayer, ErrAttributeTypeUnsupported,
  ErrAttributeCreationFailed, ErrProjection, ErrFeatureWriteFailed, ErrInvalidLayer,
  ErrInvalidProvider, ErrProviderUnsupportedFeature, ErrConnectionFailed, ErrUserCanceled
}
 Error codes. More...
 
- Public Types inherited from QgsFeatureSink
enum  Flag { FastInsert = 1 << 1 }
 Flags controlling how features are added to a sink. More...
 

Public Member Functions

 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 >())
 Constructor for QgsVectorLayerExporter. More...
 
 QgsVectorLayerExporter (const QgsVectorLayerExporter &rh)=delete
 QgsVectorLayerExporter cannot be copied. More...
 
 ~QgsVectorLayerExporter () override
 Finalizes the export and closes the new created layer. More...
 
bool addFeature (QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
 Adds a single feature to the sink. More...
 
bool addFeatures (QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr) override
 Adds a list of features to the sink. More...
 
ExportError errorCode () const
 Returns any encountered error code, or false if no error was encountered. More...
 
int errorCount () const
 Returns the number of error messages encountered during the export. More...
 
QString errorMessage () const
 Returns any error message encountered during the export. More...
 
bool flushBuffer () override
 Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to the sink's destination. More...
 
QgsVectorLayerExporteroperator= (const QgsVectorLayerExporter &rh)=delete
 QgsVectorLayerExporter cannot be copied. More...
 
- Public Member Functions inherited from QgsFeatureSink
virtual ~QgsFeatureSink ()=default
 
virtual bool addFeatures (QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags=nullptr)
 Adds all features from the specified iterator to the sink. More...
 

Static Public Member Functions

static ExportError 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. More...
 

Detailed Description

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(...)
Since
QGIS 3.0

Definition at line 47 of file qgsvectorlayerexporter.h.

Member Enumeration Documentation

◆ ExportError

Error codes.

Enumerator
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.

Definition at line 52 of file qgsvectorlayerexporter.h.

Constructor & Destructor Documentation

◆ QgsVectorLayerExporter() [1/2]

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>() 
)

Constructor for QgsVectorLayerExporter.

Parameters
uriURI for destination data source
providerstring key for destination data provider
fieldsfields to include in created layer
geometryTypedestination geometry type
crsdesired CRS, or an invalid (default constructed) CRS if not available
overwriteset to true to overwrite any existing data source
optionsoptional provider dataset options

Definition at line 48 of file qgsvectorlayerexporter.cpp.

◆ QgsVectorLayerExporter() [2/2]

QgsVectorLayerExporter::QgsVectorLayerExporter ( const QgsVectorLayerExporter rh)
delete

QgsVectorLayerExporter cannot be copied.

◆ ~QgsVectorLayerExporter()

QgsVectorLayerExporter::~QgsVectorLayerExporter ( )
override

Finalizes the export and closes the new created layer.

Definition at line 127 of file qgsvectorlayerexporter.cpp.

Member Function Documentation

◆ addFeature()

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

Adds a single feature to the sink.

Feature addition behavior is controlled by the specified flags.

See also
addFeatures()
Returns
true in case of success and false in case of failure

Reimplemented from QgsFeatureSink.

Definition at line 154 of file qgsvectorlayerexporter.cpp.

◆ addFeatures()

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

Adds a list of features to the sink.

Feature addition behavior is controlled by the specified flags.

See also
addFeature()
Returns
true in case of success and false in case of failure

Implements QgsFeatureSink.

Definition at line 143 of file qgsvectorlayerexporter.cpp.

◆ errorCode()

QgsVectorLayerExporter::ExportError QgsVectorLayerExporter::errorCode ( ) const

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

See also
errorMessage()
errorCount()

Definition at line 133 of file qgsvectorlayerexporter.cpp.

◆ errorCount()

int QgsVectorLayerExporter::errorCount ( ) const
inline

Returns the number of error messages encountered during the export.

See also
errorMessage()
errorCode()

Definition at line 134 of file qgsvectorlayerexporter.h.

◆ errorMessage()

QString QgsVectorLayerExporter::errorMessage ( ) const

Returns any error message encountered during the export.

See also
errorCount()
errorCode()

Definition at line 138 of file qgsvectorlayerexporter.cpp.

◆ exportLayer()

QgsVectorLayerExporter::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 
)
static

Writes the contents of vector layer to a different datasource.

Parameters
layersource layer
uriURI for destination data source
providerKeystring key for destination data provider
destCRSdestination CRS, or an invalid (default constructed) CRS if not available
onlySelectedset to true to export only selected features
errorMessageif non-null, will be set to any error messages
optionsoptional provider dataset options
feedbackoptional feedback object to show progress and cancelation of export
Returns
NoError for a successful export, or encountered error

Definition at line 226 of file qgsvectorlayerexporter.cpp.

◆ flushBuffer()

bool QgsVectorLayerExporter::flushBuffer ( )
overridevirtual

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.

Reimplemented from QgsFeatureSink.

Definition at line 186 of file qgsvectorlayerexporter.cpp.

◆ operator=()

QgsVectorLayerExporter& QgsVectorLayerExporter::operator= ( const QgsVectorLayerExporter rh)
delete

QgsVectorLayerExporter cannot be copied.


The documentation for this class was generated from the following files: