QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
205  {
207  CanAddNewLayer = 1 << 0,
208 
210  CanAppendToExistingLayer = 1 << 1,
211 
213  CanAddNewFieldsToExistingLayer = 1 << 2,
214 
216  CanDeleteLayer = 1 << 3
217  };
218 
221  Q_DECLARE_FLAGS( EditionCapabilities, EditionCapability )
222 
223 
226  typedef enum
227  {
230 
233 
236 
238  AppendToLayerAddFields
240 
263  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
264  const QString& fileName,
265  const QString& fileEncoding,
266  const QgsCoordinateReferenceSystem *destCRS,
267  const QString& driverName = "ESRI Shapefile",
268  bool onlySelected = false,
269  QString *errorMessage = nullptr,
270  const QStringList &datasourceOptions = QStringList(),
271  const QStringList &layerOptions = QStringList(),
272  bool skipAttributeCreation = false,
273  QString *newFilename = nullptr,
274  SymbologyExport symbologyExport = NoSymbology,
275  double symbologyScale = 1.0,
276  const QgsRectangle* filterExtent = nullptr,
277  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
278  bool forceMulti = false,
279  bool includeZ = false,
280  QgsAttributeList attributes = QgsAttributeList(),
281  FieldValueConverter* fieldValueConverter = nullptr
282  );
283 
307  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
308  const QString& fileName,
309  const QString& fileEncoding,
310  const QgsCoordinateTransform* ct,
311  const QString& driverName = "ESRI Shapefile",
312  bool onlySelected = false,
313  QString *errorMessage = nullptr,
314  const QStringList &datasourceOptions = QStringList(),
315  const QStringList &layerOptions = QStringList(),
316  bool skipAttributeCreation = false,
317  QString *newFilename = nullptr,
318  SymbologyExport symbologyExport = NoSymbology,
319  double symbologyScale = 1.0,
320  const QgsRectangle* filterExtent = nullptr,
321  QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
322  bool forceMulti = false,
323  bool includeZ = false,
324  QgsAttributeList attributes = QgsAttributeList(),
325  FieldValueConverter* fieldValueConverter = nullptr
326  );
327 
328 
333  class CORE_EXPORT SaveVectorOptions
334  {
335  public:
338 
340  virtual ~SaveVectorOptions();
341 
344 
347 
349  ActionOnExistingFile actionOnExistingFile;
350 
353 
357 
360 
363 
366 
369 
372 
375 
378 
381 
385 
388 
390  bool includeZ;
391 
394  };
395 
404  static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
405  const QString& fileName,
406  const SaveVectorOptions& options,
407  QString *newFilename = nullptr,
408  QString *errorMessage = nullptr );
409 
411  QgsVectorFileWriter( const QString& vectorFileName,
412  const QString& fileEncoding,
413  const QgsFields& fields,
414  QGis::WkbType geometryType,
415  const QgsCoordinateReferenceSystem* srs,
416  const QString& driverName = "ESRI Shapefile",
417  const QStringList &datasourceOptions = QStringList(),
418  const QStringList &layerOptions = QStringList(),
419  QString *newFilename = nullptr,
420  SymbologyExport symbologyExport = NoSymbology
421  );
422 
424  QgsVectorFileWriter( const QString& vectorFileName,
425  const QString& fileEncoding,
426  const QgsFields& fields,
427  QgsWKBTypes::Type geometryType,
428  const QgsCoordinateReferenceSystem* srs,
429  const QString& driverName = "ESRI Shapefile",
430  const QStringList &datasourceOptions = QStringList(),
431  const QStringList &layerOptions = QStringList(),
432  QString *newFilename = nullptr,
433  SymbologyExport symbologyExport = NoSymbology
434  );
435 
452  QgsVectorFileWriter( const QString &vectorFileName,
453  const QString &fileEncoding,
454  const QgsFields &fields,
455  QgsWKBTypes::Type geometryType,
456  const QgsCoordinateReferenceSystem* srs,
457  const QString &driverName,
458  const QStringList &datasourceOptions,
459  const QStringList &layerOptions,
460  QString *newFilename,
461  SymbologyExport symbologyExport,
462  FieldValueConverter *fieldValueConverter,
463  const QString &layerName,
464  ActionOnExistingFile action
465  );
466 
468  static QMap< QString, QString> supportedFiltersAndFormats();
469 
474  static QMap< QString, QString> ogrDriverList();
475 
477  static QString fileFilterString();
478 
480  static QString filterForDriver( const QString& driverName );
481 
483  static QString convertCodecNameForEncodingOption( const QString &codecName );
484 
486  WriterError hasError();
487 
489  QString errorMessage();
490 
492  bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = nullptr, QGis::UnitType outputUnit = QGis::Meters );
493 
495  QMap<int, int> attrIdxToOgrIdx() { return mAttrIdxToOgrIdx; }
496 
499 
504  static bool deleteShapeFile( const QString& theFileName );
505 
506  SymbologyExport symbologyExport() const { return mSymbologyExport; }
507  void setSymbologyExport( SymbologyExport symExport ) { mSymbologyExport = symExport; }
508 
509  double symbologyScaleDenominator() const { return mSymbologyScaleDenominator; }
510  void setSymbologyScaleDenominator( double d );
511 
512  static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
513 
520  static OGRwkbGeometryType ogrTypeFromWkbType( QgsWKBTypes::Type type );
521 
526  static EditionCapabilities editionCapabilities( const QString& datasetName );
527 
532  static bool targetLayerExists( const QString& datasetName,
533  const QString& layerName );
534 
539  static bool areThereNewFieldsToCreate( const QString& datasetName,
540  const QString& layerName,
541  QgsVectorLayer* layer,
542  const QgsAttributeList& attributes );
543 
544  protected:
546  OGRGeometryH createEmptyGeometry( QgsWKBTypes::Type wkbType );
547 
548  OGRDataSourceH mDS;
549  OGRLayerH mLayer;
551 
553 
557 
559 
562 
565 
567 
568 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1700
569  QMap< QgsSymbolLayerV2*, QString > mSymbolLayerTable;
570 #endif
571 
574 
576 
579 
580  private:
581 
582  void init( QString vectorFileName, QString fileEncoding, const QgsFields& fields,
583  QgsWKBTypes::Type geometryType, const QgsCoordinateReferenceSystem* srs,
584  const QString& driverName, QStringList datasourceOptions,
585  QStringList layerOptions, QString* newFilename,
586  FieldValueConverter* fieldValueConverter,
587  const QString& layerName,
588 
589  ActionOnExistingFile action );
590  void resetMap( const QgsAttributeList &attributes );
591 
592  QgsRenderContext mRenderContext;
593 
594  static QMap<QString, MetaData> initMetaData();
595  void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
596  OGRFeatureH createFeature( QgsFeature& feature );
597  bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
598 
600  WriterError exportFeaturesSymbolLevels( QgsVectorLayer* layer, QgsFeatureIterator& fit, const QgsCoordinateTransform* ct, QString* errorMessage = nullptr );
601  double mmScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
602  double mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits );
603 
604  void startRender( QgsVectorLayer* vl );
605  void stopRender( QgsVectorLayer* vl );
606  QgsFeatureRendererV2* symbologyRenderer( QgsVectorLayer* vl ) const;
608  void addRendererAttributes( QgsVectorLayer* vl, QgsAttributeList& attList );
609  static QMap<QString, MetaData> sDriverMetadata;
610 
612  QgsVectorFileWriter& operator=( const QgsVectorFileWriter& rh );
613 };
614 
615 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsVectorFileWriter::EditionCapabilities )
616 
617 #endif
Append features to existing layer, but do not create new fields.
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
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:65
QMap< int, int > attrIdxToOgrIdx()
SymbologyExport symbologyExport
Symbology to export.
WriterError mError
Contains error value if construction was not successful.
bool forceMulti
Set to true to force creation of multi* geometries.
SymbologyExport mSymbologyExport
double mSymbologyScaleDenominator
Scale for symbology export (e.g.
QgsAttributeList attributes
Attributes to export (empty means all unless skipAttributeCreation is set)
QMap< QString, Option * > driverOptions
double symbologyScaleDenominator() const
Container of fields for a vector layer.
Definition: qgsfield.h:252
WkbType
Used for symbology operations.
Definition: qgis.h:61
StringOption(const QString &docString, const QString &defaultValue=QString())
QStringList layerOptions
List of OGR layer creation options.
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
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())
bool onlySelectedFeatures
Write only selected features of layer.
IntOption(const QString &docString, int defaultValue)
Options to pass to writeAsVectorFormat()
const QgsCoordinateTransform * ct
Transform to reproject exported geometries with, or invalid transform for no transformation.
QStringList datasourceOptions
List of OGR data source creation options.
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
ActionOnExistingFile
Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteL...
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:44
FieldValueConverter * fieldValueConverter
Field value converter.
QMap< int, int > mAttrIdxToOgrIdx
Map attribute indizes to OGR field indexes.
QgsRectangle filterExtent
If not empty, only features intersecting the extent will be saved.
OGRSpatialReferenceH mOgrRef
QMap< QString, Option * > layerOptions
QgsWKBTypes::Type overrideGeometryType
Set to a valid geometry type to override the default geometry type for the layer. ...
QString compulsoryEncoding
Some formats require a compulsory encoding, typically UTF-8.
void setSymbologyExport(SymbologyExport symExport)
Contains information about the context of a rendering operation.
bool skipAttributeCreation
Only write geometries.
double symbologyScale
Scale of symbology.
EditionCapability
Edition capability flags.
Class for storing a coordinate reference system (CRS)
Option(const QString &docString, OptionType type)
Class for doing transforms between two map coordinate systems.
ActionOnExistingFile actionOnExistingFile
Action on existing file.
UnitType
Map units that qgis supports.
Definition: qgis.h:159
Interface to convert raw field values to their user-friendly value.
bool includeZ
Set to true to include z dimension in output.
SymbologyExport symbologyExport() const
Represents a vector layer which manages a vector based data sets.
void * OGRSpatialReferenceH