Class: QgsVectorLayerExporter

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

QgsVectorLayerExporter can be used in two ways:

  • Using a static call to QgsVectorLayerExporter.exportLayer(…) which exports the entire layer to the destination provider.

  • Create an instance of the class and issue calls to addFeature(…)

Class Hierarchy

Inheritance diagram of qgis.core.QgsVectorLayerExporter

Base classes

QgsFeatureSink

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

Enums

ExportError

alias of VectorExportResult

Methods

attributeEditCapabilities

Returns the attribute capabilities of the exporter.

errorCode

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

errorCount

Returns the number of error messages encountered during the export.

errorMessage

Returns any error message encountered during the export.

Static Methods

exportLayer

Writes the contents of vector layer to a different datasource.

class qgis.core.QgsVectorLayerExporter[source]

Bases: QgsFeatureSink

__init__(uri: str | None, provider: str | None, fields: QgsFields, geometryType: Qgis.WkbType, crs: QgsCoordinateReferenceSystem, overwrite: bool = False, options: Dict[str | None, Any] = {}, sinkFlags: QgsFeatureSink.SinkFlags | QgsFeatureSink.SinkFlag = QgsFeatureSink.SinkFlags())

Constructor for QgsVectorLayerExporter.

Parameters:
  • uri (Optional[str]) – URI for destination data source

  • provider (Optional[str]) – string key for destination data provider

  • fields (QgsFields) – fields to include in created layer

  • geometryType (Qgis.WkbType) – destination geometry type

  • crs (QgsCoordinateReferenceSystem) – desired CRS, or an invalid (default constructed) CRS if not available

  • overwrite (bool = False) – set to True to overwrite any existing data source

  • options (Dict[Optional[str], Any] = {}) – optional provider dataset options

  • sinkFlags (Union[QgsFeatureSink.SinkFlags, QgsFeatureSink.SinkFlag] = QgsFeatureSink.SinkFlags()) – for how to add features

ExportError

alias of VectorExportResult

class ExportOptions[source]

Bases: object

Encapsulates options for use with QgsVectorLayerExporter.

Added in version 3.44.

destinationCrs(self) QgsCoordinateReferenceSystem[source]

Returns the destination coordinate reference system used for exported features.

Return type:

QgsCoordinateReferenceSystem

expressionContext(self) QgsExpressionContext

Returns the expression context used when a filterExpression() is set.

Return type:

QgsExpressionContext

extent(self) QgsReferencedRectangle[source]

Returns the extent filter for the features to export.

Only features with a bounding box intersecting this extent will be exported.

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

See also

setExtent()

Return type:

QgsReferencedRectangle

filterExpression(self) str[source]

Returns the filter expression for features to export.

Only features matching this expression will be exported.

Warning

This setting is incompatible with selectedOnly()

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

Return type:

str

outputFields(self) List[QgsVectorLayerExporter.OutputField]

Returns the output field definitions for the destination table.

If empty, all input fields will be copied directly.

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

Return type:

List[QgsVectorLayerExporter.OutputField]

selectedOnly(self) bool[source]

Returns whether the export will only include selected features.

Warning

This setting is incompatible with filterExpression()

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

Return type:

bool

setDestinationCrs(self, crs: QgsCoordinateReferenceSystem)[source]

Sets the destination coordinate reference system to use for exported features.

See also

destinationCrs()

Parameters:

crs (QgsCoordinateReferenceSystem)

setExpressionContext(self, context: QgsExpressionContext)[source]

Sets the expression context to use when a filterExpression() is set.

Parameters:

context (QgsExpressionContext)

setExtent(self, extent: QgsReferencedRectangle)[source]

Sets an extent filter for the features to export.

Only features with a bounding box intersecting extent will be exported.

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

See also

extent()

Parameters:

extent (QgsReferencedRectangle)

setFilterExpression(self, expression: str | None)[source]

Set the filter expression for the features to export.

Only features matching this expression will be exported.

Warning

This setting is incompatible with setSelectedOnly()

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

Parameters:

expression (Optional[str])

setOutputFields(self, fields: Iterable[QgsVectorLayerExporter.OutputField])[source]

Sets the output field definitions for the destination table.

If empty, all input fields will be copied directly.

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

See also

outputFields()

Parameters:

fields (Iterable[QgsVectorLayerExporter.OutputField])

setSelectedOnly(self, selected: bool)[source]

Sets whether the export should only include selected features.

Warning

This setting is incompatible with setFilterExpression()

Note

This option only applies when the QgsVectorLayerExporter.exportLayer() method is used.

See also

selectedOnly()

Parameters:

selected (bool)

setTransformContext(self, context: QgsCoordinateTransformContext)[source]

Sets the coordinate transform context to use when transforming exported features.

Parameters:

context (QgsCoordinateTransformContext)

transformContext(self) QgsCoordinateTransformContext[source]

Returns the coordinate transform context used when transforming exported features.

Return type:

QgsCoordinateTransformContext

class OutputField

Bases: object

Encapsulates output field definition.

Added in version 3.44.

expression: str

The expression for the exported field from the source fields

field: QgsField

Destination field definition

attributeEditCapabilities(self) Qgis.VectorDataProviderAttributeEditCapabilities[source]

Returns the attribute capabilities of the exporter.

Added in version 3.32.

Return type:

Qgis.VectorDataProviderAttributeEditCapabilities

errorCode(self) Qgis.VectorExportResult[source]

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

See also

errorMessage()

See also

errorCount()

Return type:

Qgis.VectorExportResult

errorCount(self) int[source]

Returns the number of error messages encountered during the export.

See also

errorMessage()

See also

errorCode()

Return type:

int

errorMessage(self) str[source]

Returns any error message encountered during the export.

See also

errorCount()

See also

errorCode()

Return type:

str

static exportLayer(layer: QgsVectorLayer | None, uri: str | None, providerKey: str | None, destCRS: QgsCoordinateReferenceSystem, onlySelected: bool = False, options: Dict[str | None, Any] = {}, feedback: QgsFeedback | None = None)[source]

Writes the contents of vector layer to a different datasource.

Parameters:
  • layer (Optional[QgsVectorLayer]) – source layer

  • uri (Optional[str]) – URI for destination data source

  • providerKey (Optional[str]) – string key for destination data provider

  • destCRS (QgsCoordinateReferenceSystem) – destination CRS, or an invalid (default constructed) CRS if not available

  • onlySelected (bool = False) – set to True to export only selected features

  • options (Dict[Optional[str], Any] = {}) – optional provider dataset options

  • feedback (Optional[QgsFeedback] = None) -> (Qgis.VectorExportResult) – optional feedback object to show progress and cancellation of export

Returns:

  • NoError for a successful export, or encountered error

  • errorMessage: if non-null, any error messages

static exportLayer(layer: QgsVectorLayer | None, uri: str | None, providerKey: str | None, exportOptions: QgsVectorLayerExporter.ExportOptions, providerOptions: Dict[str | None, Any] = {}, feedback: QgsFeedback | None = None)[source]

Writes the contents of vector layer to a different data provider.

Parameters:
  • layer (Optional[QgsVectorLayer]) – source layer

  • uri (Optional[str]) – URI for destination data source

  • providerKey (Optional[str]) – string key for destination data provider

  • exportOptions (QgsVectorLayerExporter.ExportOptions) – options controlling the feature export

  • providerOptions (Dict[Optional[str], Any] = {}) – optional provider dataset options

  • feedback (Optional[QgsFeedback] = None) -> (Qgis.VectorExportResult) – optional feedback object to show progress and cancellation of export

Returns:

  • Qgis.VectorExportResult.NoError for a successful export, or encountered error

  • errorMessage: any error messages which occur during the export

Added in version 3.44.