QGIS API Documentation  2.14.0-Essen
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 
63  };
64 
65  class SetOption : public Option
66  {
67  public:
68  SetOption( const QString& docString, const QStringList& values, const QString& defaultValue, bool allowNone = false )
69  : Option( docString, Set )
70  , values( values.toSet() )
71  , defaultValue( defaultValue )
72  , allowNone( allowNone )
73  {}
74 
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 
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  explicit HiddenOption( const QString& value )
114  : Option( "", Hidden )
115  , mValue( value )
116  {}
117 
119  };
120 
121  struct MetaData
122  {
124  {}
125 
126  MetaData( const QString& longName, const QString& trLongName, const QString& glob, const QString& ext, const QMap<QString, Option*>& driverOptions, const QMap<QString, Option*>& layerOptions )
127  : longName( longName )
128  , trLongName( trLongName )
129  , glob( glob )
130  , ext( ext )
131  , driverOptions( driverOptions )
132  , layerOptions( layerOptions )
133  {}
134 
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 
183  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
184  const QString& fileName,
185  const QString& fileEncoding,
186  const QgsCoordinateReferenceSystem *destCRS,
187  const QString& driverName = "ESRI Shapefile",
188  bool onlySelected = false,
189  QString *errorMessage = nullptr,
190  const QStringList &datasourceOptions = QStringList(),
191  const QStringList &layerOptions = QStringList(),
192  bool skipAttributeCreation = false,
193  QString *newFilename = nullptr,
194  SymbologyExport symbologyExport = NoSymbology,
195  double symbologyScale = 1.0,
196  const QgsRectangle* filterExtent = nullptr,
197  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
198  bool forceMulti = false,
199  bool includeZ = false
200  );
201 
223  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
224  const QString& fileName,
225  const QString& fileEncoding,
226  const QgsCoordinateTransform* ct,
227  const QString& driverName = "ESRI Shapefile",
228  bool onlySelected = false,
229  QString *errorMessage = nullptr,
230  const QStringList &datasourceOptions = QStringList(),
231  const QStringList &layerOptions = QStringList(),
232  bool skipAttributeCreation = false,
233  QString *newFilename = nullptr,
234  SymbologyExport symbologyExport = NoSymbology,
235  double symbologyScale = 1.0,
236  const QgsRectangle* filterExtent = nullptr,
237  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
238  bool forceMulti = false,
239  bool includeZ = false
240  );
241 
243  QgsVectorFileWriter( const QString& vectorFileName,
244  const QString& fileEncoding,
245  const QgsFields& fields,
246  QGis::WkbType geometryType,
247  const QgsCoordinateReferenceSystem* srs,
248  const QString& driverName = "ESRI Shapefile",
249  const QStringList &datasourceOptions = QStringList(),
250  const QStringList &layerOptions = QStringList(),
251  QString *newFilename = nullptr,
252  SymbologyExport symbologyExport = NoSymbology
253  );
254 
256  QgsVectorFileWriter( const QString& vectorFileName,
257  const QString& fileEncoding,
258  const QgsFields& fields,
259  QgsWKBTypes::Type geometryType,
260  const QgsCoordinateReferenceSystem* srs,
261  const QString& driverName = "ESRI Shapefile",
262  const QStringList &datasourceOptions = QStringList(),
263  const QStringList &layerOptions = QStringList(),
264  QString *newFilename = nullptr,
265  SymbologyExport symbologyExport = NoSymbology
266  );
267 
269  static QMap< QString, QString> supportedFiltersAndFormats();
270 
275  static QMap< QString, QString> ogrDriverList();
276 
278  static QString fileFilterString();
279 
281  static QString filterForDriver( const QString& driverName );
282 
284  static QString convertCodecNameForEncodingOption( const QString &codecName );
285 
287  WriterError hasError();
288 
290  QString errorMessage();
291 
293  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = nullptr, QGis::UnitType outputUnit = QGis::Meters );
294 
296  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
297 
300 
305  static bool deleteShapeFile( const QString& theFileName );
306 
307  SymbologyExport symbologyExport() const { return mSymbologyExport; }
308  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
309 
310  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
311  void setSymbologyScaleDenominator( double d );
312 
313  static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
314 
321  static OGRwkbGeometryType ogrTypeFromWkbType( QgsWKBTypes::Type type );
322 
323  protected:
325  OGRGeometryH createEmptyGeometry( QgsWKBTypes::Type wkbType );
326 
327  OGRDataSourceH mDS;
328  OGRLayerH mLayer;
330  OGRGeometryH mGeom;
331 
333 
337 
339 
342 
345 
347 
348 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
349  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
350 #endif
351 
354 
356 
357  private:
358  void init( QString vectorFileName, QString fileEncoding, const QgsFields& fields, QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem* srs, const QString& driverName, QStringList datasourceOptions, QStringList layerOptions, QString* newFilename );
359 
360  QgsRenderContext mRenderContext;
361 
362  static QMap<QString, MetaData> initMetaData();
366  static bool driverMetadata( const QString& driverName, QString &longName, QString &trLongName, QString &glob, QString &ext );
367  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
368  OGRFeatureH createFeature( QgsFeature& feature );
369  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
370 
372  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = nullptr );
373  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
374  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
375 
376  void startRender( QgsVectorLayer* vl );
377  void stopRender( QgsVectorLayer* vl );
378  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
380  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
381  static QMap<QString, MetaData> sDriverMetadata;
382 
384  QgsVectorFileWriter& operator=( const QgsVectorFileWriter& rh );
385 };
386 
387 #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
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:62
QMap< int, int > attrIdxToOgrIdx()
WriterError mError
Contains error value if construction was not successful.
MetaData(const QString &longName, const QString &trLongName, const QString &glob, const QString &ext, const QMap< QString, Option * > &driverOptions, const QMap< QString, Option * > &layerOptions)
SymbologyExport mSymbologyExport
double mSymbologyScaleDenominator
Scale for symbology export (e.g.
QMap< QString, Option * > driverOptions
Container of fields for a vector layer.
Definition: qgsfield.h:187
WkbType
Used for symbology operations.
Definition: qgis.h:57
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:187
IntOption(const QString &docString, int defaultValue)
SetOption(const QString &docString, const QStringList &values, const QString &defaultValue, bool allowNone=false)
QgsWKBTypes::Type mWkbType
Geometry type which is being used.
QMap< int, int > mAttrIdxToOgrIdx
Map attribute indizes to OGR field indexes.
OGRSpatialReferenceH mOgrRef
double symbologyScaleDenominator() const
QMap< QString, Option * > layerOptions
void setSymbologyExport(SymbologyExport symExport)
Contains information about the context of a rendering operation.
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:155
Represents a vector layer which manages a vector based data sets.
void * OGRSpatialReferenceH