QGIS API Documentation  2.6.0-Brighton
 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:
46  {
47  Set,
49  Int,
50  Hidden
51  };
52 
53  class Option
54  {
55  public:
56  Option( const QString& docString, OptionType type )
57  : docString( docString )
58  , type( type ) {}
59  virtual ~Option() {}
60 
61  QString docString;
63  };
64 
65  class SetOption : public Option
66  {
67  public:
68  SetOption( const QString& docString, QStringList values, const QString& defaultValue, bool allowNone = false )
69  : Option( docString, Set )
70  , values( values.toSet() )
71  , defaultValue( defaultValue )
72  , allowNone( allowNone )
73  {}
74 
75  QSet<QString> values;
76  QString defaultValue;
77  bool allowNone;
78  };
79 
80  class StringOption: public Option
81  {
82  public:
83  StringOption( const QString& docString, const QString& defaultValue = QString() )
84  : Option( docString, String )
85  , defaultValue( defaultValue )
86  {}
87 
88  QString defaultValue;
89  };
90 
91  class IntOption: public Option
92  {
93  public:
94  IntOption( const QString& docString, int defaultValue )
95  : Option( docString, Int )
96  , defaultValue( defaultValue )
97  {}
98 
100  };
101 
102  class BoolOption : public SetOption
103  {
104  public:
105  BoolOption( const QString& docString, bool defaultValue )
106  : SetOption( docString, QStringList() << "YES" << "NO", defaultValue ? "YES" : "NO" )
107  {}
108  };
109 
110  class HiddenOption : public Option
111  {
112  public:
113  HiddenOption( const QString& value )
114  : Option( "", Hidden )
115  , mValue( value )
116  {}
117 
118  QString mValue;
119  };
120 
121  struct MetaData
122  {
124  {}
125 
126  MetaData( QString longName, QString trLongName, QString glob, QString ext, QMap<QString, Option*> driverOptions, QMap<QString, Option*> layerOptions )
127  : longName( longName )
128  , trLongName( trLongName )
129  , glob( glob )
130  , ext( ext )
131  , driverOptions( driverOptions )
132  , layerOptions( layerOptions )
133  {}
134 
135  QString longName;
136  QString trLongName;
137  QString glob;
138  QString ext;
139  QMap<QString, Option*> driverOptions;
140  QMap<QString, Option*> layerOptions;
141  };
142 
144  {
145  NoError = 0,
154  };
155 
157  {
158  NoSymbology = 0, //export only data
159  FeatureSymbology, //Keeps the number of features and export symbology per feature
160  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
161  };
162 
179  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
180  const QString& fileName,
181  const QString& fileEncoding,
182  const QgsCoordinateReferenceSystem *destCRS,
183  const QString& driverName = "ESRI Shapefile",
184  bool onlySelected = false,
185  QString *errorMessage = 0,
186  const QStringList &datasourceOptions = QStringList(),
187  const QStringList &layerOptions = QStringList(),
188  bool skipAttributeCreation = false,
189  QString *newFilename = 0,
190  SymbologyExport symbologyExport = NoSymbology,
191  double symbologyScale = 1.0,
192  const QgsRectangle* filterExtent = 0 // added in 2.4
193  );
194 
196  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
197  const QString& fileName,
198  const QString& fileEncoding,
199  const QgsCoordinateTransform* ct,
200  const QString& driverName = "ESRI Shapefile",
201  bool onlySelected = false,
202  QString *errorMessage = 0,
203  const QStringList &datasourceOptions = QStringList(),
204  const QStringList &layerOptions = QStringList(),
205  bool skipAttributeCreation = false,
206  QString *newFilename = 0,
207  SymbologyExport symbologyExport = NoSymbology,
208  double symbologyScale = 1.0,
209  const QgsRectangle* filterExtent = 0 // added in 2.4
210  );
211 
213  QgsVectorFileWriter( const QString& vectorFileName,
214  const QString& fileEncoding,
215  const QgsFields& fields,
216  QGis::WkbType geometryType,
217  const QgsCoordinateReferenceSystem* srs,
218  const QString& driverName = "ESRI Shapefile",
219  const QStringList &datasourceOptions = QStringList(),
220  const QStringList &layerOptions = QStringList(),
221  QString *newFilename = 0,
222  SymbologyExport symbologyExport = NoSymbology
223  );
224 
226  static QMap< QString, QString> supportedFiltersAndFormats();
227 
232  static QMap< QString, QString> ogrDriverList();
233 
235  static QString fileFilterString();
236 
238  static QString filterForDriver( const QString& driverName );
239 
241  static QString convertCodecNameForEncodingOption( const QString &codecName );
242 
244  WriterError hasError();
245 
247  QString errorMessage();
248 
250  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = 0, QGis::UnitType outputUnit = QGis::Meters );
251 
253  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
254 
257 
262  static bool deleteShapeFile( QString theFileName );
263 
264  SymbologyExport symbologyExport() const { return mSymbologyExport; }
265  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
266 
267  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
268  void setSymbologyScaleDenominator( double d ) { mSymbologyScaleDenominator = d; }
269 
270  static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
271 
272  protected:
274  OGRGeometryH createEmptyGeometry( QGis::WkbType wkbType );
275 
276  OGRDataSourceH mDS;
277  OGRLayerH mLayer;
278  OGRGeometryH mGeom;
279 
281 
284  QString mErrorMessage;
285 
286  QTextCodec *mCodec;
287 
290 
292  QMap<int, int> mAttrIdxToOgrIdx;
293 
295 
296 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
297  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
298 #endif
299 
302 
303  private:
304  static QMap<QString, MetaData> initMetaData();
308  static bool driverMetadata( QString driverName, QString &longName, QString &trLongName, QString &glob, QString &ext );
309  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
310  OGRFeatureH createFeature( QgsFeature& feature );
311  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
312 
314  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = 0 );
315  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
316  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
317  QgsRenderContext renderContext() const;
318  void startRender( QgsVectorLayer* vl ) const;
319  void stopRender( QgsVectorLayer* vl ) const;
320  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
322  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
323  static QMap<QString, MetaData> sDriverMetadata;
324 };
325 
326 #endif