QGIS API Documentation  2.17.0-Master (3a3b9ab7)
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 
39 class CORE_EXPORT QgsVectorFileWriter
40 {
41  public:
43  {
44  Set,
46  Int,
47  Hidden
48  };
49 
52  class Option
53  {
54  public:
55  Option( const QString& docString, OptionType type )
56  : docString( docString )
57  , type( type ) {}
58  virtual ~Option() {}
59 
62  };
63 
66  class SetOption : public Option
67  {
68  public:
69  SetOption( const QString& docString, const QStringList& values, const QString& defaultValue, bool allowNone = false )
70  : Option( docString, Set )
71  , values( values.toSet() )
72  , defaultValue( defaultValue )
73  , allowNone( allowNone )
74  {}
75 
78  bool allowNone;
79  };
80 
83  class StringOption: public Option
84  {
85  public:
86  StringOption( const QString& docString, const QString& defaultValue = QString() )
87  : Option( docString, String )
88  , defaultValue( defaultValue )
89  {}
90 
92  };
93 
96  class IntOption: public Option
97  {
98  public:
99  IntOption( const QString& docString, int defaultValue )
100  : Option( docString, Int )
101  , defaultValue( defaultValue )
102  {}
103 
105  };
106 
109  class BoolOption : public SetOption
110  {
111  public:
112  BoolOption( const QString& docString, bool defaultValue )
113  : SetOption( docString, QStringList() << "YES" << "NO", defaultValue ? "YES" : "NO" )
114  {}
115  };
116 
119  class HiddenOption : public Option
120  {
121  public:
122  explicit HiddenOption( const QString& value )
123  : Option( "", Hidden )
124  , mValue( value )
125  {}
126 
128  };
129 
130  struct MetaData
131  {
133  {}
134 
135  MetaData( const QString& longName, const QString& trLongName, const QString& glob, const QString& ext, const QMap<QString, Option*>& driverOptions, const QMap<QString, Option*>& layerOptions, const QString& compulsoryEncoding = QString() )
136  : longName( longName )
137  , trLongName( trLongName )
138  , glob( glob )
139  , ext( ext )
140  , driverOptions( driverOptions )
141  , layerOptions( layerOptions )
142  , compulsoryEncoding( compulsoryEncoding )
143  {}
144 
153  };
154 
156  {
157  NoError = 0,
166  };
167 
169  {
170  NoSymbology = 0, //export only data
171  FeatureSymbology, //Keeps the number of features and export symbology per feature
172  SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
173  };
174 
179  class CORE_EXPORT FieldValueConverter
180  {
181  public:
184 
186  virtual ~FieldValueConverter();
187 
192  virtual QgsField fieldDefinition( const QgsField& field );
193 
199  virtual QVariant convert( int fieldIdxInLayer, const QVariant& value );
200  };
201 
224  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
225  const QString& fileName,
226  const QString& fileEncoding,
227  const QgsCoordinateReferenceSystem *destCRS,
228  const QString& driverName = "ESRI Shapefile",
229  bool onlySelected = false,
230  QString *errorMessage = nullptr,
231  const QStringList &datasourceOptions = QStringList(),
232  const QStringList &layerOptions = QStringList(),
233  bool skipAttributeCreation = false,
234  QString *newFilename = nullptr,
235  SymbologyExport symbologyExport = NoSymbology,
236  double symbologyScale = 1.0,
237  const QgsRectangle* filterExtent = nullptr,
238  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
239  bool forceMulti = false,
240  bool includeZ = false,
241  QgsAttributeList attributes = QgsAttributeList(),
242  FieldValueConverter* fieldValueConverter = nullptr
243  );
244 
268  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
269  const QString& fileName,
270  const QString& fileEncoding,
271  const QgsCoordinateTransform* ct,
272  const QString& driverName = "ESRI Shapefile",
273  bool onlySelected = false,
274  QString *errorMessage = nullptr,
275  const QStringList &datasourceOptions = QStringList(),
276  const QStringList &layerOptions = QStringList(),
277  bool skipAttributeCreation = false,
278  QString *newFilename = nullptr,
279  SymbologyExport symbologyExport = NoSymbology,
280  double symbologyScale = 1.0,
281  const QgsRectangle* filterExtent = nullptr,
282  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
283  bool forceMulti = false,
284  bool includeZ = false,
285  QgsAttributeList attributes = QgsAttributeList(),
286  FieldValueConverter* fieldValueConverter = nullptr
287  );
288 
290  QgsVectorFileWriter( const QString& vectorFileName,
291  const QString& fileEncoding,
292  const QgsFields& fields,
293  QGis::WkbType geometryType,
294  const QgsCoordinateReferenceSystem* srs,
295  const QString& driverName = "ESRI Shapefile",
296  const QStringList &datasourceOptions = QStringList(),
297  const QStringList &layerOptions = QStringList(),
298  QString *newFilename = nullptr,
299  SymbologyExport symbologyExport = NoSymbology
300  );
301 
303  QgsVectorFileWriter( const QString& vectorFileName,
304  const QString& fileEncoding,
305  const QgsFields& fields,
306  QgsWKBTypes::Type geometryType,
307  const QgsCoordinateReferenceSystem* srs,
308  const QString& driverName = "ESRI Shapefile",
309  const QStringList &datasourceOptions = QStringList(),
310  const QStringList &layerOptions = QStringList(),
311  QString *newFilename = nullptr,
312  SymbologyExport symbologyExport = NoSymbology
313  );
314 
316  static QMap< QString, QString> supportedFiltersAndFormats();
317 
322  static QMap< QString, QString> ogrDriverList();
323 
325  static QString fileFilterString();
326 
328  static QString filterForDriver( const QString& driverName );
329 
331  static QString convertCodecNameForEncodingOption( const QString &codecName );
332 
334  WriterError hasError();
335 
337  QString errorMessage();
338 
340  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = nullptr, QGis::UnitType outputUnit = QGis::Meters );
341 
343  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
344 
347 
352  static bool deleteShapeFile( const QString& theFileName );
353 
354  SymbologyExport symbologyExport() const { return mSymbologyExport; }
355  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
356 
357  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
358  void setSymbologyScaleDenominator( double d );
359 
360  static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
361 
368  static OGRwkbGeometryType ogrTypeFromWkbType( QgsWKBTypes::Type type );
369 
370  protected:
372  OGRGeometryH createEmptyGeometry( QgsWKBTypes::Type wkbType );
373 
374  OGRDataSourceH mDS;
375  OGRLayerH mLayer;
377  OGRGeometryH mGeom;
378 
380 
384 
386 
389 
392 
394 
395 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
396  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
397 #endif
398 
401 
403 
406 
407  private:
408 
422  QgsVectorFileWriter( const QString& vectorFileName,
423  const QString& fileEncoding,
424  const QgsFields& fields,
425  QgsWKBTypes::Type geometryType,
426  const QgsCoordinateReferenceSystem* srs,
427  const QString& driverName,
428  const QStringList &datasourceOptions,
429  const QStringList &layerOptions,
430  QString *newFilename,
431  SymbologyExport symbologyExport,
432  FieldValueConverter* fieldValueConverter
433  );
434 
435  void init( QString vectorFileName, QString fileEncoding, const QgsFields& fields,
436  QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem* srs,
437  const QString& driverName, QStringList datasourceOptions,
438  QStringList layerOptions, QString* newFilename,
439  FieldValueConverter* fieldValueConverter );
440  void resetMap( const QgsAttributeList &attributes );
441 
442  QgsRenderContext mRenderContext;
443 
444  static QMap<QString, MetaData> initMetaData();
445  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
446  OGRFeatureH createFeature( QgsFeature& feature );
447  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
448 
450  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = nullptr );
451  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
452  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
453 
454  void startRender( QgsVectorLayer* vl );
455  void stopRender( QgsVectorLayer* vl );
456  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
458  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
459  static QMap<QString, MetaData> sDriverMetadata;
460 
462  QgsVectorFileWriter& operator=( const QgsVectorFileWriter& rh );
463 };
464 
465 #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:65
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:209
MetaData(const QString &longName, const QString &trLongName, const QString &glob, const QString &ext, const QMap< QString, Option * > &driverOptions, const QMap< QString, Option * > &layerOptions, const QString &compulsoryEncoding=QString())
WkbType
Used for symbology operations.
Definition: qgis.h:61
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.
FieldValueConverter * mFieldValueConverter
Field value converter.
QList< int > QgsAttributeList
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:44
QMap< int, int > mAttrIdxToOgrIdx
Map attribute indizes to OGR field indexes.
OGRSpatialReferenceH mOgrRef
double symbologyScaleDenominator() const
QMap< QString, Option * > layerOptions
QString compulsoryEncoding
Some formats require a compulsory encoding, typically UTF-8.
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:159
Interface to convert raw field values to their user-friendly value.
Represents a vector layer which manages a vector based data sets.
void * OGRSpatialReferenceH