QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsvectorfilewriter.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorfilewriter.h
3  generic vector file writer
4  -------------------
5  begin : Jun 6 2004
6  copyright : (C) 2004 by Tim Sutton
7  email : tim at linfiniti.com
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #ifndef _QGSVECTORFILEWRITER_H_
20 #define _QGSVECTORFILEWRITER_H_
21 
22 #include "qgsvectorlayer.h"
23 #include "qgsfield.h"
24 #include "qgssymbolv2.h"
25 #include <ogr_api.h>
26 
27 #include <QPair>
28 
29 
30 class QgsSymbolLayerV2;
31 class QTextCodec;
32 
42 class CORE_EXPORT QgsVectorFileWriter
43 {
44  public:
45 
47  {
48  NoError = 0,
54  ErrProjection, // added in 1.5
55  ErrFeatureWriteFailed, // added in 1.6
56  ErrInvalidLayer, // added in 2.0
57  };
58 
59  //added in 2.0
61  {
62  NoSymbology = 0, //export only data
63  FeatureSymbology, //Keeps the number of features and export symbology per feature
64  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
65  };
66 
83  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
84  const QString& fileName,
85  const QString& fileEncoding,
86  const QgsCoordinateReferenceSystem *destCRS,
87  const QString& driverName = "ESRI Shapefile",
88  bool onlySelected = false,
89  QString *errorMessage = 0,
90  const QStringList &datasourceOptions = QStringList(), // added in 1.6
91  const QStringList &layerOptions = QStringList(), // added in 1.6
92  bool skipAttributeCreation = false, // added in 1.6
93  QString *newFilename = 0, // added in 1.9
94  SymbologyExport symbologyExport = NoSymbology, //added in 2.0
95  double symbologyScale = 1.0 // added in 2.0
96  );
97 
99  QgsVectorFileWriter( const QString& vectorFileName,
100  const QString& fileEncoding,
101  const QgsFields& fields,
102  QGis::WkbType geometryType,
103  const QgsCoordinateReferenceSystem* srs,
104  const QString& driverName = "ESRI Shapefile",
105  const QStringList &datasourceOptions = QStringList(), // added in 1.6
106  const QStringList &layerOptions = QStringList(), // added in 1.6
107  QString *newFilename = 0, // added in 1.9
108  SymbologyExport symbologyExport = NoSymbology//added in 2.0
109  );
110 
112  static QMap< QString, QString> supportedFiltersAndFormats();
113 
118  static QMap< QString, QString> ogrDriverList();
119 
121  static QString fileFilterString();
122 
124  static QString filterForDriver( const QString& driverName );
125 
127  static QString convertCodecNameForEncodingOption( const QString &codecName );
128 
130  WriterError hasError();
131 
135  QString errorMessage();
136 
138  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = 0, QGis::UnitType outputUnit = QGis::Meters );
139 
141  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
142 
145 
150  static bool deleteShapeFile( QString theFileName );
151 
152  SymbologyExport symbologyExport() const { return mSymbologyExport; }
153  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
154 
155  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
156  void setSymbologyScaleDenominator( double d ) { mSymbologyScaleDenominator = d; }
157 
158  protected:
160  OGRGeometryH createEmptyGeometry( QGis::WkbType wkbType );
161 
162  OGRDataSourceH mDS;
163  OGRLayerH mLayer;
164  OGRGeometryH mGeom;
165 
167 
170  QString mErrorMessage;
171 
172  QTextCodec *mCodec;
173 
176 
178  QMap<int, int> mAttrIdxToOgrIdx;
179 
181 
182 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
183  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
184 #endif
185 
188 
189  private:
190  static bool driverMetadata( QString driverName, QString &longName, QString &trLongName, QString &glob, QString &ext );
191  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
192  OGRFeatureH createFeature( QgsFeature& feature );
193  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
194 
196  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = 0 );
197  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
198  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
199  QgsRenderContext renderContext() const;
200  void startRender( QgsVectorLayer* vl ) const;
201  void stopRender( QgsVectorLayer* vl ) const;
202  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
204  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
205 
206 };
207 
208 #endif