QGIS API Documentation
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  HiddenOption
 
class  IntOption
 
struct  MetaData
 
class  Option
 
class  SetOption
 
class  StringOption
 

Public Types

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, QGis::WkbType geometryType, const QgsCoordinateReferenceSystem *srs, 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 (const QString &vectorFileName, const QString &fileEncoding, const QgsFields &fields, QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem *srs, 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, QgsFeatureRendererV2 *renderer=nullptr, QGis::UnitType outputUnit=QGis::Meters)
 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 QString convertCodecNameForEncodingOption (const QString &codecName)
 Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile. 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 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, QStringogrDriverList ()
 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, QStringsupportedFiltersAndFormats ()
 Returns map with format filter string as key and OGR format key as value. More...
 
static WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateReferenceSystem *destCRS, 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, QgsAttributeList attributes=QgsAttributeList())
 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, QgsAttributeList attributes=QgsAttributeList())
 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...
 
QTextCodecmCodec
 
OGRDataSourceH mDS
 
WriterError mError
 Contains error value if construction was not successful. More...
 
QString mErrorMessage
 
QgsFields mFields
 
OGRGeometryH mGeom
 
OGRLayerH mLayer
 
QString mOgrDriverName
 
OGRSpatialReferenceH mOgrRef
 
SymbologyExport mSymbologyExport
 
double mSymbologyScaleDenominator
 Scale for symbology export (e.g. 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::writeAsShapefile(...) which saves the whole vector layer
  2. create an instance of the class and issue calls to addFeature(...)

Currently supports only writing to shapefiles, but shouldn't be a problem to add capability to support other OGR-writable formats.

Definition at line 42 of file qgsvectorfilewriter.h.

Member Enumeration Documentation

Enumerator
Set 
String 
Int 
Hidden 

Definition at line 45 of file qgsvectorfilewriter.h.

Enumerator
NoSymbology 
FeatureSymbology 
SymbolLayerSymbology 

Definition at line 156 of file qgsvectorfilewriter.h.

Enumerator
NoError 
ErrDriverNotFound 
ErrCreateDataSource 
ErrCreateLayer 
ErrAttributeTypeUnsupported 
ErrAttributeCreationFailed 
ErrProjection 
ErrFeatureWriteFailed 
ErrInvalidLayer 

Definition at line 143 of file qgsvectorfilewriter.h.

Constructor & Destructor Documentation

QgsVectorFileWriter::QgsVectorFileWriter ( const QString vectorFileName,
const QString fileEncoding,
const QgsFields fields,
QGis::WkbType  geometryType,
const QgsCoordinateReferenceSystem srs,
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 56 of file qgsvectorfilewriter.cpp.

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

Create a new vector file writer.

Definition at line 81 of file qgsvectorfilewriter.cpp.

QgsVectorFileWriter::~QgsVectorFileWriter ( )

Close opened shapefile for writing.

Definition at line 2014 of file qgsvectorfilewriter.cpp.

Member Function Documentation

bool QgsVectorFileWriter::addFeature ( QgsFeature feature,
QgsFeatureRendererV2 renderer = nullptr,
QGis::UnitType  outputUnit = QGis::Meters 
)

Add feature to the currently opened data source.

Definition at line 1715 of file qgsvectorfilewriter.cpp.

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

Definition at line 300 of file qgsvectorfilewriter.h.

QString QgsVectorFileWriter::convertCodecNameForEncodingOption ( const QString codecName)
static

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

Definition at line 2504 of file qgsvectorfilewriter.cpp.

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

Definition at line 538 of file qgsvectorfilewriter.cpp.

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

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

Definition at line 1674 of file qgsvectorfilewriter.cpp.

QString QgsVectorFileWriter::errorMessage ( )

Retrieves error message.

Definition at line 1710 of file qgsvectorfilewriter.cpp.

QString QgsVectorFileWriter::fileFilterString ( )
static

Returns filter string that can be used for dialogs.

Definition at line 2477 of file qgsvectorfilewriter.cpp.

QString QgsVectorFileWriter::filterForDriver ( const QString driverName)
static

Creates a filter for an OGR driver key.

Definition at line 2492 of file qgsvectorfilewriter.cpp.

QgsVectorFileWriter::WriterError QgsVectorFileWriter::hasError ( )

Checks whether there were any errors in constructor.

Definition at line 1705 of file qgsvectorfilewriter.cpp.

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

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

void QgsVectorFileWriter::setSymbologyExport ( SymbologyExport  symExport)
inline

Definition at line 312 of file qgsvectorfilewriter.h.

void QgsVectorFileWriter::setSymbologyScaleDenominator ( double  d)

Definition at line 2372 of file qgsvectorfilewriter.cpp.

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

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

Definition at line 2378 of file qgsvectorfilewriter.cpp.

SymbologyExport QgsVectorFileWriter::symbologyExport ( ) const
inline

Definition at line 311 of file qgsvectorfilewriter.h.

double QgsVectorFileWriter::symbologyScaleDenominator ( ) const
inline

Definition at line 314 of file qgsvectorfilewriter.h.

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat ( QgsVectorLayer layer,
const QString fileName,
const QString fileEncoding,
const QgsCoordinateReferenceSystem destCRS,
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,
QgsAttributeList  attributes = QgsAttributeList() 
)
static

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

Parameters
layerlayer to write
fileNamefile name to write to
fileEncodingencoding to use
destCRSpointer to CRS to reproject exported geometries to
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)

Definition at line 2033 of file qgsvectorfilewriter.cpp.

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,
QgsAttributeList  attributes = QgsAttributeList() 
)
static

Writes a layer out to a vector file.

Parameters
layerlayer to write
fileNamefile name to write to
fileEncodingencoding to use
ctpointer to coordinate transform to reproject exported geometries with
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)
Note
added in 2.2

Definition at line 2064 of file qgsvectorfilewriter.cpp.

Member Data Documentation

QMap<int, int> QgsVectorFileWriter::mAttrIdxToOgrIdx
protected

Map attribute indizes to OGR field indexes.

Definition at line 348 of file qgsvectorfilewriter.h.

QTextCodec* QgsVectorFileWriter::mCodec
protected

Definition at line 342 of file qgsvectorfilewriter.h.

OGRDataSourceH QgsVectorFileWriter::mDS
protected

Definition at line 331 of file qgsvectorfilewriter.h.

WriterError QgsVectorFileWriter::mError
protected

Contains error value if construction was not successful.

Definition at line 339 of file qgsvectorfilewriter.h.

QString QgsVectorFileWriter::mErrorMessage
protected

Definition at line 340 of file qgsvectorfilewriter.h.

QgsFields QgsVectorFileWriter::mFields
protected

Definition at line 336 of file qgsvectorfilewriter.h.

OGRGeometryH QgsVectorFileWriter::mGeom
protected

Definition at line 334 of file qgsvectorfilewriter.h.

OGRLayerH QgsVectorFileWriter::mLayer
protected

Definition at line 332 of file qgsvectorfilewriter.h.

QString QgsVectorFileWriter::mOgrDriverName
protected

Definition at line 359 of file qgsvectorfilewriter.h.

OGRSpatialReferenceH QgsVectorFileWriter::mOgrRef
protected

Definition at line 333 of file qgsvectorfilewriter.h.

SymbologyExport QgsVectorFileWriter::mSymbologyExport
protected

Definition at line 350 of file qgsvectorfilewriter.h.

double QgsVectorFileWriter::mSymbologyScaleDenominator
protected

Scale for symbology export (e.g.

for symbols units in map units)

Definition at line 357 of file qgsvectorfilewriter.h.

QgsWKBTypes::Type QgsVectorFileWriter::mWkbType
protected

Geometry type which is being used.

Definition at line 345 of file qgsvectorfilewriter.h.


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