QGIS API Documentation
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 
184  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
185  const QString& fileName,
186  const QString& fileEncoding,
187  const QgsCoordinateReferenceSystem *destCRS,
188  const QString& driverName = "ESRI Shapefile",
189  bool onlySelected = false,
190  QString *errorMessage = nullptr,
191  const QStringList &datasourceOptions = QStringList(),
192  const QStringList &layerOptions = QStringList(),
193  bool skipAttributeCreation = false,
194  QString *newFilename = nullptr,
195  SymbologyExport symbologyExport = NoSymbology,
196  double symbologyScale = 1.0,
197  const QgsRectangle* filterExtent = nullptr,
198  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
199  bool forceMulti = false,
200  bool includeZ = false,
201  QgsAttributeList attributes = QgsAttributeList()
202  );
203 
226  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
227  const QString& fileName,
228  const QString& fileEncoding,
229  const QgsCoordinateTransform* ct,
230  const QString& driverName = "ESRI Shapefile",
231  bool onlySelected = false,
232  QString *errorMessage = nullptr,
233  const QStringList &datasourceOptions = QStringList(),
234  const QStringList &layerOptions = QStringList(),
235  bool skipAttributeCreation = false,
236  QString *newFilename = nullptr,
237  SymbologyExport symbologyExport = NoSymbology,
238  double symbologyScale = 1.0,
239  const QgsRectangle* filterExtent = nullptr,
240  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
241  bool forceMulti = false,
242  bool includeZ = false,
243  QgsAttributeList attributes = QgsAttributeList()
244  );
245 
247  QgsVectorFileWriter( const QString& vectorFileName,
248  const QString& fileEncoding,
249  const QgsFields& fields,
250  QGis::WkbType geometryType,
251  const QgsCoordinateReferenceSystem* srs,
252  const QString& driverName = "ESRI Shapefile",
253  const QStringList &datasourceOptions = QStringList(),
254  const QStringList &layerOptions = QStringList(),
255  QString *newFilename = nullptr,
256  SymbologyExport symbologyExport = NoSymbology
257  );
258 
260  QgsVectorFileWriter( const QString& vectorFileName,
261  const QString& fileEncoding,
262  const QgsFields& fields,
263  QgsWKBTypes::Type geometryType,
264  const QgsCoordinateReferenceSystem* srs,
265  const QString& driverName = "ESRI Shapefile",
266  const QStringList &datasourceOptions = QStringList(),
267  const QStringList &layerOptions = QStringList(),
268  QString *newFilename = nullptr,
269  SymbologyExport symbologyExport = NoSymbology
270  );
271 
273  static QMap< QString, QString> supportedFiltersAndFormats();
274 
279  static QMap< QString, QString> ogrDriverList();
280 
282  static QString fileFilterString();
283 
285  static QString filterForDriver( const QString& driverName );
286 
288  static QString convertCodecNameForEncodingOption( const QString &codecName );
289 
291  WriterError hasError();
292 
294  QString errorMessage();
295 
297  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = nullptr, QGis::UnitType outputUnit = QGis::Meters );
298 
300  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
301 
304 
309  static bool deleteShapeFile( const QString& theFileName );
310 
311  SymbologyExport symbologyExport() const { return mSymbologyExport; }
312  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
313 
314  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
315  void setSymbologyScaleDenominator( double d );
316 
317  static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
318 
325  static OGRwkbGeometryType ogrTypeFromWkbType( QgsWKBTypes::Type type );
326 
327  protected:
329  OGRGeometryH createEmptyGeometry( QgsWKBTypes::Type wkbType );
330 
331  OGRDataSourceH mDS;
332  OGRLayerH mLayer;
334  OGRGeometryH mGeom;
335 
337 
341 
343 
346 
349 
351 
352 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
353  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
354 #endif
355 
358 
360 
361  private:
362  void init( QString vectorFileName, QString fileEncoding, const QgsFields& fields, QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem* srs, const QString& driverName, QStringList datasourceOptions, QStringList layerOptions, QString* newFilename );
363  void resetMap( const QgsAttributeList &attributes );
364 
365  QgsRenderContext mRenderContext;
366 
367  static QMap<QString, MetaData> initMetaData();
371  static bool driverMetadata( const QString& driverName, QString &longName, QString &trLongName, QString &glob, QString &ext );
372  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
373  OGRFeatureH createFeature( QgsFeature& feature );
374  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
375 
377  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = nullptr );
378  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
379  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
380 
381  void startRender( QgsVectorLayer* vl );
382  void stopRender( QgsVectorLayer* vl );
383  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
385  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
386  static QMap<QString, MetaData> sDriverMetadata;
387 
389  QgsVectorFileWriter& operator=( const QgsVectorFileWriter& rh );
390 };
391 
392 #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.
QList< int > QgsAttributeList
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