QGIS API Documentation  2.99.0-Master (c42dad3)
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
QgsVectorFileWriter Class Reference

A convenience class for writing vector files to disk. More...

#include <qgsvectorfilewriter.h>

Classes

class  BoolOption
 
class  FieldValueConverter
 Interface to convert raw field values to their user-friendly value. 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 }
 Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteLayer. More...
 
enum  EditionCapability { CanAddNewLayer = 1 << 0, CanAppendToExistingLayer = 1 << 1, CanAddNewFieldsToExistingLayer = 1 << 2, CanDeleteLayer = 1 << 3 }
 Edition capability flags. More...
 
enum  OptionType { Set, String, Int, Hidden }
 
enum  SymbologyExport { NoSymbology = 0, FeatureSymbology, SymbolLayerSymbology }
 
enum  WriterError {
  NoError = 0, ErrDriverNotFound, ErrCreateDataSource, ErrCreateLayer,
  ErrAttributeTypeUnsupported, ErrAttributeCreationFailed, ErrProjection, ErrFeatureWriteFailed,
  ErrInvalidLayer
}
 

Public Member Functions

 QgsVectorFileWriter (const QString &vectorFileName, const QString &fileEncoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &srs=QgsCoordinateReferenceSystem(), const QString &driverName="ESRI Shapefile", const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QString *newFilename=nullptr, SymbologyExport symbologyExport=NoSymbology)
 Create a new vector file writer. More...
 
 ~QgsVectorFileWriter ()
 Close opened shapefile for writing. More...
 
bool addFeature (QgsFeature &feature, QgsFeatureRenderer *renderer=nullptr, QgsUnitTypes::DistanceUnit outputUnit=QgsUnitTypes::DistanceMeters)
 Add feature to the currently opened data source. More...
 
QMap< int, int > attrIdxToOgrIdx ()
 
QString errorMessage ()
 Retrieves error message. More...
 
WriterError hasError ()
 Checks whether there were any errors in constructor. More...
 
void setSymbologyExport (SymbologyExport symExport)
 
void setSymbologyScaleDenominator (double d)
 
SymbologyExport symbologyExport () const
 
double symbologyScaleDenominator () const
 

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. More...
 
static QString convertCodecNameForEncodingOption (const QString &codecName)
 Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile. More...
 
static QStringList defaultDatasetOptions (const QString &driverName)
 Returns a list of the default dataset options for a specified driver. More...
 
static QStringList defaultLayerOptions (const QString &driverName)
 Returns a list of the default layer options for a specified driver. More...
 
static bool deleteShapeFile (const QString &theFileName)
 Delete a shapefile (and its accompanying shx / dbf / prf) More...
 
static bool driverMetadata (const QString &driverName, MetaData &driverMetadata)
 
static EditionCapabilities editionCapabilities (const QString &datasetName)
 Return edition capabilities for an existing dataset name. More...
 
static QString fileFilterString ()
 Returns filter string that can be used for dialogs. More...
 
static QString filterForDriver (const QString &driverName)
 Creates a filter for an OGR driver key. More...
 
static QMap< QString, QString > ogrDriverList ()
 Returns driver list that can be used for dialogs. More...
 
static OGRwkbGeometryType ogrTypeFromWkbType (QgsWkbTypes::Type type)
 Get the ogr geometry type from an internal QGIS wkb type enum. More...
 
static QMap< QString, QString > supportedFiltersAndFormats ()
 Returns map with format filter string as key and OGR format key as value. More...
 
static bool targetLayerExists (const QString &datasetName, const QString &layerName)
 Returns whether the target layer already exists. More...
 
static WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateReferenceSystem &destCRS=QgsCoordinateReferenceSystem(), const QString &driverName="ESRI Shapefile", bool onlySelected=false, QString *errorMessage=nullptr, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=nullptr, SymbologyExport symbologyExport=NoSymbology, double symbologyScale=1.0, const QgsRectangle *filterExtent=nullptr, QgsWkbTypes::Type overrideGeometryType=QgsWkbTypes::Unknown, bool forceMulti=false, bool includeZ=false, const QgsAttributeList &attributes=QgsAttributeList(), FieldValueConverter *fieldValueConverter=nullptr)
 Write contents of vector layer to an (OGR supported) vector formt. More...
 
static WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateTransform &ct, const QString &driverName="ESRI Shapefile", bool onlySelected=false, QString *errorMessage=nullptr, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=nullptr, SymbologyExport symbologyExport=NoSymbology, double symbologyScale=1.0, const QgsRectangle *filterExtent=nullptr, QgsWkbTypes::Type overrideGeometryType=QgsWkbTypes::Unknown, bool forceMulti=false, bool includeZ=false, const QgsAttributeList &attributes=QgsAttributeList(), FieldValueConverter *fieldValueConverter=nullptr)
 Writes a layer out to a vector file. More...
 
static WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const SaveVectorOptions &options, QString *newFilename=nullptr, QString *errorMessage=nullptr)
 Writes a layer out to a vector file. More...
 

Protected Member Functions

OGRGeometryH createEmptyGeometry (QgsWkbTypes::Type wkbType)
 

Protected Attributes

QMap< int, int > mAttrIdxToOgrIdx
 Map attribute indizes to OGR field indexes. More...
 
QTextCodec * mCodec
 
OGRDataSourceH mDS
 
WriterError mError
 Contains error value if construction was not successful. More...
 
QString mErrorMessage
 
QgsFields mFields
 
FieldValueConvertermFieldValueConverter
 Field value converter. More...
 
OGRGeometryH mGeom
 
OGRLayerH mLayer
 
QString mOgrDriverName
 
OGRSpatialReferenceH mOgrRef
 
SymbologyExport mSymbologyExport
 
double mSymbologyScaleDenominator
 Scale for symbology export (e.g. for symbols units in map units) More...
 
QgsWkbTypes::Type mWkbType
 Geometry type which is being used. More...
 

Detailed Description

A convenience class for writing vector files to disk.

There are two possibilities how to use this class:

  1. 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 39 of file qgsvectorfilewriter.h.

Member Enumeration Documentation

§ ActionOnExistingFile

Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteLayer.

Note
Added in QGIS 3.0 Enumeration to describe how to handle existing files
Added in QGIS 3.0
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 226 of file qgsvectorfilewriter.h.

§ EditionCapability

Edition capability flags.

Note
Added in QGIS 3.0
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 204 of file qgsvectorfilewriter.h.

§ OptionType

Enumerator
Set 
String 
Int 
Hidden 

Definition at line 42 of file qgsvectorfilewriter.h.

§ SymbologyExport

Enumerator
NoSymbology 
FeatureSymbology 
SymbolLayerSymbology 

Definition at line 168 of file qgsvectorfilewriter.h.

§ WriterError

Enumerator
NoError 
ErrDriverNotFound 
ErrCreateDataSource 
ErrCreateLayer 
ErrAttributeTypeUnsupported 
ErrAttributeCreationFailed 
ErrProjection 
ErrFeatureWriteFailed 
ErrInvalidLayer 

Definition at line 155 of file qgsvectorfilewriter.h.

Constructor & Destructor Documentation

§ QgsVectorFileWriter()

QgsVectorFileWriter::QgsVectorFileWriter ( const QString &  vectorFileName,
const QString &  fileEncoding,
const QgsFields fields,
QgsWkbTypes::Type  geometryType,
const QgsCoordinateReferenceSystem srs = QgsCoordinateReferenceSystem(),
const QString &  driverName = "ESRI Shapefile",
const QStringList &  datasourceOptions = QStringList(),
const QStringList &  layerOptions = QStringList(),
QString *  newFilename = nullptr,
SymbologyExport  symbologyExport = NoSymbology 
)

Create a new vector file writer.

Definition at line 77 of file qgsvectorfilewriter.cpp.

§ ~QgsVectorFileWriter()

QgsVectorFileWriter::~QgsVectorFileWriter ( )

Close opened shapefile for writing.

Definition at line 2185 of file qgsvectorfilewriter.cpp.

Member Function Documentation

§ addFeature()

bool QgsVectorFileWriter::addFeature ( QgsFeature feature,
QgsFeatureRenderer renderer = nullptr,
QgsUnitTypes::DistanceUnit  outputUnit = QgsUnitTypes::DistanceMeters 
)

Add feature to the currently opened data source.

Definition at line 1878 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.

Note
added in QGIS 3.0

Definition at line 3141 of file qgsvectorfilewriter.cpp.

§ attrIdxToOgrIdx()

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

Definition at line 452 of file qgsvectorfilewriter.h.

§ 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 2735 of file qgsvectorfilewriter.cpp.

§ createEmptyGeometry()

OGRGeometryH QgsVectorFileWriter::createEmptyGeometry ( QgsWkbTypes::Type  wkbType)
protected
Note
not available in python bindings

Definition at line 687 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
Note
added in QGIS 3.0
See also
defaultLayerOptions()

Definition at line 1837 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
Note
added in QGIS 3.0
See also
defaultDatasetOptions()

Definition at line 1846 of file qgsvectorfilewriter.cpp.

§ deleteShapeFile()

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

Delete a shapefile (and its accompanying shx / dbf / prf)

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

Definition at line 2586 of file qgsvectorfilewriter.cpp.

§ driverMetadata()

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

Definition at line 1819 of file qgsvectorfilewriter.cpp.

§ editionCapabilities()

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

Return edition capabilities for an existing dataset name.

Note
added in QGIS 3.0

Definition at line 3083 of file qgsvectorfilewriter.cpp.

§ errorMessage()

QString QgsVectorFileWriter::errorMessage ( )

Retrieves error message.

Definition at line 1873 of file qgsvectorfilewriter.cpp.

§ fileFilterString()

QString QgsVectorFileWriter::fileFilterString ( )
static

Returns filter string that can be used for dialogs.

Definition at line 2711 of file qgsvectorfilewriter.cpp.

§ filterForDriver()

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

Creates a filter for an OGR driver key.

Definition at line 2726 of file qgsvectorfilewriter.cpp.

§ hasError()

QgsVectorFileWriter::WriterError QgsVectorFileWriter::hasError ( )

Checks whether there were any errors in constructor.

Definition at line 1868 of file qgsvectorfilewriter.cpp.

§ ogrDriverList()

QMap< QString, QString > QgsVectorFileWriter::ogrDriverList ( )
static

Returns driver list that can be used for dialogs.

It contains all OGR drivers

  • some additional internal QGIS driver names to distinguish between more supported formats of the same OGR driver

Definition at line 2645 of file qgsvectorfilewriter.cpp.

§ ogrTypeFromWkbType()

OGRwkbGeometryType QgsVectorFileWriter::ogrTypeFromWkbType ( QgsWkbTypes::Type  type)
static

Get 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 1855 of file qgsvectorfilewriter.cpp.

§ setSymbologyExport()

void QgsVectorFileWriter::setSymbologyExport ( SymbologyExport  symExport)
inline

Definition at line 464 of file qgsvectorfilewriter.h.

§ setSymbologyScaleDenominator()

void QgsVectorFileWriter::setSymbologyScaleDenominator ( double  d)

Definition at line 2612 of file qgsvectorfilewriter.cpp.

§ supportedFiltersAndFormats()

QMap< QString, QString > QgsVectorFileWriter::supportedFiltersAndFormats ( )
static

Returns map with format filter string as key and OGR format key as value.

Definition at line 2618 of file qgsvectorfilewriter.cpp.

§ symbologyExport()

SymbologyExport QgsVectorFileWriter::symbologyExport ( ) const
inline

Definition at line 463 of file qgsvectorfilewriter.h.

§ symbologyScaleDenominator()

double QgsVectorFileWriter::symbologyScaleDenominator ( ) const
inline

Definition at line 466 of file qgsvectorfilewriter.h.

§ targetLayerExists()

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

Returns whether the target layer already exists.

Note
added in QGIS 3.0

Definition at line 3123 of file qgsvectorfilewriter.cpp.

§ writeAsVectorFormat() [1/3]

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

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

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
errorMessagepointer to buffer fo error message
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 a null pointer, 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)

Definition at line 2204 of file qgsvectorfilewriter.cpp.

§ writeAsVectorFormat() [2/3]

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat ( QgsVectorLayer layer,
const QString &  fileName,
const QString &  fileEncoding,
const QgsCoordinateTransform ct,
const QString &  driverName = "ESRI Shapefile",
bool  onlySelected = false,
QString *  errorMessage = nullptr,
const QStringList &  datasourceOptions = QStringList(),
const QStringList &  layerOptions = QStringList(),
bool  skipAttributeCreation = false,
QString *  newFilename = nullptr,
SymbologyExport  symbologyExport = NoSymbology,
double  symbologyScale = 1.0,
const QgsRectangle filterExtent = nullptr,
QgsWkbTypes::Type  overrideGeometryType = QgsWkbTypes::Unknown,
bool  forceMulti = false,
bool  includeZ = false,
const QgsAttributeList attributes = QgsAttributeList(),
FieldValueConverter fieldValueConverter = 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
errorMessagepointer to buffer fo error message
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 a null pointer, 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)
Note
added in 2.2

Definition at line 2238 of file qgsvectorfilewriter.cpp.

§ writeAsVectorFormat() [3/3]

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat ( QgsVectorLayer layer,
const QString &  fileName,
const SaveVectorOptions options,
QString *  newFilename = nullptr,
QString *  errorMessage = 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).
errorMessagepointer to buffer fo error message
Note
added in 3.0

Definition at line 2304 of file qgsvectorfilewriter.cpp.

Member Data Documentation

§ mAttrIdxToOgrIdx

QMap<int, int> QgsVectorFileWriter::mAttrIdxToOgrIdx
protected

Map attribute indizes to OGR field indexes.

Definition at line 536 of file qgsvectorfilewriter.h.

§ mCodec

QTextCodec* QgsVectorFileWriter::mCodec
protected

Definition at line 530 of file qgsvectorfilewriter.h.

§ mDS

OGRDataSourceH QgsVectorFileWriter::mDS
protected

Definition at line 519 of file qgsvectorfilewriter.h.

§ mError

WriterError QgsVectorFileWriter::mError
protected

Contains error value if construction was not successful.

Definition at line 527 of file qgsvectorfilewriter.h.

§ mErrorMessage

QString QgsVectorFileWriter::mErrorMessage
protected

Definition at line 528 of file qgsvectorfilewriter.h.

§ mFields

QgsFields QgsVectorFileWriter::mFields
protected

Definition at line 524 of file qgsvectorfilewriter.h.

§ mFieldValueConverter

FieldValueConverter* QgsVectorFileWriter::mFieldValueConverter
protected

Field value converter.

Definition at line 550 of file qgsvectorfilewriter.h.

§ mGeom

OGRGeometryH QgsVectorFileWriter::mGeom
protected

Definition at line 522 of file qgsvectorfilewriter.h.

§ mLayer

OGRLayerH QgsVectorFileWriter::mLayer
protected

Definition at line 520 of file qgsvectorfilewriter.h.

§ mOgrDriverName

QString QgsVectorFileWriter::mOgrDriverName
protected

Definition at line 547 of file qgsvectorfilewriter.h.

§ mOgrRef

OGRSpatialReferenceH QgsVectorFileWriter::mOgrRef
protected

Definition at line 521 of file qgsvectorfilewriter.h.

§ mSymbologyExport

SymbologyExport QgsVectorFileWriter::mSymbologyExport
protected

Definition at line 538 of file qgsvectorfilewriter.h.

§ mSymbologyScaleDenominator

double QgsVectorFileWriter::mSymbologyScaleDenominator
protected

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

Definition at line 545 of file qgsvectorfilewriter.h.

§ mWkbType

QgsWkbTypes::Type QgsVectorFileWriter::mWkbType
protected

Geometry type which is being used.

Definition at line 533 of file qgsvectorfilewriter.h.


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