QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
QgsVectorFileWriter Class Reference

A convenience class for writing vector layers to disk based formats (e.g. More...

#include <qgsvectorfilewriter.h>

Inheritance diagram for QgsVectorFileWriter:
Inheritance graph
[legend]

Classes

class  BoolOption
 
struct  DriverDetails
 Details of available driver formats. More...
 
class  FieldValueConverter
 Interface to convert raw field values to their user-friendly value. More...
 
struct  FilterFormatDetails
 Details of available filters and formats. More...
 
class  HiddenOption
 
class  IntOption
 
struct  MetaData
 
class  Option
 
class  SaveVectorOptions
 Options to pass to writeAsVectorFormat() More...
 
class  SetOption
 
class  StringOption
 

Public Types

enum  ActionOnExistingFile { CreateOrOverwriteFile , CreateOrOverwriteLayer , AppendToLayerNoNewFields , AppendToLayerAddFields }
 Enumeration to describe how to handle existing files. More...
 
typedef QFlags< EditionCapabilityEditionCapabilities
 Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteLayer.
 
enum  EditionCapability { CanAddNewLayer = 1 << 0 , CanAppendToExistingLayer = 1 << 1 , CanAddNewFieldsToExistingLayer = 1 << 2 , CanDeleteLayer = 1 << 3 }
 Edition capability flags. More...
 
enum  FieldNameSource { Original = 0 , PreferAlias }
 Source for exported field names. More...
 
enum  OptionType { Set , String , Int , Hidden }
 
enum  VectorFormatOption { SortRecommended = 1 << 1 , SkipNonSpatialFormats = 1 << 2 , SupportsMultipleLayers = 1 << 3 }
 Options for sorting and filtering vector formats. More...
 
typedef QFlags< VectorFormatOptionVectorFormatOptions
 
enum  WriterError {
  NoError = 0 , ErrDriverNotFound , ErrCreateDataSource , ErrCreateLayer ,
  ErrAttributeTypeUnsupported , ErrAttributeCreationFailed , ErrProjection , ErrFeatureWriteFailed ,
  ErrInvalidLayer , ErrSavingMetadata , Canceled
}
 
- Public Types inherited from QgsFeatureSink
enum  Flag { FastInsert = 1 << 1 , RollBackOnErrors = 1 << 2 }
 Flags controlling how features are added to a sink. More...
 
typedef QFlags< FlagFlags
 
enum  SinkFlag { RegeneratePrimaryKey = 1 << 1 }
 Flags that can be set on a QgsFeatureSink. More...
 
typedef QFlags< SinkFlagSinkFlags
 

Public Member Functions

 QgsVectorFileWriter (const QgsVectorFileWriter &rh)=delete
 QgsVectorFileWriter cannot be copied.
 
Q_DECL_DEPRECATED QgsVectorFileWriter (const QString &vectorFileName, const QString &fileEncoding, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &srs, const QString &driverName, const QStringList &datasourceOptions, const QStringList &layerOptions, QString *newFilename, Qgis::FeatureSymbologyExport symbologyExport, QgsVectorFileWriter::FieldValueConverter *fieldValueConverter, const QString &layerName, QgsVectorFileWriter::ActionOnExistingFile action, QString *newLayer=nullptr, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), FieldNameSource fieldNameSource=Original, bool includeConstraints=false, bool setFieldDomains=true, const QgsAbstractDatabaseProviderConnection *sourceDatabaseProviderConnection=nullptr)
 Create a new vector file writer.
 
Q_DECL_DEPRECATED QgsVectorFileWriter (const QString &vectorFileName, const QString &fileEncoding, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &srs=QgsCoordinateReferenceSystem(), const QString &driverName="GPKG", const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QString *newFilename=nullptr, Qgis::FeatureSymbologyExport symbologyExport=Qgis::FeatureSymbologyExport::NoSymbology, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QString *newLayer=nullptr, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext(), FieldNameSource fieldNameSource=Original)
 Create a new vector file writer.
 
 ~QgsVectorFileWriter () override
 Close opened shapefile for writing.
 
bool addFeature (QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
 Adds a single feature to the sink.
 
bool addFeatures (QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
 Adds a list of features to the sink.
 
bool addFeatureWithStyle (QgsFeature &feature, QgsFeatureRenderer *renderer, Qgis::DistanceUnit outputUnit=Qgis::DistanceUnit::Meters)
 Adds a feature to the currently opened data source, using the style from a specified renderer.
 
QMap< int, int > attrIdxToOgrIdx () const
 
Qgis::VectorFileWriterCapabilities capabilities () const
 Returns the capabilities supported by the writer.
 
QString driver () const
 Returns the GDAL (short) driver name associated with the output file.
 
QString driverLongName () const
 Returns the GDAL long driver name associated with the output file.
 
QString errorMessage () const
 Retrieves error message.
 
QgsVectorFileWriter::WriterError hasError () const
 Checks whether there were any errors in constructor.
 
QString lastError () const override
 Returns the most recent error encountered by the sink, e.g.
 
QgsVectorFileWriteroperator= (const QgsVectorFileWriter &rh)=delete
 QgsVectorFileWriter cannot be copied.
 
void setSymbologyExport (Qgis::FeatureSymbologyExport symExport)
 Sets the feature symbology export handling for the writer.
 
void setSymbologyScale (double scale)
 Set reference scale for output.
 
Qgis::FeatureSymbologyExport symbologyExport () const
 Returns the feature symbology export handling for the writer.
 
double symbologyScale () const
 Returns the reference scale for output.
 
- Public Member Functions inherited from QgsFeatureSink
virtual ~QgsFeatureSink ()=default
 
virtual bool addFeatures (QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
 Adds all features from the specified iterator to the sink.
 
virtual bool flushBuffer ()
 Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to the sink's destination.
 

Static Public Member Functions

static bool areThereNewFieldsToCreate (const QString &datasetName, const QString &layerName, QgsVectorLayer *layer, const QgsAttributeList &attributes)
 Returns whether there are among the attributes specified some that do not exist yet in the layer.
 
static QString convertCodecNameForEncodingOption (const QString &codecName)
 Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile.
 
static QgsVectorFileWritercreate (const QString &fileName, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &srs, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QString *newFilename=nullptr, QString *newLayer=nullptr)
 Create a new vector file writer.
 
static QStringList defaultDatasetOptions (const QString &driverName)
 Returns a list of the default dataset options for a specified driver.
 
static QStringList defaultLayerOptions (const QString &driverName)
 Returns a list of the default layer options for a specified driver.
 
static bool deleteShapeFile (const QString &fileName)
 Delete a shapefile (and its accompanying shx / dbf / prj / qix / qpj / cpg / sbn / sbx / idm / ind)
 
static QString driverForExtension (const QString &extension)
 Returns the OGR driver name for a specified file extension.
 
static bool driverMetadata (const QString &driverName, MetaData &driverMetadata)
 
static QgsVectorFileWriter::EditionCapabilities editionCapabilities (const QString &datasetName)
 Returns edition capabilities for an existing dataset name.
 
static QString fileFilterString (VectorFormatOptions options=SortRecommended)
 Returns filter string that can be used for dialogs.
 
static QString filterForDriver (const QString &driverName)
 Creates a filter for an OGR driver key.
 
static QList< QgsVectorFileWriter::DriverDetailsogrDriverList (VectorFormatOptions options=SortRecommended)
 Returns the driver list that can be used for dialogs.
 
static OGRwkbGeometryType ogrTypeFromWkbType (Qgis::WkbType type)
 Gets the ogr geometry type from an internal QGIS wkb type enum.
 
static QList< QgsVectorFileWriter::FilterFormatDetailssupportedFiltersAndFormats (VectorFormatOptions options=SortRecommended)
 Returns a list or pairs, with format filter string as first element and OGR format key as second element.
 
static QStringList supportedFormatExtensions (VectorFormatOptions options=SortRecommended)
 Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
 
static bool supportsFeatureStyles (const QString &driverName)
 Returns true if the specified driverName supports feature styles.
 
static bool targetLayerExists (const QString &datasetName, const QString &layerName)
 Returns whether the target layer already exists.
 
static Q_DECL_DEPRECATED QgsVectorFileWriter::WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename=nullptr, QString *errorMessage=nullptr, QString *newLayer=nullptr)
 Writes a layer out to a vector file.
 
static Q_DECL_DEPRECATED QgsVectorFileWriter::WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateReferenceSystem &destCRS=QgsCoordinateReferenceSystem(), const QString &driverName="GPKG", bool onlySelected=false, QString *errorMessage=nullptr, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=nullptr, Qgis::FeatureSymbologyExport symbologyExport=Qgis::FeatureSymbologyExport::NoSymbology, double symbologyScale=1.0, const QgsRectangle *filterExtent=nullptr, Qgis::WkbType overrideGeometryType=Qgis::WkbType::Unknown, bool forceMulti=false, bool includeZ=false, const QgsAttributeList &attributes=QgsAttributeList(), QgsVectorFileWriter::FieldValueConverter *fieldValueConverter=nullptr, QString *newLayer=nullptr)
 Write contents of vector layer to an (OGR supported) vector format.
 
static Q_DECL_DEPRECATED QgsVectorFileWriter::WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateTransform &ct, const QString &driverName="GPKG", bool onlySelected=false, QString *errorMessage=nullptr, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=nullptr, Qgis::FeatureSymbologyExport symbologyExport=Qgis::FeatureSymbologyExport::NoSymbology, double symbologyScale=1.0, const QgsRectangle *filterExtent=nullptr, Qgis::WkbType overrideGeometryType=Qgis::WkbType::Unknown, bool forceMulti=false, bool includeZ=false, const QgsAttributeList &attributes=QgsAttributeList(), QgsVectorFileWriter::FieldValueConverter *fieldValueConverter=nullptr, QString *newLayer=nullptr)
 Writes a layer out to a vector file.
 
static Q_DECL_DEPRECATED QgsVectorFileWriter::WriterError writeAsVectorFormatV2 (QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename=nullptr, QString *newLayer=nullptr, QString *errorMessage=nullptr)
 Writes a layer out to a vector file.
 
static QgsVectorFileWriter::WriterError writeAsVectorFormatV3 (QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *errorMessage=nullptr, QString *newFilename=nullptr, QString *newLayer=nullptr)
 Writes a layer out to a vector file.
 

Protected Member Functions

OGRGeometryH createEmptyGeometry (Qgis::WkbType wkbType)
 

Protected Attributes

QMap< int, int > mAttrIdxToOgrIdx
 Map attribute indizes to OGR field indexes.
 
QTextCodec * mCodec = nullptr
 
gdal::ogr_datasource_unique_ptr mDS
 
WriterError mError
 Contains error value if construction was not successful.
 
QString mErrorMessage
 
QgsFields mFields
 
FieldValueConvertermFieldValueConverter = nullptr
 Field value converter.
 
bool mIncludeConstraints = false
 Whether to transfer field constraints to output.
 
OGRLayerH mLayer = nullptr
 
QString mOgrDriverLongName
 
QString mOgrDriverName
 
OGRSpatialReferenceH mOgrRef = nullptr
 
bool mSetFieldDomains = true
 Whether to set field domains to output.
 
QMap< QgsSymbolLayer *, QString > mSymbolLayerTable
 
Qgis::FeatureSymbologyExport mSymbologyExport = Qgis::FeatureSymbologyExport::NoSymbology
 
double mSymbologyScale
 Scale for symbology export (e.g. for symbols units in map units)
 
Qgis::WkbType mWkbType
 Geometry type which is being used.
 

Friends

class QgsVectorFileWriterTask
 
class TestQgsVectorFileWriter
 

Detailed Description

A convenience class for writing vector layers to disk based formats (e.g.

Shapefiles, GeoPackage).

There are two possibilities how to use this class:

  1. A static call to QgsVectorFileWriter::writeAsVectorFormat(...) which saves the whole vector layer.
  2. Create an instance of the class and issue calls to addFeature(...).

Definition at line 47 of file qgsvectorfilewriter.h.

Member Typedef Documentation

◆ EditionCapabilities

Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteLayer.

Definition at line 260 of file qgsvectorfilewriter.h.

◆ VectorFormatOptions

Definition at line 204 of file qgsvectorfilewriter.h.

Member Enumeration Documentation

◆ ActionOnExistingFile

Enumeration to describe how to handle existing files.

Enumerator
CreateOrOverwriteFile 

Create or overwrite file.

CreateOrOverwriteLayer 

Create or overwrite layer.

AppendToLayerNoNewFields 

Append features to existing layer, but do not create new fields.

AppendToLayerAddFields 

Append features to existing layer, and create new fields if needed.

Definition at line 265 of file qgsvectorfilewriter.h.

◆ EditionCapability

Edition capability flags.

Enumerator
CanAddNewLayer 

Flag to indicate that a new layer can be added to the dataset.

CanAppendToExistingLayer 

Flag to indicate that new features can be added to an existing layer.

CanAddNewFieldsToExistingLayer 

Flag to indicate that new fields can be added to an existing layer. Imply CanAppendToExistingLayer.

CanDeleteLayer 

Flag to indicate that an existing layer can be deleted.

Definition at line 242 of file qgsvectorfilewriter.h.

◆ FieldNameSource

Source for exported field names.

Since
QGIS 3.18
Enumerator
Original 

Use original field names.

PreferAlias 

Use the field alias as the exported field name, wherever one is set. Otherwise use the original field names.

Definition at line 189 of file qgsvectorfilewriter.h.

◆ OptionType

Enumerator
Set 
String 
Int 
Hidden 

Definition at line 50 of file qgsvectorfilewriter.h.

◆ VectorFormatOption

Options for sorting and filtering vector formats.

Enumerator
SortRecommended 

Use recommended sort order, with extremely commonly used formats listed first.

SkipNonSpatialFormats 

Filter out any formats which do not have spatial support (e.g. those which cannot save geometries)

SupportsMultipleLayers 

Filter to only formats which support multiple layers (since QGIS 3.32)

Definition at line 198 of file qgsvectorfilewriter.h.

◆ WriterError

Enumerator
NoError 
ErrDriverNotFound 
ErrCreateDataSource 
ErrCreateLayer 
ErrAttributeTypeUnsupported 
ErrAttributeCreationFailed 
ErrProjection 
ErrFeatureWriteFailed 
ErrInvalidLayer 
ErrSavingMetadata 

Metadata saving failed.

Canceled 

Writing was interrupted by manual cancellation.

Definition at line 169 of file qgsvectorfilewriter.h.

Constructor & Destructor Documentation

◆ QgsVectorFileWriter() [1/3]

QgsVectorFileWriter::QgsVectorFileWriter ( const QString &  vectorFileName,
const QString &  fileEncoding,
const QgsFields fields,
Qgis::WkbType  geometryType,
const QgsCoordinateReferenceSystem srs = QgsCoordinateReferenceSystem(),
const QString &  driverName = "GPKG",
const QStringList &  datasourceOptions = QStringList(),
const QStringList &  layerOptions = QStringList(),
QString *  newFilename = nullptr,
Qgis::FeatureSymbologyExport  symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology,
QgsFeatureSink::SinkFlags  sinkFlags = QgsFeatureSink::SinkFlags(),
QString *  newLayer = nullptr,
const QgsCoordinateTransformContext transformContext = QgsCoordinateTransformContext(),
FieldNameSource  fieldNameSource = Original 
)

Create a new vector file writer.

Deprecated:
Use create() instead.

Definition at line 75 of file qgsvectorfilewriter.cpp.

◆ QgsVectorFileWriter() [2/3]

QgsVectorFileWriter::QgsVectorFileWriter ( const QString &  vectorFileName,
const QString &  fileEncoding,
const QgsFields fields,
Qgis::WkbType  geometryType,
const QgsCoordinateReferenceSystem srs,
const QString &  driverName,
const QStringList &  datasourceOptions,
const QStringList &  layerOptions,
QString *  newFilename,
Qgis::FeatureSymbologyExport  symbologyExport,
QgsVectorFileWriter::FieldValueConverter fieldValueConverter,
const QString &  layerName,
QgsVectorFileWriter::ActionOnExistingFile  action,
QString *  newLayer = nullptr,
const QgsCoordinateTransformContext transformContext = QgsCoordinateTransformContext(),
QgsFeatureSink::SinkFlags  sinkFlags = QgsFeatureSink::SinkFlags(),
FieldNameSource  fieldNameSource = Original,
bool  includeConstraints = false,
bool  setFieldDomains = true,
const QgsAbstractDatabaseProviderConnection sourceDatabaseProviderConnection = nullptr 
)

Create a new vector file writer.

Parameters
vectorFileNamefile name to write to
fileEncodingencoding to use
fieldsfields to write
geometryTypegeometry type of output file
srsspatial reference system of output file
driverNameOGR driver to use
datasourceOptionslist of OGR data source creation options
layerOptionslist of OGR layer creation options
newFilenamepotentially modified file name (output parameter)
symbologyExportsymbology to export
fieldValueConverterfield value converter (added in QGIS 2.16)
layerNamelayer name. If let empty, it will be derived from the filename (added in QGIS 3.0)
actionaction on existing file (added in QGIS 3.0)
newLayerpotentially modified layer name (output parameter) (added in QGIS 3.4)
transformContexttransform context, needed if the output file srs is forced to specific crs (added in QGIS 3.10.3)
sinkFlagsfeature sink flags (added in QGIS 3.10.3)
fieldNameSourcesource for field names (since QGIS 3.18)
includeConstraintsset to true to copy field constraints to the destination layer (since QGIS 3.34)
setFieldDomainsset to true to copy field domains (since QGIS 3.36)
sourceDatabaseProviderConnectionsource database provider connection, for field domains (since QGIS 3.36)
Note
not available in Python bindings
Deprecated:
Use create() instead.

Definition at line 99 of file qgsvectorfilewriter.cpp.

◆ QgsVectorFileWriter() [3/3]

QgsVectorFileWriter::QgsVectorFileWriter ( const QgsVectorFileWriter rh)
delete

QgsVectorFileWriter cannot be copied.

◆ ~QgsVectorFileWriter()

QgsVectorFileWriter::~QgsVectorFileWriter ( )
override

Close opened shapefile for writing.

Definition at line 3280 of file qgsvectorfilewriter.cpp.

Member Function Documentation

◆ addFeature()

bool QgsVectorFileWriter::addFeature ( QgsFeature feature,
QgsFeatureSink::Flags  flags = QgsFeatureSink::Flags() 
)
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 2678 of file qgsvectorfilewriter.cpp.

◆ addFeatures()

bool QgsVectorFileWriter::addFeatures ( QgsFeatureList features,
QgsFeatureSink::Flags  flags = QgsFeatureSink::Flags() 
)
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 2683 of file qgsvectorfilewriter.cpp.

◆ addFeatureWithStyle()

bool QgsVectorFileWriter::addFeatureWithStyle ( QgsFeature feature,
QgsFeatureRenderer renderer,
Qgis::DistanceUnit  outputUnit = Qgis::DistanceUnit::Meters 
)

Adds a feature to the currently opened data source, using the style from a specified renderer.

Definition at line 2699 of file qgsvectorfilewriter.cpp.

◆ areThereNewFieldsToCreate()

bool QgsVectorFileWriter::areThereNewFieldsToCreate ( const QString &  datasetName,
const QString &  layerName,
QgsVectorLayer layer,
const QgsAttributeList attributes 
)
static

Returns whether there are among the attributes specified some that do not exist yet in the layer.

Definition at line 4612 of file qgsvectorfilewriter.cpp.

◆ attrIdxToOgrIdx()

QMap< int, int > QgsVectorFileWriter::attrIdxToOgrIdx ( ) const
inline
Note
not available in Python bindings

Definition at line 876 of file qgsvectorfilewriter.h.

◆ capabilities()

Qgis::VectorFileWriterCapabilities QgsVectorFileWriter::capabilities ( ) const

Returns the capabilities supported by the writer.

Since
QGIS 3.32

Definition at line 2673 of file qgsvectorfilewriter.cpp.

◆ convertCodecNameForEncodingOption()

QString QgsVectorFileWriter::convertCodecNameForEncodingOption ( const QString &  codecName)
static

Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile.

Definition at line 4213 of file qgsvectorfilewriter.cpp.

◆ create()

QgsVectorFileWriter * QgsVectorFileWriter::create ( const QString &  fileName,
const QgsFields fields,
Qgis::WkbType  geometryType,
const QgsCoordinateReferenceSystem srs,
const QgsCoordinateTransformContext transformContext,
const QgsVectorFileWriter::SaveVectorOptions options,
QgsFeatureSink::SinkFlags  sinkFlags = QgsFeatureSink::SinkFlags(),
QString *  newFilename = nullptr,
QString *  newLayer = nullptr 
)
static

Create a new vector file writer.

Parameters
fileNamefile name to write to
fieldsfields to write
geometryTypegeometry type of output file
srsspatial reference system of output file
transformContextcoordinate transform context
optionssave options
sinkFlagsfeature sink flags
newFilenamepotentially modified file name (output parameter)
newLayerpotentially modified layer name (output parameter)
Since
QGIS 3.10.3

Definition at line 131 of file qgsvectorfilewriter.cpp.

◆ createEmptyGeometry()

OGRGeometryH QgsVectorFileWriter::createEmptyGeometry ( Qgis::WkbType  wkbType)
protected
Note
not available in Python bindings

Definition at line 1038 of file qgsvectorfilewriter.cpp.

◆ defaultDatasetOptions()

QStringList QgsVectorFileWriter::defaultDatasetOptions ( const QString &  driverName)
static

Returns a list of the default dataset options for a specified driver.

Parameters
driverNamename of OGR driver
See also
defaultLayerOptions()

Definition at line 2623 of file qgsvectorfilewriter.cpp.

◆ defaultLayerOptions()

QStringList QgsVectorFileWriter::defaultLayerOptions ( const QString &  driverName)
static

Returns a list of the default layer options for a specified driver.

Parameters
driverNamename of OGR driver
See also
defaultDatasetOptions()

Definition at line 2632 of file qgsvectorfilewriter.cpp.

◆ deleteShapeFile()

bool QgsVectorFileWriter::deleteShapeFile ( const QString &  fileName)
static

Delete a shapefile (and its accompanying shx / dbf / prj / qix / qpj / cpg / sbn / sbx / idm / ind)

Parameters
fileName/path/to/file.shp
Returns
bool true if the file was deleted successfully

Definition at line 3860 of file qgsvectorfilewriter.cpp.

◆ driver()

QString QgsVectorFileWriter::driver ( ) const

Returns the GDAL (short) driver name associated with the output file.

See also
driverLongName()
Since
QGIS 3.32

Definition at line 2663 of file qgsvectorfilewriter.cpp.

◆ driverForExtension()

QString QgsVectorFileWriter::driverForExtension ( const QString &  extension)
static

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.

Definition at line 4153 of file qgsvectorfilewriter.cpp.

◆ driverLongName()

QString QgsVectorFileWriter::driverLongName ( ) const

Returns the GDAL long driver name associated with the output file.

See also
driver()
Since
QGIS 3.32

Definition at line 2668 of file qgsvectorfilewriter.cpp.

◆ driverMetadata()

bool QgsVectorFileWriter::driverMetadata ( const QString &  driverName,
QgsVectorFileWriter::MetaData driverMetadata 
)
static

Definition at line 2599 of file qgsvectorfilewriter.cpp.

◆ editionCapabilities()

QgsVectorFileWriter::EditionCapabilities QgsVectorFileWriter::editionCapabilities ( const QString &  datasetName)
static

Returns edition capabilities for an existing dataset name.

Definition at line 4557 of file qgsvectorfilewriter.cpp.

◆ errorMessage()

QString QgsVectorFileWriter::errorMessage ( ) const

Retrieves error message.

Definition at line 2658 of file qgsvectorfilewriter.cpp.

◆ fileFilterString()

QString QgsVectorFileWriter::fileFilterString ( VectorFormatOptions  options = SortRecommended)
static

Returns filter string that can be used for dialogs.

The options argument can be used to control the sorting and filtering of returned drivers.

Definition at line 4188 of file qgsvectorfilewriter.cpp.

◆ filterForDriver()

QString QgsVectorFileWriter::filterForDriver ( const QString &  driverName)
static

Creates a filter for an OGR driver key.

Definition at line 4202 of file qgsvectorfilewriter.cpp.

◆ hasError()

QgsVectorFileWriter::WriterError QgsVectorFileWriter::hasError ( ) const

Checks whether there were any errors in constructor.

Definition at line 2653 of file qgsvectorfilewriter.cpp.

◆ lastError()

QString QgsVectorFileWriter::lastError ( ) const
overridevirtual

Returns the most recent error encountered by the sink, e.g.

when a call to addFeatures() returns false.

Since
QGIS 3.16

Reimplemented from QgsFeatureSink.

Definition at line 2694 of file qgsvectorfilewriter.cpp.

◆ ogrDriverList()

QList< QgsVectorFileWriter::DriverDetails > QgsVectorFileWriter::ogrDriverList ( VectorFormatOptions  options = SortRecommended)
static

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.

Definition at line 4051 of file qgsvectorfilewriter.cpp.

◆ ogrTypeFromWkbType()

OGRwkbGeometryType QgsVectorFileWriter::ogrTypeFromWkbType ( Qgis::WkbType  type)
static

Gets the ogr geometry type from an internal QGIS wkb type enum.

Will drop M values and convert Z to 2.5D where required.

Note
not available in Python bindings

Definition at line 2641 of file qgsvectorfilewriter.cpp.

◆ operator=()

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

QgsVectorFileWriter cannot be copied.

◆ setSymbologyExport()

void QgsVectorFileWriter::setSymbologyExport ( Qgis::FeatureSymbologyExport  symExport)
inline

Sets the feature symbology export handling for the writer.

See also
symbologyExport()

Definition at line 900 of file qgsvectorfilewriter.h.

◆ setSymbologyScale()

void QgsVectorFileWriter::setSymbologyScale ( double  scale)

Set reference scale for output.

The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.

See also
symbologyScale()

Definition at line 3886 of file qgsvectorfilewriter.cpp.

◆ supportedFiltersAndFormats()

QList< QgsVectorFileWriter::FilterFormatDetails > QgsVectorFileWriter::supportedFiltersAndFormats ( VectorFormatOptions  options = SortRecommended)
static

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

Definition at line 3919 of file qgsvectorfilewriter.cpp.

◆ supportedFormatExtensions()

QStringList QgsVectorFileWriter::supportedFormatExtensions ( VectorFormatOptions  options = SortRecommended)
static

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

Definition at line 4009 of file qgsvectorfilewriter.cpp.

◆ supportsFeatureStyles()

bool QgsVectorFileWriter::supportsFeatureStyles ( const QString &  driverName)
static

Returns true if the specified driverName supports feature styles.

The driverName argument must be a valid GDAL driver name.

Definition at line 151 of file qgsvectorfilewriter.cpp.

◆ symbologyExport()

Qgis::FeatureSymbologyExport QgsVectorFileWriter::symbologyExport ( ) const
inline

Returns the feature symbology export handling for the writer.

See also
setSymbologyExport()

Definition at line 893 of file qgsvectorfilewriter.h.

◆ symbologyScale()

double QgsVectorFileWriter::symbologyScale ( ) const
inline

Returns the reference scale for output.

The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.

See also
setSymbologyScale()

Definition at line 907 of file qgsvectorfilewriter.h.

◆ targetLayerExists()

bool QgsVectorFileWriter::targetLayerExists ( const QString &  datasetName,
const QString &  layerName 
)
static

Returns whether the target layer already exists.

Definition at line 4596 of file qgsvectorfilewriter.cpp.

◆ writeAsVectorFormat() [1/3]

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat ( QgsVectorLayer layer,
const QString &  fileName,
const QgsVectorFileWriter::SaveVectorOptions options,
QString *  newFilename = nullptr,
QString *  errorMessage = nullptr,
QString *  newLayer = nullptr 
)
static

Writes a layer out to a vector file.

Parameters
layersource layer to write
fileNamefile name to write to
optionsoptions.
newFilenameQString pointer which will contain the new file name created (in case it is different to fileName).
errorMessagewill be set to the error message text, if an error occurs while writing the layer
newLayerQString pointer which will contain the new layer name created (in case it is different to the provided layer name) (added in QGIS 3.4, not available in python)
Deprecated:
Use writeAsVectorFormatV2() instead.

Definition at line 3819 of file qgsvectorfilewriter.cpp.

◆ writeAsVectorFormat() [2/3]

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat ( QgsVectorLayer layer,
const QString &  fileName,
const QString &  fileEncoding,
const QgsCoordinateReferenceSystem destCRS = QgsCoordinateReferenceSystem(),
const QString &  driverName = "GPKG",
bool  onlySelected = false,
QString *  errorMessage = nullptr,
const QStringList &  datasourceOptions = QStringList(),
const QStringList &  layerOptions = QStringList(),
bool  skipAttributeCreation = false,
QString *  newFilename = nullptr,
Qgis::FeatureSymbologyExport  symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology,
double  symbologyScale = 1.0,
const QgsRectangle filterExtent = nullptr,
Qgis::WkbType  overrideGeometryType = Qgis::WkbType::Unknown,
bool  forceMulti = false,
bool  includeZ = false,
const QgsAttributeList attributes = QgsAttributeList(),
QgsVectorFileWriter::FieldValueConverter fieldValueConverter = nullptr,
QString *  newLayer = nullptr 
)
static

Write contents of vector layer to an (OGR supported) vector format.

Parameters
layerlayer to write
fileNamefile name to write to
fileEncodingencoding to use
destCRSCRS to reproject exported geometries to, or invalid CRS for no reprojection
driverNameOGR driver to use
onlySelectedwrite only selected features of layer
errorMessagewill be set to the error message text, if an error occurs while writing the layer
datasourceOptionslist of OGR data source creation options
layerOptionslist of OGR layer creation options
skipAttributeCreationonly write geometries
newFilenameQString pointer which will contain the new file name created (in case it is different to fileName).
symbologyExportsymbology to export
symbologyScalescale of symbology
filterExtentif not nullptr, only features intersecting the extent will be saved (added in QGIS 2.4)
overrideGeometryTypeset 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)
forceMultiset to true to force creation of multi* geometries (added in QGIS 2.14)
includeZset to true to include z dimension in output. This option is only valid if overrideGeometryType is set. (added in QGIS 2.14)
attributesattributes to export (empty means all unless skipAttributeCreation is set)
fieldValueConverterfield value converter (added in QGIS 2.16)
newLayerQString pointer which will contain the new layer name created (in case it is different to the provided layer name) (added in QGIS 3.4, not available in python)
Deprecated:
Use writeAsVectorFormatV2() instead.

Definition at line 3298 of file qgsvectorfilewriter.cpp.

◆ writeAsVectorFormat() [3/3]

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat ( QgsVectorLayer layer,
const QString &  fileName,
const QString &  fileEncoding,
const QgsCoordinateTransform ct,
const QString &  driverName = "GPKG",
bool  onlySelected = false,
QString *  errorMessage = nullptr,
const QStringList &  datasourceOptions = QStringList(),
const QStringList &  layerOptions = QStringList(),
bool  skipAttributeCreation = false,
QString *  newFilename = nullptr,
Qgis::FeatureSymbologyExport  symbologyExport = Qgis::FeatureSymbologyExport::NoSymbology,
double  symbologyScale = 1.0,
const QgsRectangle filterExtent = nullptr,
Qgis::WkbType  overrideGeometryType = Qgis::WkbType::Unknown,
bool  forceMulti = false,
bool  includeZ = false,
const QgsAttributeList attributes = QgsAttributeList(),
QgsVectorFileWriter::FieldValueConverter fieldValueConverter = nullptr,
QString *  newLayer = nullptr 
)
static

Writes a layer out to a vector file.

Parameters
layerlayer to write
fileNamefile name to write to
fileEncodingencoding to use
ctcoordinate transform to reproject exported geometries with, or invalid transform for no transformation
driverNameOGR driver to use
onlySelectedwrite only selected features of layer
errorMessagewill be set to the error message text, if an error occurs while writing the layer
datasourceOptionslist of OGR data source creation options
layerOptionslist of OGR layer creation options
skipAttributeCreationonly write geometries
newFilenameQString pointer which will contain the new file name created (in case it is different to fileName).
symbologyExportsymbology to export
symbologyScalescale of symbology
filterExtentif not nullptr, only features intersecting the extent will be saved (added in QGIS 2.4)
overrideGeometryTypeset 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)
forceMultiset to true to force creation of multi* geometries (added in QGIS 2.14)
includeZset to true to include z dimension in output. This option is only valid if overrideGeometryType is set. (added in QGIS 2.14)
attributesattributes to export (empty means all unless skipAttributeCreation is set)
fieldValueConverterfield value converter (added in QGIS 2.16)
newLayerQString pointer which will contain the new layer name created (in case it is different to the provided layer name) (added in QGIS 3.4, not available in python)
Deprecated:
Use writeAsVectorFormatV2() instead.

Definition at line 3345 of file qgsvectorfilewriter.cpp.

◆ writeAsVectorFormatV2()

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV2 ( QgsVectorLayer layer,
const QString &  fileName,
const QgsCoordinateTransformContext transformContext,
const QgsVectorFileWriter::SaveVectorOptions options,
QString *  newFilename = nullptr,
QString *  newLayer = nullptr,
QString *  errorMessage = nullptr 
)
static

Writes a layer out to a vector file.

Parameters
layersource layer to write
fileNamefile name to write to
transformContextcoordinate transform context
optionssave options
newFilenamepotentially modified file name (output parameter)
newLayerpotentially modified layer name (output parameter)
errorMessagewill be set to the error message text, if an error occurs while writing the layer
Returns
Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
Deprecated:
since QGIS 3.20, use writeAsVectorFormatV3 instead

Definition at line 3834 of file qgsvectorfilewriter.cpp.

◆ writeAsVectorFormatV3()

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV3 ( QgsVectorLayer layer,
const QString &  fileName,
const QgsCoordinateTransformContext transformContext,
const QgsVectorFileWriter::SaveVectorOptions options,
QString *  errorMessage = nullptr,
QString *  newFilename = nullptr,
QString *  newLayer = nullptr 
)
static

Writes a layer out to a vector file.

Parameters
layersource layer to write
fileNamefile name to write to
transformContextcoordinate transform context
optionssave options
newFilenamepotentially modified file name (output parameter)
newLayerpotentially modified layer name (output parameter)
errorMessagewill be set to the error message text, if an error occurs while writing the layer
Returns
Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
Since
QGIS 3.20

Definition at line 3850 of file qgsvectorfilewriter.cpp.

Friends And Related Symbol Documentation

◆ QgsVectorFileWriterTask

friend class QgsVectorFileWriterTask
friend

Definition at line 1114 of file qgsvectorfilewriter.h.

◆ TestQgsVectorFileWriter

friend class TestQgsVectorFileWriter
friend

Definition at line 1115 of file qgsvectorfilewriter.h.

Member Data Documentation

◆ mAttrIdxToOgrIdx

QMap<int, int> QgsVectorFileWriter::mAttrIdxToOgrIdx
protected

Map attribute indizes to OGR field indexes.

Definition at line 979 of file qgsvectorfilewriter.h.

◆ mCodec

QTextCodec* QgsVectorFileWriter::mCodec = nullptr
protected

Definition at line 973 of file qgsvectorfilewriter.h.

◆ mDS

gdal::ogr_datasource_unique_ptr QgsVectorFileWriter::mDS
protected

Definition at line 963 of file qgsvectorfilewriter.h.

◆ mError

WriterError QgsVectorFileWriter::mError
protected

Contains error value if construction was not successful.

Definition at line 970 of file qgsvectorfilewriter.h.

◆ mErrorMessage

QString QgsVectorFileWriter::mErrorMessage
protected

Definition at line 971 of file qgsvectorfilewriter.h.

◆ mFields

QgsFields QgsVectorFileWriter::mFields
protected

Definition at line 967 of file qgsvectorfilewriter.h.

◆ mFieldValueConverter

FieldValueConverter* QgsVectorFileWriter::mFieldValueConverter = nullptr
protected

Field value converter.

Definition at line 992 of file qgsvectorfilewriter.h.

◆ mIncludeConstraints

bool QgsVectorFileWriter::mIncludeConstraints = false
protected

Whether to transfer field constraints to output.

Definition at line 995 of file qgsvectorfilewriter.h.

◆ mLayer

OGRLayerH QgsVectorFileWriter::mLayer = nullptr
protected

Definition at line 964 of file qgsvectorfilewriter.h.

◆ mOgrDriverLongName

QString QgsVectorFileWriter::mOgrDriverLongName
protected

Definition at line 989 of file qgsvectorfilewriter.h.

◆ mOgrDriverName

QString QgsVectorFileWriter::mOgrDriverName
protected

Definition at line 988 of file qgsvectorfilewriter.h.

◆ mOgrRef

OGRSpatialReferenceH QgsVectorFileWriter::mOgrRef = nullptr
protected

Definition at line 965 of file qgsvectorfilewriter.h.

◆ mSetFieldDomains

bool QgsVectorFileWriter::mSetFieldDomains = true
protected

Whether to set field domains to output.

Definition at line 998 of file qgsvectorfilewriter.h.

◆ mSymbolLayerTable

QMap< QgsSymbolLayer *, QString > QgsVectorFileWriter::mSymbolLayerTable
protected

Definition at line 983 of file qgsvectorfilewriter.h.

◆ mSymbologyExport

Qgis::FeatureSymbologyExport QgsVectorFileWriter::mSymbologyExport = Qgis::FeatureSymbologyExport::NoSymbology
protected

Definition at line 981 of file qgsvectorfilewriter.h.

◆ mSymbologyScale

double QgsVectorFileWriter::mSymbologyScale
protected

Scale for symbology export (e.g. for symbols units in map units)

Definition at line 986 of file qgsvectorfilewriter.h.

◆ mWkbType

Qgis::WkbType QgsVectorFileWriter::mWkbType
protected

Geometry type which is being used.

Definition at line 976 of file qgsvectorfilewriter.h.


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