QGIS API Documentation  2.4.0-Chugiak
 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,
151  ErrProjection, // added in 1.5
152  ErrFeatureWriteFailed, // added in 1.6
153  ErrInvalidLayer, // added in 2.0
154  };
155 
156  //added in 2.0
158  {
159  NoSymbology = 0, //export only data
160  FeatureSymbology, //Keeps the number of features and export symbology per feature
161  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
162  };
163 
181  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
182  const QString& fileName,
183  const QString& fileEncoding,
184  const QgsCoordinateReferenceSystem *destCRS,
185  const QString& driverName = "ESRI Shapefile",
186  bool onlySelected = false,
187  QString *errorMessage = 0,
188  const QStringList &datasourceOptions = QStringList(), // added in 1.6
189  const QStringList &layerOptions = QStringList(), // added in 1.6
190  bool skipAttributeCreation = false, // added in 1.6
191  QString *newFilename = 0, // added in 1.9
192  SymbologyExport symbologyExport = NoSymbology, //added in 2.0
193  double symbologyScale = 1.0, // added in 2.0
194  const QgsRectangle* filterExtent = 0 // added in 2.4
195  );
196 
198  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
199  const QString& fileName,
200  const QString& fileEncoding,
201  const QgsCoordinateTransform* ct,
202  const QString& driverName = "ESRI Shapefile",
203  bool onlySelected = false,
204  QString *errorMessage = 0,
205  const QStringList &datasourceOptions = QStringList(),
206  const QStringList &layerOptions = QStringList(),
207  bool skipAttributeCreation = false,
208  QString *newFilename = 0,
209  SymbologyExport symbologyExport = NoSymbology,
210  double symbologyScale = 1.0,
211  const QgsRectangle* filterExtent = 0 // added in 2.4
212  );
213 
215  QgsVectorFileWriter( const QString& vectorFileName,
216  const QString& fileEncoding,
217  const QgsFields& fields,
218  QGis::WkbType geometryType,
219  const QgsCoordinateReferenceSystem* srs,
220  const QString& driverName = "ESRI Shapefile",
221  const QStringList &datasourceOptions = QStringList(), // added in 1.6
222  const QStringList &layerOptions = QStringList(), // added in 1.6
223  QString *newFilename = 0, // added in 1.9
224  SymbologyExport symbologyExport = NoSymbology //added in 2.0
225  );
226 
228  static QMap< QString, QString> supportedFiltersAndFormats();
229 
234  static QMap< QString, QString> ogrDriverList();
235 
237  static QString fileFilterString();
238 
240  static QString filterForDriver( const QString& driverName );
241 
243  static QString convertCodecNameForEncodingOption( const QString &codecName );
244 
246  WriterError hasError();
247 
251  QString errorMessage();
252 
254  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = 0, QGis::UnitType outputUnit = QGis::Meters );
255 
257  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
258 
261 
266  static bool deleteShapeFile( QString theFileName );
267 
268  SymbologyExport symbologyExport() const { return mSymbologyExport; }
269  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
270 
271  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
272  void setSymbologyScaleDenominator( double d ) { mSymbologyScaleDenominator = d; }
273 
274  static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
275 
276  protected:
278  OGRGeometryH createEmptyGeometry( QGis::WkbType wkbType );
279 
280  OGRDataSourceH mDS;
281  OGRLayerH mLayer;
282  OGRGeometryH mGeom;
283 
285 
288  QString mErrorMessage;
289 
290  QTextCodec *mCodec;
291 
294 
296  QMap<int, int> mAttrIdxToOgrIdx;
297 
299 
300 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
301  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
302 #endif
303 
306 
307  private:
308  static QMap<QString, MetaData> initMetaData();
312  static bool driverMetadata( QString driverName, QString &longName, QString &trLongName, QString &glob, QString &ext );
313  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
314  OGRFeatureH createFeature( QgsFeature& feature );
315  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
316 
318  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = 0 );
319  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
320  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
321  QgsRenderContext renderContext() const;
322  void startRender( QgsVectorLayer* vl ) const;
323  void stopRender( QgsVectorLayer* vl ) const;
324  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
326  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
327  static QMap<QString, MetaData> sDriverMetadata;
328 };
329 
330 #endif
Wrapper for iterator of features from vector data provider or vector layer.
BoolOption(const QString &docString, bool defaultValue)
A rectangle specified with double values.
Definition: qgsrectangle.h:35
SymbologyExport symbologyExport() const
QMap< int, int > attrIdxToOgrIdx()
WriterError mError
contains error value if construction was not successful
SymbologyExport mSymbologyExport
double mSymbologyScaleDenominator
Scale for symbology export (e.g.
QMap< QString, Option * > driverOptions
Container of fields for a vector layer.
Definition: qgsfield.h:161
WkbType
Used for symbology operations.
Definition: qgis.h:53
StringOption(const QString &docString, const QString &defaultValue=QString())
A convenience class for writing vector files to disk.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
SetOption(const QString &docString, QStringList values, const QString &defaultValue, bool allowNone=false)
IntOption(const QString &docString, int defaultValue)
MetaData(QString longName, QString trLongName, QString glob, QString ext, QMap< QString, Option * > driverOptions, QMap< QString, Option * > layerOptions)
QList< int > QgsAttributeList
QGis::WkbType mWkbType
geometry type which is being used
QMap< int, int > mAttrIdxToOgrIdx
map attribute indizes to OGR field indexes
double symbologyScaleDenominator() const
QMap< QString, Option * > layerOptions
void setSymbologyExport(SymbologyExport symExport)
Contains information about the context of a rendering operation.
void setSymbologyScaleDenominator(double d)
Class for storing a coordinate reference system (CRS)
Option(const QString &docString, OptionType type)
Class for doing transforms between two map coordinate systems.
UnitType
Map units that qgis supports.
Definition: qgis.h:229
Represents a vector layer which manages a vector based data sets.
static QMap< QString, MetaData > sDriverMetadata