Class: QgsVectorFileWriterΒΆ
- class qgis.core.QgsVectorFileWriterΒΆ
Bases:
QgsFeatureSink
A convenience class for writing vector layers to disk based formats (e.g. Shapefiles, GeoPackage).
There are two possibilities how to use this class:
A static call to
QgsVectorFileWriter
.writeAsVectorFormat(β¦) which saves the whole vector layer.Create an instance of the class and issue calls to addFeature(β¦).
Methods
- param feature
Adds a
feature
to the currently opened data source, using the style from a specifiedrenderer
.- param features
Returns whether there are among the attributes specified some that do not exist yet in the layer
Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile
Create a new vector file writer.
Returns a list of the default dataset options for a specified driver.
Returns a list of the default layer options for a specified driver.
Delete a shapefile (and its accompanying shx / dbf / prj / qix / qpj / cpg / sbn / sbx / idm / ind)
Returns the OGR driver name for a specified file
extension
.- param driverName
Returns edition capabilities for an existing dataset name.
Retrieves error message
Returns filter string that can be used for dialogs.
Creates a filter for an OGR driver key
Checks whether there were any errors in constructor
- rtype
str
Returns the driver list that can be used for dialogs.
- param symExport
Set reference
scale
for output.Returns a list or pairs, with format filter string as first element and OGR format key as second element.
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
Returns
True
if the specifieddriverName
supports feature styles.- rtype
QgsVectorFileWriter.SymbologyExport
Returns the reference scale for output.
Returns whether the target layer already exists.
Write contents of vector layer to an (OGR supported) vector format
Writes a layer out to a vector file.
Writes a layer out to a vector file.
Attributes
- class ActionOnExistingFileΒΆ
Bases:
int
- AppendToLayerAddFields = 3ΒΆ
- AppendToLayerNoNewFields = 2ΒΆ
- class BoolOption(docString: str, defaultValue: bool)ΒΆ
- class BoolOption(QgsVectorFileWriter.BoolOption)
Bases:
SetOption
- CanAddNewFieldsToExistingLayer = 4ΒΆ
- CanAddNewLayer = 1ΒΆ
- CanAppendToExistingLayer = 2ΒΆ
- CanDeleteLayer = 8ΒΆ
- Canceled = 10ΒΆ
- CreateOrOverwriteFile = 0ΒΆ
- CreateOrOverwriteLayer = 1ΒΆ
- class DriverDetailsΒΆ
- class DriverDetails(QgsVectorFileWriter.DriverDetails)
Bases:
sip.wrapper
- driverNameΒΆ
- longNameΒΆ
- class EditionCapabilitiesΒΆ
- class EditionCapabilities(Union[QgsVectorFileWriter.EditionCapabilities, QgsVectorFileWriter.EditionCapability])
- class EditionCapabilities(QgsVectorFileWriter.EditionCapabilities)
Bases:
sip.wrapper
- class EditionCapabilityΒΆ
Bases:
int
- ErrAttributeCreationFailed = 5ΒΆ
- ErrAttributeTypeUnsupported = 4ΒΆ
- ErrCreateDataSource = 2ΒΆ
- ErrCreateLayer = 3ΒΆ
- ErrDriverNotFound = 1ΒΆ
- ErrFeatureWriteFailed = 7ΒΆ
- ErrInvalidLayer = 8ΒΆ
- ErrProjection = 6ΒΆ
- ErrSavingMetadata = 9ΒΆ
- FeatureSymbology = 1ΒΆ
- class FieldNameSourceΒΆ
Bases:
int
- class FieldValueConverterΒΆ
Bases:
sip.wrapper
Interface to convert raw field values to their user-friendly value.
New in version 2.16.
QgsVectorFileWriter.FieldValueConverter() Constructor
QgsVectorFileWriter.FieldValueConverter(QgsVectorFileWriter.FieldValueConverter)
- clone(self) QgsVectorFileWriter.FieldValueConverter ΒΆ
Creates a clone of the FieldValueConverter.
- Return type
- convert(self, fieldIdxInLayer: int, value: Any) Any ΒΆ
Convert the provided value, for field fieldIdxInLayer. Default implementation will return provided value unmodified.
- Parameters
fieldIdxInLayer (int) β field index
value (Any) β original raw value
- Return type
Any
- Returns
possibly modified value.
- class FilterFormatDetailsΒΆ
- class FilterFormatDetails(QgsVectorFileWriter.FilterFormatDetails)
Bases:
sip.wrapper
- driverNameΒΆ
- filterStringΒΆ
- globsΒΆ
- Hidden = 3ΒΆ
- class HiddenOption(value: str)ΒΆ
- class HiddenOption(QgsVectorFileWriter.HiddenOption)
Bases:
Option
- mValueΒΆ
- Int = 2ΒΆ
- class IntOption(docString: str, defaultValue: int)ΒΆ
- class IntOption(QgsVectorFileWriter.IntOption)
Bases:
Option
- defaultValueΒΆ
- class MetaDataΒΆ
Bases:
sip.wrapper
Constructor for MetaData
QgsVectorFileWriter.MetaData(longName: str, trLongName: str, glob: str, ext: str, driverOptions: object, layerOptions: object, compulsoryEncoding: str = ββ)
QgsVectorFileWriter.MetaData(QgsVectorFileWriter.MetaData)
- compulsoryEncodingΒΆ
- driverOptionsΒΆ
- extΒΆ
- globΒΆ
- layerOptionsΒΆ
- longNameΒΆ
- trLongNameΒΆ
- NoError = 0ΒΆ
- NoSymbology = 0ΒΆ
- class Option(docString: str, type: QgsVectorFileWriter.OptionType)ΒΆ
- class Option(QgsVectorFileWriter.Option)
Bases:
sip.wrapper
- docStringΒΆ
- typeΒΆ
- class OptionTypeΒΆ
Bases:
int
- Original = 0ΒΆ
- PreferAlias = 1ΒΆ
- class SaveVectorOptionsΒΆ
Bases:
sip.wrapper
Options to pass to
writeAsVectorFormat()
New in version 3.0.
QgsVectorFileWriter.SaveVectorOptions() Constructor
QgsVectorFileWriter.SaveVectorOptions(QgsVectorFileWriter.SaveVectorOptions)
- actionOnExistingFileΒΆ
- attributesΒΆ
- ctΒΆ
- datasourceOptionsΒΆ
- driverNameΒΆ
- feedbackΒΆ
- fieldNameSourceΒΆ
- fieldValueConverterΒΆ
- fileEncodingΒΆ
- filterExtentΒΆ
- forceMultiΒΆ
- includeZΒΆ
- layerMetadataΒΆ
- layerNameΒΆ
- layerOptionsΒΆ
- onlySelectedFeaturesΒΆ
- overrideGeometryTypeΒΆ
- saveMetadataΒΆ
- skipAttributeCreationΒΆ
- symbologyExportΒΆ
- symbologyScaleΒΆ
- Set = 0ΒΆ
- class SetOption(docString: str, values: Iterable[str], defaultValue: str, allowNone: bool = False)ΒΆ
- class SetOption(QgsVectorFileWriter.SetOption)
Bases:
Option
- allowNoneΒΆ
- defaultValueΒΆ
- valuesΒΆ
- SkipNonSpatialFormats = 4ΒΆ
- SortRecommended = 2ΒΆ
- String = 1ΒΆ
- class StringOption(docString: str, defaultValue: str = '')ΒΆ
- class StringOption(QgsVectorFileWriter.StringOption)
Bases:
Option
- defaultValueΒΆ
- SymbolLayerSymbology = 2ΒΆ
- class SymbologyExportΒΆ
Bases:
int
- class VectorFormatOptionΒΆ
Bases:
int
- class VectorFormatOptionsΒΆ
- class VectorFormatOptions(Union[QgsVectorFileWriter.VectorFormatOptions, QgsVectorFileWriter.VectorFormatOption])
- class VectorFormatOptions(QgsVectorFileWriter.VectorFormatOptions)
Bases:
sip.wrapper
- class WriterErrorΒΆ
Bases:
int
- addFeature(self, feature: QgsFeature, flags: Union[QgsFeatureSink.Flags, QgsFeatureSink.Flag] = QgsFeatureSink.Flags()) bool ΒΆ
- Parameters
feature (QgsFeature) β
flags (Union[QgsFeatureSink.Flags) β
- Return type
bool
- addFeatureWithStyle(self, feature: QgsFeature, renderer: QgsFeatureRenderer, outputUnit: QgsUnitTypes.DistanceUnit = QgsUnitTypes.DistanceMeters) bool ΒΆ
Adds a
feature
to the currently opened data source, using the style from a specifiedrenderer
.New in version 3.0.
- Parameters
feature (QgsFeature) β
renderer (QgsFeatureRenderer) β
outputUnit (QgsUnitTypes.DistanceUnit = QgsUnitTypes.DistanceMeters) β
- Return type
bool
- addFeatures(self, features: Iterable[QgsFeature], flags: Union[QgsFeatureSink.Flags, QgsFeatureSink.Flag] = QgsFeatureSink.Flags()) bool ΒΆ
- Parameters
features (Iterable[QgsFeature]) β
flags (Union[QgsFeatureSink.Flags) β
- Return type
bool
- areThereNewFieldsToCreate(datasetName: str, layerName: str, layer: QgsVectorLayer, attributes: Iterable[int]) bool ΒΆ
Returns whether there are among the attributes specified some that do not exist yet in the layer
New in version 3.0.
- Parameters
datasetName (str) β
layerName (str) β
layer (QgsVectorLayer) β
attributes (Iterable[int]) β
- Return type
bool
- convertCodecNameForEncodingOption(codecName: str) str ΒΆ
Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile
- Parameters
codecName (str) β
- Return type
str
- create(fileName: str, fields: QgsFields, geometryType: QgsWkbTypes.Type, srs: QgsCoordinateReferenceSystem, transformContext: QgsCoordinateTransformContext, options: QgsVectorFileWriter.SaveVectorOptions, sinkFlags: Union[QgsFeatureSink.SinkFlags, QgsFeatureSink.SinkFlag] = QgsFeatureSink.SinkFlags(), newFilename: str = '', newLayer: str = '') QgsVectorFileWriter ΒΆ
Create a new vector file writer.
- Parameters
fileName (str) β file name to write to
fields (QgsFields) β fields to write
geometryType (QgsWkbTypes.Type) β geometry type of output file
srs (QgsCoordinateReferenceSystem) β spatial reference system of output file
transformContext (QgsCoordinateTransformContext) β coordinate transform context
options (QgsVectorFileWriter.SaveVectorOptions) β save options
sinkFlags (Union[QgsFeatureSink.SinkFlags) β feature sink flags
newFilename (str = '') β potentially modified file name (output parameter)
newLayer (str = '') β potentially modified layer name (output parameter)
New in version 3.10.3.
- Return type
- defaultDatasetOptions(driverName: str) List[str] ΒΆ
Returns a list of the default dataset options for a specified driver.
- Parameters
driverName (str) β name of OGR driver
See also
New in version 3.0.
- Return type
List[str]
- defaultLayerOptions(driverName: str) List[str] ΒΆ
Returns a list of the default layer options for a specified driver.
- Parameters
driverName (str) β name of OGR driver
See also
New in version 3.0.
- Return type
List[str]
- deleteShapeFile(fileName: str) bool ΒΆ
Delete a shapefile (and its accompanying shx / dbf / prj / qix / qpj / cpg / sbn / sbx / idm / ind)
- Parameters
fileName (str) β /path/to/file.shp
- Return type
bool
- Returns
bool
True
if the file was deleted successfully
- driverForExtension(extension: str) str ΒΆ
Returns the OGR driver name for a specified file
extension
. E.g. the driver name for the β.shpβ extension is βESRI Shapefileβ. If no suitable drivers are found then an empty string is returned.New in version 3.0.
- Parameters
extension (str) β
- Return type
str
- driverMetadata(driverName: str, driverMetadata: QgsVectorFileWriter.MetaData) bool ΒΆ
- Parameters
driverName (str) β
driverMetadata (QgsVectorFileWriter.MetaData) β
- Return type
bool
- editionCapabilities(datasetName: str) QgsVectorFileWriter.EditionCapabilities ΒΆ
Returns edition capabilities for an existing dataset name.
New in version 3.0.
- Parameters
datasetName (str) β
- Return type
- errorMessage(self) str ΒΆ
Retrieves error message
- Return type
str
- fileFilterString(options: Union[QgsVectorFileWriter.VectorFormatOptions, QgsVectorFileWriter.VectorFormatOption] = QgsVectorFileWriter.SortRecommended) str ΒΆ
Returns filter string that can be used for dialogs.
The
options
argument can be used to control the sorting and filtering of returned drivers.- Parameters
options (Union[QgsVectorFileWriter.VectorFormatOptions) β
- Return type
str
- filterForDriver(driverName: str) str ΒΆ
Creates a filter for an OGR driver key
- Parameters
driverName (str) β
- Return type
str
- hasError(self) QgsVectorFileWriter.WriterError ΒΆ
Checks whether there were any errors in constructor
- Return type
- lastError(self) str ΒΆ
- Return type
str
- ogrDriverList(options: Union[QgsVectorFileWriter.VectorFormatOptions, QgsVectorFileWriter.VectorFormatOption] = QgsVectorFileWriter.SortRecommended) List[QgsVectorFileWriter.DriverDetails] ΒΆ
Returns the driver list that can be used for dialogs. It contains all OGR drivers plus some additional internal QGIS driver names to distinguish between more supported formats of the same OGR driver.
The returned list consists of structs containing the driver long name (e.g. user-friendly display name for the format) and internal driver short name.
The
options
argument can be used to control the sorting and filtering of returned drivers.- Parameters
options (Union[QgsVectorFileWriter.VectorFormatOptions) β
- Return type
- setSymbologyExport(self, symExport: QgsVectorFileWriter.SymbologyExport)ΒΆ
- Parameters
symExport (QgsVectorFileWriter.SymbologyExport) β
- setSymbologyScale(self, scale: float)ΒΆ
Set reference
scale
for output. Thescale
value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.See also
New in version 3.0.
- Parameters
scale (float) β
- supportedFiltersAndFormats(options: Union[QgsVectorFileWriter.VectorFormatOptions, QgsVectorFileWriter.VectorFormatOption] = QgsVectorFileWriter.SortRecommended) List[QgsVectorFileWriter.FilterFormatDetails] ΒΆ
Returns a list or pairs, with format filter string as first element and OGR format key as second element.
The
options
argument can be used to control the sorting and filtering of returned formats.See also
- Parameters
options (Union[QgsVectorFileWriter.VectorFormatOptions) β
- Return type
- supportedFormatExtensions(options: Union[QgsVectorFileWriter.VectorFormatOptions, QgsVectorFileWriter.VectorFormatOption] = QgsVectorFileWriter.SortRecommended) List[str] ΒΆ
Returns a list of file extensions for supported formats, e.g βshpβ, βgpkgβ.
The
options
argument can be used to control the sorting and filtering of returned formats.See also
New in version 3.0.
- Parameters
options (Union[QgsVectorFileWriter.VectorFormatOptions) β
- Return type
List[str]
- supportsFeatureStyles(driverName: str) bool ΒΆ
Returns
True
if the specifieddriverName
supports feature styles.The
driverName
argument must be a valid GDAL driver name.New in version 3.0.
- Parameters
driverName (str) β
- Return type
bool
- symbologyExport(self) QgsVectorFileWriter.SymbologyExport ΒΆ
- Return type
- symbologyScale(self) float ΒΆ
Returns the reference scale for output. The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
See also
New in version 3.0.
- Return type
float
- targetLayerExists(datasetName: str, layerName: str) bool ΒΆ
Returns whether the target layer already exists.
New in version 3.0.
- Parameters
datasetName (str) β
layerName (str) β
- Return type
bool
- writeAsVectorFormat(layer: QgsVectorLayer, fileName: str, fileEncoding: str, destCRS: QgsCoordinateReferenceSystem = QgsCoordinateReferenceSystem(), driverName: str = '', onlySelected: bool = False, datasourceOptions: Iterable[str] = [], layerOptions: Iterable[str] = [], skipAttributeCreation: bool = False, newFilename: str = '', symbologyExport: QgsVectorFileWriter.SymbologyExport = QgsVectorFileWriter.NoSymbology, symbologyScale: float = 1, filterExtent: QgsRectangle = None, overrideGeometryType: QgsWkbTypes.Type = QgsWkbTypes.Unknown, forceMulti: bool = False, includeZ: bool = False, attributes: Iterable[int] = [], fieldValueConverter: QgsVectorFileWriter.FieldValueConverter = None) Tuple[QgsVectorFileWriter.WriterError, str] ΒΆ
Write contents of vector layer to an (OGR supported) vector format
- Parameters
layer (QgsVectorLayer) β layer to write
fileName (str) β file name to write to
fileEncoding (str) β encoding to use
destCRS (
QgsCoordinateReferenceSystem
= QgsCoordinateReferenceSystem()) β CRS to reproject exported geometries to, or invalid CRS for no reprojectiondriverName (str = '') β OGR driver to use
onlySelected (bool = False) β write only selected features of layer
errorMessage β will be set to the error message text, if an error occurs while writing the layer
datasourceOptions (Iterable[str] = []) β list of OGR data source creation options
layerOptions (Iterable[str] = []) β list of OGR layer creation options
skipAttributeCreation (bool = False) β only write geometries
newFilename (str = '') β QString pointer which will contain the new file name created (in case it is different to fileName).
symbologyExport (QgsVectorFileWriter.SymbologyExport = QgsVectorFileWriter.NoSymbology) β symbology to export
symbologyScale (float = 1) β scale of symbology
filterExtent (
QgsRectangle
= None) β if notNone
, only features intersecting the extent will be saved (added in QGIS 2.4)overrideGeometryType (QgsWkbTypes.Type = QgsWkbTypes.Unknown) β set to a valid geometry type to override the default geometry type for the layer. This parameter allows for conversion of geometryless tables to null geometries, etc (added in QGIS 2.14)
forceMulti (bool = False) β set to
True
to force creation of multi* geometries (added in QGIS 2.14)includeZ (bool = False) β set to
True
to include z dimension in output. This option is only valid if overrideGeometryType is set. (added in QGIS 2.14)attributes (Iterable[int] = []) β attributes to export (empty means all unless skipAttributeCreation is set)
fieldValueConverter (QgsVectorFileWriter.FieldValueConverter = None) β field value converter (added in QGIS 2.16)
Deprecated since version Use:
writeAsVectorFormatV2()
instead.writeAsVectorFormat(layer:
QgsVectorLayer
, fileName: str, fileEncoding: str, ct:QgsCoordinateTransform
, driverName: str = ββ, onlySelected: bool = False, datasourceOptions: Iterable[str] = [], layerOptions: Iterable[str] = [], skipAttributeCreation: bool = False, newFilename: str = ββ, symbologyExport: QgsVectorFileWriter.SymbologyExport = QgsVectorFileWriter.NoSymbology, symbologyScale: float = 1, filterExtent:QgsRectangle
= None, overrideGeometryType: QgsWkbTypes.Type = QgsWkbTypes.Unknown, forceMulti: bool = False, includeZ: bool = False, attributes: Iterable[int] = [], fieldValueConverter: QgsVectorFileWriter.FieldValueConverter = None) -> Tuple[QgsVectorFileWriter.WriterError, str] Writes a layer out to a vector file.- Parameters
layer β layer to write
fileName β file name to write to
fileEncoding β encoding to use
ct β coordinate transform to reproject exported geometries with, or invalid transform for no transformation
driverName β OGR driver to use
onlySelected β write only selected features of layer
errorMessage β will be set to the error message text, if an error occurs while writing the layer
datasourceOptions β list of OGR data source creation options
layerOptions β list of OGR layer creation options
skipAttributeCreation β only write geometries
newFilename β QString pointer which will contain the new file name created (in case it is different to fileName).
symbologyExport β symbology to export
symbologyScale β scale of symbology
filterExtent β if not
None
, only features intersecting the extent will be saved (added in QGIS 2.4)overrideGeometryType β set to a valid geometry type to override the default geometry type for the layer. This parameter allows for conversion of geometryless tables to null geometries, etc (added in QGIS 2.14)
forceMulti β set to
True
to force creation of multi* geometries (added in QGIS 2.14)includeZ β set to
True
to include z dimension in output. This option is only valid if overrideGeometryType is set. (added in QGIS 2.14)attributes β attributes to export (empty means all unless skipAttributeCreation is set)
fieldValueConverter β field value converter (added in QGIS 2.16)
New in version 2.2.
Deprecated since version Use:
writeAsVectorFormatV2()
instead.writeAsVectorFormat(layer:
QgsVectorLayer
, fileName: str, options: QgsVectorFileWriter.SaveVectorOptions, newFilename: str = ββ) -> Tuple[QgsVectorFileWriter.WriterError, str] Writes a layer out to a vector file.- Parameters
layer β source layer to write
fileName β file name to write to
options β options.
newFilename β QString pointer which will contain the new file name created (in case it is different to fileName).
errorMessage β will be set to the error message text, if an error occurs while writing the layer
New in version 3.0.
Deprecated since version Use:
writeAsVectorFormatV2()
instead.- Return type
Tuple[QgsVectorFileWriter.WriterError, str]
- writeAsVectorFormatV2(layer: QgsVectorLayer, fileName: str, transformContext: QgsCoordinateTransformContext, options: QgsVectorFileWriter.SaveVectorOptions, newFilename: str = '', newLayer: str = '') Tuple[QgsVectorFileWriter.WriterError, str] ΒΆ
Writes a layer out to a vector file.
- Parameters
layer (QgsVectorLayer) β source layer to write
fileName (str) β file name to write to
transformContext (QgsCoordinateTransformContext) β coordinate transform context
options (QgsVectorFileWriter.SaveVectorOptions) β save options
newFilename (str = '') β potentially modified file name (output parameter)
newLayer (str = '') β potentially modified layer name (output parameter)
- Return type
Tuple[QgsVectorFileWriter.WriterError, str]
- Returns
Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
errorMessage: will be set to the error message text, if an error occurs while writing the layer
Deprecated since version QGIS: 3.20 use writeAsVectorFormatV3 instead
- writeAsVectorFormatV3(layer: QgsVectorLayer, fileName: str, transformContext: QgsCoordinateTransformContext, options: QgsVectorFileWriter.SaveVectorOptions) Tuple[QgsVectorFileWriter.WriterError, str, str, str] ΒΆ
Writes a layer out to a vector file.
- Parameters
layer (QgsVectorLayer) β source layer to write
fileName (str) β file name to write to
transformContext (QgsCoordinateTransformContext) β coordinate transform context
options (QgsVectorFileWriter.SaveVectorOptions) β save options
newFilename β potentially modified file name (output parameter)
errorMessage β will be set to the error message text, if an error occurs while writing the layer
- Return type
Tuple[QgsVectorFileWriter.WriterError, str, str, str]
- Returns
Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
newLayer: potentially modified layer name (output parameter)
New in version 3.20.