QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
Definition: qgsvectorfilewriter.h:151
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:139
BoolOption(const QString &docString, bool defaultValue)
Definition: qgsvectorfilewriter.h:105
A rectangle specified with double values.
Definition: qgsrectangle.h:35
SymbologyExport symbologyExport() const
Definition: qgsvectorfilewriter.h:268
OutputUnit
Definition: qgssymbolv2.h:47
Definition: qgsvectorfilewriter.h:153
Definition: qgsvectorfilewriter.h:147
Definition: qgsvectorfilewriter.h:146
Definition: qgsvectorfilewriter.h:121
Definition: qgsvectorfilewriter.h:53
QMap< int, int > attrIdxToOgrIdx()
Definition: qgsvectorfilewriter.h:257
WriterError mError
contains error value if construction was not successful
Definition: qgsvectorfilewriter.h:287
QString docString
Definition: qgsvectorfilewriter.h:61
UnitType
Map units that qgis supports.
Definition: qgis.h:229
SymbologyExport mSymbologyExport
Definition: qgsvectorfilewriter.h:298
double mSymbologyScaleDenominator
Scale for symbology export (e.g.
Definition: qgsvectorfilewriter.h:305
OGRGeometryH mGeom
Definition: qgsvectorfilewriter.h:282
QMap< QString, Option * > driverOptions
Definition: qgsvectorfilewriter.h:139
QString mValue
Definition: qgsvectorfilewriter.h:118
OptionType
Definition: qgsvectorfilewriter.h:45
Container of fields for a vector layer.
Definition: qgsfield.h:163
StringOption(const QString &docString, const QString &defaultValue=QString())
Definition: qgsvectorfilewriter.h:83
Definition: qgsvectorfilewriter.h:47
Definition: qgssymbollayerv2.h:45
QString glob
Definition: qgsvectorfilewriter.h:137
WkbType
Used for symbology operations.
Definition: qgis.h:53
Definition: qgsvectorfilewriter.h:160
A convenience class for writing vector files to disk.
Definition: qgsvectorfilewriter.h:42
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
Definition: qgsvectorfilewriter.h:49
Definition: qgsvectorfilewriter.h:65
Definition: qgsvectorfilewriter.h:91
SetOption(const QString &docString, QStringList values, const QString &defaultValue, bool allowNone=false)
Definition: qgsvectorfilewriter.h:68
WriterError
Definition: qgsvectorfilewriter.h:143
Definition: qgsvectorfilewriter.h:150
Definition: qgsvectorfilewriter.h:102
IntOption(const QString &docString, int defaultValue)
Definition: qgsvectorfilewriter.h:94
MetaData(QString longName, QString trLongName, QString glob, QString ext, QMap< QString, Option * > driverOptions, QMap< QString, Option * > layerOptions)
Definition: qgsvectorfilewriter.h:126
QString mErrorMessage
Definition: qgsvectorfilewriter.h:288
OGRDataSourceH mDS
Definition: qgsvectorfilewriter.h:280
QString defaultValue
Definition: qgsvectorfilewriter.h:88
QList< int > QgsAttributeList
Definition: qgsfeaturerequest.h:26
QString trLongName
Definition: qgsvectorfilewriter.h:136
MetaData()
Definition: qgsvectorfilewriter.h:123
QGis::WkbType mWkbType
geometry type which is being used
Definition: qgsvectorfilewriter.h:293
QgsFields mFields
Definition: qgsvectorfilewriter.h:284
Definition: qgsvectorfilewriter.h:80
OptionType type
Definition: qgsvectorfilewriter.h:62
QMap< int, int > mAttrIdxToOgrIdx
map attribute indizes to OGR field indexes
Definition: qgsvectorfilewriter.h:296
virtual ~Option()
Definition: qgsvectorfilewriter.h:59
double symbologyScaleDenominator() const
Definition: qgsvectorfilewriter.h:271
bool allowNone
Definition: qgsvectorfilewriter.h:77
Definition: qgsvectorfilewriter.h:152
Definition: qgis.h:231
QMap< QString, Option * > layerOptions
Definition: qgsvectorfilewriter.h:140
QString longName
Definition: qgsvectorfilewriter.h:135
void setSymbologyExport(SymbologyExport symExport)
Definition: qgsvectorfilewriter.h:269
Definition: qgsvectorfilewriter.h:149
QTextCodec * mCodec
Definition: qgsvectorfilewriter.h:290
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:39
HiddenOption(const QString &value)
Definition: qgsvectorfilewriter.h:113
void setSymbologyScaleDenominator(double d)
Definition: qgsvectorfilewriter.h:272
QSet< QString > values
Definition: qgsvectorfilewriter.h:75
QString ext
Definition: qgsvectorfilewriter.h:138
SymbologyExport
Definition: qgsvectorfilewriter.h:157
Class for storing a coordinate reference system (CRS)
Definition: qgscoordinatereferencesystem.h:50
Definition: qgsvectorfilewriter.h:48
Option(const QString &docString, OptionType type)
Definition: qgsvectorfilewriter.h:56
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:52
int defaultValue
Definition: qgsvectorfilewriter.h:99
Definition: qgsrendererv2.h:72
Definition: qgsvectorfilewriter.h:110
Definition: qgsvectorfilewriter.h:148
QString defaultValue
Definition: qgsvectorfilewriter.h:76
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:470
OGRLayerH mLayer
Definition: qgsvectorfilewriter.h:281