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,
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
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:264
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:253
WriterError mError
contains error value if construction was not successful
Definition: qgsvectorfilewriter.h:283
QString docString
Definition: qgsvectorfilewriter.h:61
UnitType
Map units that qgis supports.
Definition: qgis.h:229
SymbologyExport mSymbologyExport
Definition: qgsvectorfilewriter.h:294
double mSymbologyScaleDenominator
Scale for symbology export (e.g.
Definition: qgsvectorfilewriter.h:301
OGRGeometryH mGeom
Definition: qgsvectorfilewriter.h:278
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:172
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:159
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:284
OGRDataSourceH mDS
Definition: qgsvectorfilewriter.h:276
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:289
QgsFields mFields
Definition: qgsvectorfilewriter.h:280
Definition: qgsvectorfilewriter.h:80
OptionType type
Definition: qgsvectorfilewriter.h:62
QMap< int, int > mAttrIdxToOgrIdx
map attribute indizes to OGR field indexes
Definition: qgsvectorfilewriter.h:292
virtual ~Option()
Definition: qgsvectorfilewriter.h:59
double symbologyScaleDenominator() const
Definition: qgsvectorfilewriter.h:267
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:265
Definition: qgsvectorfilewriter.h:149
QTextCodec * mCodec
Definition: qgsvectorfilewriter.h:286
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:268
QSet< QString > values
Definition: qgsvectorfilewriter.h:75
QString ext
Definition: qgsvectorfilewriter.h:138
SymbologyExport
Definition: qgsvectorfilewriter.h:156
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:468
OGRLayerH mLayer
Definition: qgsvectorfilewriter.h:277