QGIS API Documentation  master-3f58142
QgsVectorFileWriter Class Reference

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

#include <qgsvectorfilewriter.h>

+ Collaboration diagram for QgsVectorFileWriter:

List of all members.

Public Types

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=0, SymbologyExport symbologyExport=NoSymbology)
 create shapefile and initialize it
 ~QgsVectorFileWriter ()
 close opened shapefile for writing
bool addFeature (QgsFeature &feature, QgsFeatureRendererV2 *renderer=0, QGis::UnitType outputUnit=QGis::Meters)
 add feature to the currently opened shapefile
QMap< int, int > attrIdxToOgrIdx ()
QString errorMessage ()
 retrieves error message
WriterError hasError ()
 checks whether there were any errors in constructor
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.
static bool deleteShapeFile (QString theFileName)
 Delete a shapefile (and its accompanying shx / dbf / prf)
static QString fileFilterString ()
 Returns filter string that can be used for dialogs.
static QString filterForDriver (const QString &driverName)
 Creates a filter for an OGR driver key.
static QMap< QString, QString > ogrDriverList ()
 Returns driver list that can be used for dialogs.
static QMap< QString, QString > supportedFiltersAndFormats ()
 Returns map with format filter string as key and OGR format key as value.
static WriterError writeAsVectorFormat (QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateReferenceSystem *destCRS, const QString &driverName="ESRI Shapefile", bool onlySelected=false, QString *errorMessage=0, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=0, SymbologyExport symbologyExport=NoSymbology, double symbologyScale=1.0)
 Write contents of vector layer to an (OGR supported) vector formt.

Protected Member Functions

OGRGeometryH createEmptyGeometry (QGis::WkbType wkbType)

Protected Attributes

QMap< int, int > mAttrIdxToOgrIdx
 map attribute indizes to OGR field indexes
QTextCodec * mCodec
OGRDataSourceH mDS
WriterError mError
 contains error value if construction was not successful
QString mErrorMessage
QgsFields mFields
OGRGeometryH mGeom
OGRLayerH mLayer
SymbologyExport mSymbologyExport
double mSymbologyScaleDenominator
 Scale for symbology export (e.g.
QGis::WkbType mWkbType
 geometry type which is being used

Private Member Functions

void addRendererAttributes (QgsVectorLayer *vl, QgsAttributeList &attList)
 Adds attributes needed for classification.
OGRFeatureH createFeature (QgsFeature &feature)
void createSymbolLayerTable (QgsVectorLayer *vl, const QgsCoordinateTransform *ct, OGRDataSourceH ds)
WriterError exportFeaturesSymbolLevels (QgsVectorLayer *layer, QgsFeatureIterator &fit, const QgsCoordinateTransform *ct, QString *errorMessage=0)
 Writes features considering symbol level order.
double mapUnitScaleFactor (double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits)
double mmScaleFactor (double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits)
QgsRenderContext renderContext () const
void startRender (QgsVectorLayer *vl) const
void stopRender (QgsVectorLayer *vl) const
QgsFeatureRendererV2symbologyRenderer (QgsVectorLayer *vl) const
bool writeFeature (OGRLayerH layer, OGRFeatureH feature)

Static Private Member Functions

static bool driverMetadata (QString driverName, QString &longName, QString &trLongName, QString &glob, QString &ext)

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:
NoSymbology 
FeatureSymbology 
SymbolLayerSymbology 

Definition at line 60 of file qgsvectorfilewriter.h.

Enumerator:
NoError 
ErrDriverNotFound 
ErrCreateDataSource 
ErrCreateLayer 
ErrAttributeTypeUnsupported 
ErrAttributeCreationFailed 
ErrProjection 
ErrFeatureWriteFailed 
ErrInvalidLayer 

Definition at line 46 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 = 0,
SymbologyExport  symbologyExport = NoSymbology 
)

close opened shapefile for writing

Definition at line 671 of file qgsvectorfilewriter.cpp.

References mDS, and mGeom.


Member Function Documentation

Adds attributes needed for classification.

Definition at line 1482 of file qgsvectorfilewriter.cpp.

References QgsVectorLayer::fieldNameIndex(), symbologyRenderer(), and QgsFeatureRendererV2::usedAttributes().

Referenced by writeAsVectorFormat().

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

Definition at line 141 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 1026 of file qgsvectorfilewriter.cpp.

Referenced by QgsVectorFileWriter().

OGRGeometryH QgsVectorFileWriter::createEmptyGeometry ( QGis::WkbType  wkbType) [protected]
Note:
not available in python bindings

Definition at line 434 of file qgsvectorfilewriter.cpp.

Referenced by createFeature(), and QgsVectorFileWriter().

bool QgsVectorFileWriter::deleteShapeFile ( 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 880 of file qgsvectorfilewriter.cpp.

References file, and QgsDebugMsg.

Referenced by QgsVectorFileWriter().

bool QgsVectorFileWriter::driverMetadata ( QString  driverName,
QString &  longName,
QString &  trLongName,
QString &  glob,
QString &  ext 
) [static, private]

Definition at line 1043 of file qgsvectorfilewriter.cpp.

References tr.

Referenced by filterForDriver(), ogrDriverList(), and QgsVectorFileWriter().

retrieves error message

Note:
added in 1.5

Definition at line 445 of file qgsvectorfilewriter.cpp.

References mErrorMessage.

Referenced by writeAsVectorFormat().

Returns filter string that can be used for dialogs.

Definition at line 999 of file qgsvectorfilewriter.cpp.

References supportedFiltersAndFormats().

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

Creates a filter for an OGR driver key.

Definition at line 1014 of file qgsvectorfilewriter.cpp.

References driverMetadata().

Referenced by supportedFiltersAndFormats().

checks whether there were any errors in constructor

Definition at line 440 of file qgsvectorfilewriter.cpp.

References mError.

Referenced by writeAsVectorFormat().

double QgsVectorFileWriter::mapUnitScaleFactor ( double  scaleDenominator,
QgsSymbolV2::OutputUnit  symbolUnits,
QGis::UnitType  mapUnits 
) [private]
double QgsVectorFileWriter::mmScaleFactor ( double  scaleDenominator,
QgsSymbolV2::OutputUnit  symbolUnits,
QGis::UnitType  mapUnits 
) [private]
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 932 of file qgsvectorfilewriter.cpp.

References driverMetadata(), QgsApplication::registerOgrDrivers(), and TO8.

Definition at line 153 of file qgsvectorfilewriter.h.

Definition at line 156 of file qgsvectorfilewriter.h.

Referenced by writeAsVectorFormat().

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

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

Definition at line 905 of file qgsvectorfilewriter.cpp.

References filterForDriver(), and QgsApplication::registerOgrDrivers().

Referenced by fileFilterString().

Definition at line 152 of file qgsvectorfilewriter.h.

Referenced by writeAsVectorFormat().

Definition at line 155 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 = 0,
const QStringList &  datasourceOptions = QStringList(),
const QStringList &  layerOptions = QStringList(),
bool  skipAttributeCreation = false,
QString *  newFilename = 0,
SymbologyExport  symbologyExport = NoSymbology,
double  symbologyScale = 1.0 
) [static]

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

Note:
: this method was added in version 1.5
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

Definition at line 685 of file qgsvectorfilewriter.cpp.

References addFeature(), addRendererAttributes(), QgsFeatureRendererV2::capabilities(), QgsMapLayer::crs(), QgsCoordinateTransform::destCRS(), ErrFeatureWriteFailed, ErrInvalidLayer, errorMessage(), ErrProjection, exportFeaturesSymbolLevels(), QgsFeature::geometry(), QgsVectorLayer::getFeatures(), hasError(), QgsFeature::id(), QgsFeature::initAttributes(), QgsCoordinateReferenceSystem::isValid(), QgsCoordinateReferenceSystem::mapUnits(), QgsFeatureIterator::nextFeature(), NoError, QgsFeatureRequest::NoGeometry, NoSymbology, QgsVectorLayer::pendingAllAttributesList(), QgsVectorLayer::pendingFields(), QgsDebugMsg, QgsVectorFileWriter(), QgsVectorLayer::rendererV2(), QgsVectorLayer::selectedFeaturesIds(), QgsFeatureRequest::setFlags(), QgsFeatureRequest::setSubsetOfAttributes(), setSymbologyScaleDenominator(), startRender(), stopRender(), SymbolLayerSymbology, QgsFeatureRendererV2::SymbolLevels, symbologyExport(), tr, QgsGeometry::transform(), QgsFeatureRendererV2::usingSymbolLevels(), QgsLogger::warning(), QgsException::what(), QGis::WKBNoGeometry, and QgsVectorLayer::wkbType().

bool QgsVectorFileWriter::writeFeature ( OGRLayerH  layer,
OGRFeatureH  feature 
) [private]

Member Data Documentation

QMap<int, int> QgsVectorFileWriter::mAttrIdxToOgrIdx [protected]

map attribute indizes to OGR field indexes

Definition at line 178 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().

QTextCodec* QgsVectorFileWriter::mCodec [protected]

Definition at line 172 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().

OGRDataSourceH QgsVectorFileWriter::mDS [protected]

Definition at line 162 of file qgsvectorfilewriter.h.

Referenced by QgsVectorFileWriter(), and ~QgsVectorFileWriter().

contains error value if construction was not successful

Definition at line 169 of file qgsvectorfilewriter.h.

Referenced by createFeature(), hasError(), QgsVectorFileWriter(), and writeFeature().

Definition at line 166 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().

OGRGeometryH QgsVectorFileWriter::mGeom [protected]

Definition at line 164 of file qgsvectorfilewriter.h.

Referenced by createFeature(), QgsVectorFileWriter(), and ~QgsVectorFileWriter().

Scale for symbology export (e.g.

for symbols units in map units)

Definition at line 187 of file qgsvectorfilewriter.h.

Referenced by addFeature(), createSymbolLayerTable(), exportFeaturesSymbolLevels(), and renderContext().

geometry type which is being used

Definition at line 175 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines