QGIS API Documentation  2.15.0-Master (13f053b)
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 
50  class Option
51  {
52  public:
53  Option( const QString& docString, OptionType type )
54  : docString( docString )
55  , type( type ) {}
56  virtual ~Option() {}
57 
60  };
61 
62  class SetOption : public Option
63  {
64  public:
65  SetOption( const QString& docString, const QStringList& values, const QString& defaultValue, bool allowNone = false )
66  : Option( docString, Set )
67  , values( values.toSet() )
68  , defaultValue( defaultValue )
69  , allowNone( allowNone )
70  {}
71 
74  bool allowNone;
75  };
76 
77  class StringOption: public Option
78  {
79  public:
80  StringOption( const QString& docString, const QString& defaultValue = QString() )
81  : Option( docString, String )
82  , defaultValue( defaultValue )
83  {}
84 
86  };
87 
88  class IntOption: public Option
89  {
90  public:
91  IntOption( const QString& docString, int defaultValue )
92  : Option( docString, Int )
93  , defaultValue( defaultValue )
94  {}
95 
97  };
98 
99  class BoolOption : public SetOption
100  {
101  public:
102  BoolOption( const QString& docString, bool defaultValue )
103  : SetOption( docString, QStringList() << "YES" << "NO", defaultValue ? "YES" : "NO" )
104  {}
105  };
106 
107  class HiddenOption : public Option
108  {
109  public:
110  explicit HiddenOption( const QString& value )
111  : Option( "", Hidden )
112  , mValue( value )
113  {}
114 
116  };
117 
118  struct MetaData
119  {
121  {}
122 
123  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() )
124  : longName( longName )
125  , trLongName( trLongName )
126  , glob( glob )
127  , ext( ext )
128  , driverOptions( driverOptions )
129  , layerOptions( layerOptions )
130  , compulsoryEncoding( compulsoryEncoding )
131  {}
132 
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 
166  class CORE_EXPORT FieldValueConverter
167  {
168  public:
171 
173  virtual ~FieldValueConverter();
174 
179  virtual QgsField fieldDefinition( const QgsField& field );
180 
186  virtual QVariant convert( int fieldIdxInLayer, const QVariant& value );
187  };
188 
211  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
212  const QString& fileName,
213  const QString& fileEncoding,
214  const QgsCoordinateReferenceSystem *destCRS,
215  const QString& driverName = "ESRI Shapefile",
216  bool onlySelected = false,
217  QString *errorMessage = nullptr,
218  const QStringList &datasourceOptions = QStringList(),
219  const QStringList &layerOptions = QStringList(),
220  bool skipAttributeCreation = false,
221  QString *newFilename = nullptr,
222  SymbologyExport symbologyExport = NoSymbology,
223  double symbologyScale = 1.0,
224  const QgsRectangle* filterExtent = nullptr,
225  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
226  bool forceMulti = false,
227  bool includeZ = false,
228  QgsAttributeList attributes = QgsAttributeList(),
229  FieldValueConverter* fieldValueConverter = nullptr
230  );
231 
255  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
256  const QString& fileName,
257  const QString& fileEncoding,
258  const QgsCoordinateTransform* ct,
259  const QString& driverName = "ESRI Shapefile",
260  bool onlySelected = false,
261  QString *errorMessage = nullptr,
262  const QStringList &datasourceOptions = QStringList(),
263  const QStringList &layerOptions = QStringList(),
264  bool skipAttributeCreation = false,
265  QString *newFilename = nullptr,
266  SymbologyExport symbologyExport = NoSymbology,
267  double symbologyScale = 1.0,
268  const QgsRectangle* filterExtent = nullptr,
269  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
270  bool forceMulti = false,
271  bool includeZ = false,
272  QgsAttributeList attributes = QgsAttributeList(),
273  FieldValueConverter* fieldValueConverter = nullptr
274  );
275 
277  QgsVectorFileWriter( const QString& vectorFileName,
278  const QString& fileEncoding,
279  const QgsFields& fields,
280  QGis::WkbType geometryType,
281  const QgsCoordinateReferenceSystem* srs,
282  const QString& driverName = "ESRI Shapefile",
283  const QStringList &datasourceOptions = QStringList(),
284  const QStringList &layerOptions = QStringList(),
285  QString *newFilename = nullptr,
286  SymbologyExport symbologyExport = NoSymbology
287  );
288 
290  QgsVectorFileWriter( const QString& vectorFileName,
291  const QString& fileEncoding,
292  const QgsFields& fields,
293  QgsWKBTypes::Type 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  static QMap< QString, QString> supportedFiltersAndFormats();
304 
309  static QMap< QString, QString> ogrDriverList();
310 
312  static QString fileFilterString();
313 
315  static QString filterForDriver( const QString& driverName );
316 
318  static QString convertCodecNameForEncodingOption( const QString &codecName );
319 
321  WriterError hasError();
322 
324  QString errorMessage();
325 
327  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = nullptr, QGis::UnitType outputUnit = QGis::Meters );
328 
330  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
331 
334 
339  static bool deleteShapeFile( const QString& theFileName );
340 
341  SymbologyExport symbologyExport() const { return mSymbologyExport; }
342  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
343 
344  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
345  void setSymbologyScaleDenominator( double d );
346 
347  static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
348 
355  static OGRwkbGeometryType ogrTypeFromWkbType( QgsWKBTypes::Type type );
356 
357  protected:
359  OGRGeometryH createEmptyGeometry( QgsWKBTypes::Type wkbType );
360 
361  OGRDataSourceH mDS;
362  OGRLayerH mLayer;
364  OGRGeometryH mGeom;
365 
367 
371 
373 
376 
379 
381 
382 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
383  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
384 #endif
385 
388 
390 
393 
394  private:
395 
409  QgsVectorFileWriter( const QString& vectorFileName,
410  const QString& fileEncoding,
411  const QgsFields& fields,
412  QgsWKBTypes::Type geometryType,
413  const QgsCoordinateReferenceSystem* srs,
414  const QString& driverName,
415  const QStringList &datasourceOptions,
416  const QStringList &layerOptions,
417  QString *newFilename,
418  SymbologyExport symbologyExport,
419  FieldValueConverter* fieldValueConverter
420  );
421 
422  void init( QString vectorFileName, QString fileEncoding, const QgsFields& fields,
423  QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem* srs,
424  const QString& driverName, QStringList datasourceOptions,
425  QStringList layerOptions, QString* newFilename,
426  FieldValueConverter* fieldValueConverter );
427  void resetMap( const QgsAttributeList &attributes );
428 
429  QgsRenderContext mRenderContext;
430 
431  static QMap<QString, MetaData> initMetaData();
432  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
433  OGRFeatureH createFeature( QgsFeature& feature );
434  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
435 
437  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = nullptr );
438  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
439  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
440 
441  void startRender( QgsVectorLayer* vl );
442  void stopRender( QgsVectorLayer* vl );
443  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
445  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
446  static QMap<QString, MetaData> sDriverMetadata;
447 
449  QgsVectorFileWriter& operator=( const QgsVectorFileWriter& rh );
450 };
451 
452 #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.
SymbologyExport mSymbologyExport
double mSymbologyScaleDenominator
Scale for symbology export (e.g.
QMap< QString, Option * > driverOptions
Container of fields for a vector layer.
Definition: qgsfield.h:193
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