QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
QgsVectorFileWriter Class Reference

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

#include <qgsvectorfilewriter.h>

Collaboration diagram for QgsVectorFileWriter:
Collaboration graph
[legend]

Classes

class  BoolOption
 
class  HiddenOption
 
class  IntOption
 
struct  MetaData
 
class  Option
 
class  SetOption
 
class  StringOption
 

Public Types

enum  OptionType { Set, String, Int, Hidden }
 
enum  WriterError {
  NoError = 0, ErrDriverNotFound, ErrCreateDataSource, ErrCreateLayer,
  ErrAttributeTypeUnsupported, ErrAttributeCreationFailed, ErrProjection, ErrFeatureWriteFailed,
  ErrInvalidLayer
}
 
enum  SymbologyExport { NoSymbology = 0, FeatureSymbology, SymbolLayerSymbology }
 

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 More...
 
WriterError hasError ()
 checks whether there were any errors in constructor More...
 
QString errorMessage ()
 retrieves error message More...
 
bool addFeature (QgsFeature &feature, QgsFeatureRendererV2 *renderer=0, QGis::UnitType outputUnit=QGis::Meters)
 add feature to the currently opened shapefile More...
 
QMap< int, int > attrIdxToOgrIdx ()
 
 ~QgsVectorFileWriter ()
 close opened shapefile for writing More...
 
SymbologyExport symbologyExport () const
 
void setSymbologyExport (SymbologyExport symExport)
 
double symbologyScaleDenominator () const
 
void setSymbologyScaleDenominator (double d)
 

Static Public Member Functions

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, const QgsRectangle *filterExtent=0)
 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=0, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=0, SymbologyExport symbologyExport=NoSymbology, double symbologyScale=1.0, const QgsRectangle *filterExtent=0)
 
static QMap< QString, QString > supportedFiltersAndFormats ()
 Returns map with format filter string as key and OGR format key as value. More...
 
static QMap< QString, QString > ogrDriverList ()
 Returns driver list that can be used for dialogs. 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 QString convertCodecNameForEncodingOption (const QString &codecName)
 Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile. More...
 
static bool deleteShapeFile (QString theFileName)
 Delete a shapefile (and its accompanying shx / dbf / prf) More...
 
static bool driverMetadata (const QString &driverName, MetaData &driverMetadata)
 

Protected Member Functions

OGRGeometryH createEmptyGeometry (QGis::WkbType wkbType)
 

Protected Attributes

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

Private Member Functions

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

Static Private Member Functions

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

Static Private Attributes

static QMap< QString, MetaDatasDriverMetadata
 

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.

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 
)
QgsVectorFileWriter::~QgsVectorFileWriter ( )

close opened shapefile for writing

Definition at line 1777 of file qgsvectorfilewriter.cpp.

References mDS, and mGeom.

Member Function Documentation

bool QgsVectorFileWriter::addFeature ( QgsFeature feature,
QgsFeatureRendererV2 renderer = 0,
QGis::UnitType  outputUnit = QGis::Meters 
)
void QgsVectorFileWriter::addRendererAttributes ( QgsVectorLayer vl,
QgsAttributeList attList 
)
private

Adds attributes needed for classification.

Definition at line 2643 of file qgsvectorfilewriter.cpp.

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

Referenced by writeAsVectorFormat().

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

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

Referenced by QgsVectorFileWriter().

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

Definition at line 490 of file qgsvectorfilewriter.cpp.

Referenced by createFeature(), and QgsVectorFileWriter().

OGRFeatureH QgsVectorFileWriter::createFeature ( QgsFeature feature)
private
void QgsVectorFileWriter::createSymbolLayerTable ( QgsVectorLayer vl,
const QgsCoordinateTransform ct,
OGRDataSourceH  ds 
)
private
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 2029 of file qgsvectorfilewriter.cpp.

References file, and QgsDebugMsg.

Referenced by QgsPointSample::createRandomPoints(), and QgsVectorFileWriter().

bool QgsVectorFileWriter::driverMetadata ( const QString &  driverName,
QgsVectorFileWriter::MetaData driverMetadata 
)
static
bool QgsVectorFileWriter::driverMetadata ( QString  driverName,
QString &  longName,
QString &  trLongName,
QString &  glob,
QString &  ext 
)
staticprivate
Deprecated:

Definition at line 2197 of file qgsvectorfilewriter.cpp.

References tr.

QString QgsVectorFileWriter::errorMessage ( )

retrieves error message

Note
added in 1.5

Definition at line 1549 of file qgsvectorfilewriter.cpp.

References mErrorMessage.

Referenced by writeAsVectorFormat().

QgsVectorFileWriter::WriterError QgsVectorFileWriter::exportFeaturesSymbolLevels ( QgsVectorLayer layer,
QgsFeatureIterator fit,
const QgsCoordinateTransform ct,
QString *  errorMessage = 0 
)
private
QString QgsVectorFileWriter::fileFilterString ( )
static

Returns filter string that can be used for dialogs.

Definition at line 2153 of file qgsvectorfilewriter.cpp.

References supportedFiltersAndFormats().

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

Creates a filter for an OGR driver key.

Definition at line 2168 of file qgsvectorfilewriter.cpp.

References driverMetadata().

Referenced by QgsNewVectorLayerDialog::runAndCreateLayer(), and supportedFiltersAndFormats().

QgsVectorFileWriter::WriterError QgsVectorFileWriter::hasError ( )

checks whether there were any errors in constructor

Definition at line 1544 of file qgsvectorfilewriter.cpp.

References mError.

Referenced by QgsPointSample::createRandomPoints(), QgsTransectSample::createSample(), and writeAsVectorFormat().

QMap< QString, QgsVectorFileWriter::MetaData > QgsVectorFileWriter::initMetaData ( )
staticprivate

Definition at line 495 of file qgsvectorfilewriter.cpp.

References driverMetadata(), and tr.

Referenced by driverMetadata().

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

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

QgsRenderContext QgsVectorFileWriter::renderContext ( ) const
private
void QgsVectorFileWriter::setSymbologyExport ( SymbologyExport  symExport)
inline

Definition at line 269 of file qgsvectorfilewriter.h.

void QgsVectorFileWriter::setSymbologyScaleDenominator ( double  d)
inline

Definition at line 272 of file qgsvectorfilewriter.h.

Referenced by writeAsVectorFormat().

void QgsVectorFileWriter::startRender ( QgsVectorLayer vl) const
private
void QgsVectorFileWriter::stopRender ( QgsVectorLayer vl) const
private
QMap< QString, QString > QgsVectorFileWriter::supportedFiltersAndFormats ( )
static

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

Definition at line 2054 of file qgsvectorfilewriter.cpp.

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

Referenced by fileFilterString().

SymbologyExport QgsVectorFileWriter::symbologyExport ( ) const
inline

Definition at line 268 of file qgsvectorfilewriter.h.

Referenced by writeAsVectorFormat().

QgsFeatureRendererV2 * QgsVectorFileWriter::symbologyRenderer ( QgsVectorLayer vl) const
private
double QgsVectorFileWriter::symbologyScaleDenominator ( ) const
inline

Definition at line 271 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,
const QgsRectangle filterExtent = 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
filterExtentif not a null pointer, only features intersecting the extent will be saved

Definition at line 1791 of file qgsvectorfilewriter.cpp.

References QgsMapLayer::crs().

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 = 0,
const QStringList &  datasourceOptions = QStringList(),
const QStringList &  layerOptions = QStringList(),
bool  skipAttributeCreation = false,
QString *  newFilename = 0,
SymbologyExport  symbologyExport = NoSymbology,
double  symbologyScale = 1.0,
const QgsRectangle filterExtent = 0 
)
static
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 296 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().

QTextCodec* QgsVectorFileWriter::mCodec
protected

Definition at line 290 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().

OGRDataSourceH QgsVectorFileWriter::mDS
protected

Definition at line 280 of file qgsvectorfilewriter.h.

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

WriterError QgsVectorFileWriter::mError
protected

contains error value if construction was not successful

Definition at line 287 of file qgsvectorfilewriter.h.

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

QString QgsVectorFileWriter::mErrorMessage
protected
QgsFields QgsVectorFileWriter::mFields
protected

Definition at line 284 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().

OGRGeometryH QgsVectorFileWriter::mGeom
protected

Definition at line 282 of file qgsvectorfilewriter.h.

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

OGRLayerH QgsVectorFileWriter::mLayer
protected
SymbologyExport QgsVectorFileWriter::mSymbologyExport
protected

Definition at line 298 of file qgsvectorfilewriter.h.

Referenced by addFeature(), and symbologyRenderer().

double QgsVectorFileWriter::mSymbologyScaleDenominator
protected

Scale for symbology export (e.g.

for symbols units in map units)

Definition at line 305 of file qgsvectorfilewriter.h.

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

QGis::WkbType QgsVectorFileWriter::mWkbType
protected

geometry type which is being used

Definition at line 293 of file qgsvectorfilewriter.h.

Referenced by createFeature(), and QgsVectorFileWriter().

QMap<QString, MetaData> QgsVectorFileWriter::sDriverMetadata
staticprivate

Definition at line 327 of file qgsvectorfilewriter.h.

Referenced by driverMetadata().


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