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¶
Base classes¶
An interface for objects which accept features via addFeature(s) methods. |
Enums
alias of |
Methods
Returns the attribute capabilities of the exporter. |
|
Returns any encountered error code, or |
|
Returns the number of error messages encountered during the export. |
|
Returns any error message encountered during the export. |
Static Methods
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 sourceoptions (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.
See also
- Return type:
- expressionContext(self) QgsExpressionContext ¶
Returns the expression context used when a
filterExpression()
is set.See also
- Return type:
- 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
- Return type:
- 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.See also
See also
- 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.See also
- Return type:
- 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.See also
- Return type:
bool
- setDestinationCrs(self, crs: QgsCoordinateReferenceSystem)[source]¶
Sets the destination coordinate reference system to use for exported features.
See also
- Parameters:
- setExpressionContext(self, context: QgsExpressionContext)[source]¶
Sets the expression
context
to use when afilterExpression()
is set.See also
- 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
- 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.See also
See also
- 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
- 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
- Parameters:
selected (bool)
- setTransformContext(self, context: QgsCoordinateTransformContext)[source]¶
Sets the coordinate transform
context
to use when transforming exported features.See also
- Parameters:
context (QgsCoordinateTransformContext)
- transformContext(self) QgsCoordinateTransformContext [source]¶
Returns the coordinate transform context used when transforming exported features.
See also
- Return type:
- 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
- attributeEditCapabilities(self) Qgis.VectorDataProviderAttributeEditCapabilities [source]¶
Returns the attribute capabilities of the exporter.
Added in version 3.32.
- Return type:
- errorCode(self) Qgis.VectorExportResult [source]¶
Returns any encountered error code, or
False
if no error was encountered.See also
See also
- Return type:
- errorCount(self) int [source]¶
Returns the number of error messages encountered during the export.
See also
See also
- Return type:
int
- errorMessage(self) str [source]¶
Returns any error message encountered during the export.
See also
See also
- 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 availableonlySelected (bool = False) – set to
True
to export only selected featuresoptions (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 errorerrorMessage: any error messages which occur during the export
Added in version 3.44.