QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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 <ogr_api.h>
23
24#include "qgis_core.h"
25#include "qgis_sip.h"
27#include "qgsfeaturesink.h"
28#include "qgsfeedback.h"
29#include "qgsfields.h"
30#include "qgsgeometryengine.h"
31#include "qgsogrutils.h"
32#include "qgsrendercontext.h"
33#include "qgsrenderer.h"
34
35#include <QString>
36
37using namespace Qt::StringLiterals;
38
39class QgsSymbolLayer;
40class QTextCodec;
42
52class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
53{
54 public:
62
67 class Option
68 {
69 public:
74 virtual ~Option() = default;
75
76 QString docString;
78 };
79
85 {
86 public:
87 SetOption( const QString &docString, const QStringList &values, const QString &defaultValue, bool allowNone = false )
88 : Option( docString, Set )
89 , values( values.begin(), values.end() )
92 {}
93
94 QSet<QString> values;
95 QString defaultValue;
97 };
98
104 {
105 public:
106 StringOption( const QString &docString, const QString &defaultValue = QString() )
109 {}
110
112 };
113
119 {
120 public:
121 IntOption( const QString &docString, int defaultValue )
122 : Option( docString, Int )
124 {}
125
127 };
128
134 {
135 public:
136 BoolOption( const QString &docString, bool defaultValue )
137 : SetOption( docString, QStringList() << u"YES"_s << u"NO"_s, defaultValue ? "YES" : "NO" )
138 {}
139 };
140
146 {
147 public:
148 explicit HiddenOption( const QString &value )
149 : Option( QString(), Hidden )
150 , mValue( value )
151 {}
152
153 QString mValue;
154 };
155
156 struct MetaData
157 {
158 MetaData() = default;
159
161 const QString &longName,
162 const QString &trLongName,
163 const QString &glob,
164 const QString &ext,
165 const QMap<QString, QgsVectorFileWriter::Option *> &driverOptions,
166 const QMap<QString, QgsVectorFileWriter::Option *> &layerOptions,
167 const QString &compulsoryEncoding = QString()
168 )
169 : longName( longName )
171 , glob( glob )
172 , ext( ext )
176 {}
177
178 QString longName;
179 QString trLongName;
180 QString glob;
181 QString ext;
182 QMap<QString, QgsVectorFileWriter::Option *> driverOptions;
183 QMap<QString, QgsVectorFileWriter::Option *> layerOptions;
186 };
187
202
213
224
225
229 class CORE_EXPORT FieldValueConverter
230 {
231 public:
233
234 virtual ~FieldValueConverter() = default;
235
241 virtual QgsField fieldDefinition( const QgsField &field );
242
249 virtual QVariant convert( int fieldIdxInLayer, const QVariant &value );
250
255 };
256
274
279
280
297
298#ifndef SIP_RUN
299
326#else
327
353#endif
354 Q_DECL_DEPRECATED static QgsVectorFileWriter::WriterError writeAsVectorFormat(
355 QgsVectorLayer *layer,
356 const QString &fileName,
357 const QString &fileEncoding,
359 const QString &driverName = "GPKG",
360 bool onlySelected = false,
361 QString *errorMessage SIP_OUT = nullptr,
362 const QStringList &datasourceOptions = QStringList(),
363 const QStringList &layerOptions = QStringList(),
364 bool skipAttributeCreation = false,
365 QString *newFilename = nullptr,
367 double symbologyScale = 1.0,
368 const QgsRectangle *filterExtent = nullptr,
369 Qgis::WkbType overrideGeometryType = Qgis::WkbType::Unknown,
370 bool forceMulti = false,
371 bool includeZ = false,
372 const QgsAttributeList &attributes = QgsAttributeList(),
373 QgsVectorFileWriter::FieldValueConverter *fieldValueConverter = nullptr
374#ifndef SIP_RUN
375 ,
376 QString *newLayer = nullptr
377 );
378#else
380#endif
381
382#ifndef SIP_RUN
383
411#else
412
439#endif
440 Q_DECL_DEPRECATED static QgsVectorFileWriter::WriterError writeAsVectorFormat(
441 QgsVectorLayer *layer,
442 const QString &fileName,
443 const QString &fileEncoding,
444 const QgsCoordinateTransform &ct,
445 const QString &driverName = "GPKG",
446 bool onlySelected = false,
447 QString *errorMessage SIP_OUT = nullptr,
448 const QStringList &datasourceOptions = QStringList(),
449 const QStringList &layerOptions = QStringList(),
450 bool skipAttributeCreation = false,
451 QString *newFilename = nullptr,
453 double symbologyScale = 1.0,
454 const QgsRectangle *filterExtent = nullptr,
455 Qgis::WkbType overrideGeometryType = Qgis::WkbType::Unknown,
456 bool forceMulti = false,
457 bool includeZ = false,
458 const QgsAttributeList &attributes = QgsAttributeList(),
459 QgsVectorFileWriter::FieldValueConverter *fieldValueConverter = nullptr
460#ifndef SIP_RUN
461 ,
462 QString *newLayer = nullptr
463 );
464#else
466#endif
467
604
605#ifndef SIP_RUN
606
618#else
619
630#endif
632 QgsVectorLayer *layer,
633 const QString &fileName,
635 QString *newFilename = nullptr,
636 QString *errorMessage SIP_OUT = nullptr
637#ifndef SIP_RUN
638 ,
639 QString *newLayer = nullptr
640 );
641#else
643#endif
644
649 Q_DECL_DEPRECATED QgsVectorFileWriter(
650 const QString &vectorFileName,
651 const QString &fileEncoding,
652 const QgsFields &fields,
653 Qgis::WkbType geometryType,
655 const QString &driverName = "GPKG",
656 const QStringList &datasourceOptions = QStringList(),
657 const QStringList &layerOptions = QStringList(),
658 QString *newFilename = nullptr,
661#ifndef SIP_RUN
662 ,
663 QString *newLayer = nullptr,
665 FieldNameSource fieldNameSource = Original
666#endif
668
694 Q_DECL_DEPRECATED QgsVectorFileWriter(
695 const QString &vectorFileName,
696 const QString &fileEncoding,
697 const QgsFields &fields,
698 Qgis::WkbType geometryType,
700 const QString &driverName,
701 const QStringList &datasourceOptions,
702 const QStringList &layerOptions,
703 QString *newFilename,
705 QgsVectorFileWriter::FieldValueConverter *fieldValueConverter,
706 const QString &layerName,
708 QString *newLayer = nullptr,
711 FieldNameSource fieldNameSource = Original,
712 bool includeConstraints = false,
713 bool setFieldDomains = true,
714 const QgsAbstractDatabaseProviderConnection *sourceDatabaseProviderConnection = nullptr
715 ) SIP_SKIP;
716
718 QgsVectorFileWriter &operator=( const QgsVectorFileWriter &rh ) = delete;
719
734 const QString &fileName,
735 const QgsFields &fields,
736 Qgis::WkbType geometryType,
738 const QgsCoordinateTransformContext &transformContext,
741 QString *newFilename = nullptr,
742 QString *newLayer = nullptr
743 ) SIP_FACTORY;
744
757 Q_DECL_DEPRECATED static QgsVectorFileWriter::WriterError writeAsVectorFormatV2(
758 QgsVectorLayer *layer,
759 const QString &fileName,
760 const QgsCoordinateTransformContext &transformContext,
762 QString *newFilename = nullptr,
763 QString *newLayer = nullptr,
764 QString *errorMessage SIP_OUT = nullptr
766
780 QgsVectorLayer *layer,
781 const QString &fileName,
782 const QgsCoordinateTransformContext &transformContext,
784 QString *errorMessage SIP_OUT = nullptr,
785 QString *newFilename SIP_OUT = nullptr,
786 QString *newLayer SIP_OUT = nullptr
787 );
788
793 {
795 QString driverName;
796
799
804 QStringList globs;
805 };
806
815 static QList< QgsVectorFileWriter::FilterFormatDetails > supportedFiltersAndFormats( VectorFormatOptions options = SortRecommended );
816
825 static QStringList supportedFormatExtensions( VectorFormatOptions options = SortRecommended );
826
833 static bool supportsFeatureStyles( const QString &driverName );
834
839 {
841 QString longName;
842
844 QString driverName;
845 };
846
858 static QList< QgsVectorFileWriter::DriverDetails > ogrDriverList( VectorFormatOptions options = SortRecommended );
859
865 static QString driverForExtension( const QString &extension );
866
873 static QString fileFilterString( VectorFormatOptions options = SortRecommended );
874
876 static QString filterForDriver( const QString &driverName );
877
879 static QString convertCodecNameForEncodingOption( const QString &codecName );
880
883
885 QString errorMessage() const;
886
893 QString driver() const;
894
901 QString driverLongName() const;
902
909
910 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
911
913 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
914 QString lastError() const override;
915
920
927
929 ~QgsVectorFileWriter() override;
930
936 static bool deleteShapeFile( const QString &fileName );
937
944
951
957 double symbologyScale() const { return mSymbologyScale; }
958
964 void setSymbologyScale( double scale );
965
966 static bool driverMetadata( const QString &driverName, MetaData &driverMetadata );
967
973 static QStringList defaultDatasetOptions( const QString &driverName );
974
980 static QStringList defaultLayerOptions( const QString &driverName );
981
988 static OGRwkbGeometryType ogrTypeFromWkbType( Qgis::WkbType type ) SIP_SKIP;
989
993 static QgsVectorFileWriter::EditionCapabilities editionCapabilities( const QString &datasetName );
994
998 static bool targetLayerExists( const QString &datasetName, const QString &layerName );
999
1003 static bool areThereNewFieldsToCreate( const QString &datasetName, const QString &layerName, QgsVectorLayer *layer, const QgsAttributeList &attributes );
1004
1005 protected:
1007 OGRGeometryH createEmptyGeometry( Qgis::WkbType wkbType ) SIP_SKIP;
1008
1010 OGRLayerH mLayer = nullptr;
1011 OGRSpatialReferenceH mOgrRef = nullptr;
1012
1014
1018
1019 QTextCodec *mCodec = nullptr;
1020
1023
1025 // In particular for a GeoPackage file, a field with the FID column is always put in first position.
1027
1030
1032
1033 QMap< QgsSymbolLayer *, QString > mSymbolLayerTable;
1034
1037
1040
1043
1046
1048 bool mSetFieldDomains = true;
1049
1050 private:
1051#ifdef SIP_RUN
1053#endif
1054
1055 struct PreparedWriterDetails
1056 {
1057 std::unique_ptr< QgsFeatureRenderer > renderer;
1059 Qgis::WkbType sourceWkbType = Qgis::WkbType::Unknown;
1060 QgsFields sourceFields;
1061 QString providerType;
1062 long long featureCount = 0;
1063 QgsFeatureIds selectedFeatureIds;
1064 QString dataSourceUri;
1065 QString storageType;
1066 QgsFeatureIterator geometryTypeScanIterator;
1067 QgsExpressionContext expressionContext;
1068 QSet< int > fieldsToConvertToInt;
1069 QgsRenderContext renderContext;
1070 bool shallTransform = false;
1073 QgsAttributeList attributes;
1074 QgsFields outputFields;
1075 QgsFeatureIterator sourceFeatureIterator;
1076 QgsGeometry filterRectGeometry;
1077 std::unique_ptr< QgsGeometryEngine > filterRectEngine;
1078 QVariantMap providerUriParams;
1079 std::unique_ptr< QgsAbstractDatabaseProviderConnection > sourceDatabaseProviderConnection;
1080 };
1081
1086 static QgsVectorFileWriter::WriterError prepareWriteAsVectorFormat( QgsVectorLayer *layer, const QgsVectorFileWriter::SaveVectorOptions &options, PreparedWriterDetails &details );
1087
1102 static QgsVectorFileWriter::WriterError writeAsVectorFormatV2(
1103 PreparedWriterDetails &details,
1104 const QString &fileName,
1105 const QgsCoordinateTransformContext &transformContext,
1107 QString *newFilename = nullptr,
1108 QString *newLayer = nullptr,
1109 QString *errorMessage SIP_OUT = nullptr,
1111 );
1112
1118 Q_DECL_DEPRECATED static QgsVectorFileWriter::WriterError writeAsVectorFormat(
1119 PreparedWriterDetails &details,
1120 const QString &fileName,
1122 QString *newFilename = nullptr,
1123 QString *errorMessage SIP_OUT = nullptr,
1124 QString *newLayer = nullptr
1126
1127 void init(
1128 QString vectorFileName,
1129 QString fileEncoding,
1130 const QgsFields &fields,
1131 Qgis::WkbType geometryType,
1133 const QString &driverName,
1134 QStringList datasourceOptions,
1135 QStringList layerOptions,
1136 QString *newFilename,
1137 QgsVectorFileWriter::FieldValueConverter *fieldValueConverter,
1138 const QString &layerName,
1139 QgsVectorFileWriter::ActionOnExistingFile action,
1140 QString *newLayer,
1141 QgsFeatureSink::SinkFlags sinkFlags,
1142 const QgsCoordinateTransformContext &transformContext,
1143 FieldNameSource fieldNameSource,
1144 const QgsAbstractDatabaseProviderConnection *sourceDatabaseProviderConnection
1145 );
1146 void resetMap( const QgsAttributeList &attributes );
1147
1148 std::unique_ptr< QgsFeatureRenderer > mRenderer;
1149 QgsRenderContext mRenderContext;
1150
1151
1152 std::unique_ptr< QgsCoordinateTransform > mCoordinateTransform;
1153
1154 bool mUsingTransaction = false;
1155 QSet< QMetaType::Type > mSupportedListSubTypes;
1156
1157 Qgis::VectorFileWriterCapabilities mCapabilities;
1158
1159 void createSymbolLayerTable( QgsVectorLayer *vl, const QgsCoordinateTransform &ct, OGRDataSourceH ds );
1160 gdal::ogr_feature_unique_ptr createFeature( const QgsFeature &feature );
1161 bool writeFeature( OGRLayerH layer, OGRFeatureH feature );
1162
1164 QgsVectorFileWriter::WriterError exportFeaturesSymbolLevels( const PreparedWriterDetails &details, QgsFeatureIterator &fit, const QgsCoordinateTransform &ct, QString *errorMessage = nullptr );
1165 double mmScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits );
1166 double mapUnitScaleFactor( double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits );
1167
1168 void startRender( QgsFeatureRenderer *sourceRenderer, const QgsFields &fields );
1169 void stopRender();
1170 std::unique_ptr< QgsFeatureRenderer > createSymbologyRenderer( QgsFeatureRenderer *sourceRenderer ) const;
1172 static void addRendererAttributes( QgsFeatureRenderer *renderer, QgsRenderContext &context, const QgsFields &fields, QgsAttributeList &attList );
1173
1175 static QStringList concatenateOptions( const QMap<QString, Option *> &options );
1176
1179};
1180
1183
1184// clazy:excludeall=qstring-allocations
1185
1186#endif
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
DistanceUnit
Units of distance.
Definition qgis.h:5170
@ Meters
Meters.
Definition qgis.h:5171
QFlags< VectorFileWriterCapability > VectorFileWriterCapabilities
Capabilities supported by a QgsVectorFileWriter object.
Definition qgis.h:1109
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Unknown
Unknown.
Definition qgis.h:295
FeatureSymbologyExport
Options for exporting features considering their symbology.
Definition qgis.h:5913
@ NoSymbology
Export only data.
Definition qgis.h:5914
Provides common functionality for database based connections.
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Wrapper for iterator of features from vector data provider or vector layer.
Abstract base class for all 2D vector feature renderers.
An interface for objects which accept features via addFeature(s) methods.
QFlags< SinkFlag > SinkFlags
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
QFlags< Flag > Flags
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
A geometry is the spatial representation of a feature.
A structured metadata store for a map layer.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
Abstract base class for symbol layers.
BoolOption(const QString &docString, bool defaultValue)
Interface to convert raw field values to their user-friendly values.
virtual QVariant convert(int fieldIdxInLayer, const QVariant &value)
Convert the provided value, for field fieldIdxInLayer.
virtual QgsVectorFileWriter::FieldValueConverter * clone() const
Creates a clone of the FieldValueConverter.
virtual QgsField fieldDefinition(const QgsField &field)
Returns a possibly modified field definition.
IntOption(const QString &docString, int defaultValue)
Describes an available option for configuring file writing for a particular output format.
Option(const QString &docString, QgsVectorFileWriter::OptionType type)
QgsVectorFileWriter::OptionType type
virtual ~Option()=default
Options to pass to QgsVectorFileWriter::writeAsVectorFormat().
bool forceMulti
Sets to true to force creation of multipart geometries.
QgsCoordinateTransform ct
Transform to reproject exported geometries with, or invalid transform for no transformation.
QStringList attributesExportNames
Attributes export names.
QgsLayerMetadata layerMetadata
Layer metadata to save for the exported vector file.
QString layerName
Layer name. If let empty, it will be derived from the filename.
QgsRectangle filterExtent
If not empty, only features intersecting the extent will be saved.
bool includeConstraints
Set to true to transfer field constraints to the exported vector file.
const QgsAbstractDatabaseProviderConnection * sourceDatabaseProviderConnection
Source database provider connection, for field domains.
QgsVectorFileWriter::FieldValueConverter * fieldValueConverter
Field value converter.
QStringList layerOptions
List of OGR layer creation options.
Qgis::WkbType overrideGeometryType
Set to a valid geometry type to override the default geometry type for the layer.
bool includeZ
Sets to true to include z dimension in output. This option is only valid if overrideGeometryType is s...
Qgis::FeatureSymbologyExport symbologyExport
Symbology to export.
bool saveMetadata
Set to true to save layer metadata for the exported vector file.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
QgsAttributeList attributes
Attributes to export (empty means all unless skipAttributeCreation is set).
bool onlySelectedFeatures
Write only selected features of layer.
QgsVectorFileWriter::FieldNameSource fieldNameSource
Source for exported field names.
bool skipAttributeCreation
Only write geometries.
bool setFieldDomains
Set to true to transfer field domains to the exported vector file.
QStringList datasourceOptions
List of OGR data source creation options.
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
An available option for configuring file writing for a particular output format, presenting a choice ...
SetOption(const QString &docString, const QStringList &values, const QString &defaultValue, bool allowNone=false)
StringOption(const QString &docString, const QString &defaultValue=QString())
A convenience class for writing vector layers to disk based formats (e.g.
static QgsVectorFileWriter::WriterError writeAsVectorFormatV3(QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *errorMessage=nullptr, QString *newFilename=nullptr, QString *newLayer=nullptr)
Writes a layer out to a vector file.
Qgis::FeatureSymbologyExport mSymbologyExport
EditionCapability
Edition capability flags.
@ CanAddNewFieldsToExistingLayer
Flag to indicate that new fields can be added to an existing layer. Imply CanAppendToExistingLayer.
@ CanAppendToExistingLayer
Flag to indicate that new features can be added to an existing layer.
@ CanAddNewLayer
Flag to indicate that a new layer can be added to the dataset.
@ CanDeleteLayer
Flag to indicate that an existing layer can be deleted.
static bool supportsFeatureStyles(const QString &driverName)
Returns true if the specified driverName supports feature styles.
Qgis::WkbType mWkbType
Geometry type which is being used.
double mSymbologyScale
Scale for symbology export (e.g. for symbols units in map units).
@ Canceled
Writing was interrupted by manual cancellation.
@ ErrSavingMetadata
Metadata saving failed.
gdal::ogr_datasource_unique_ptr mDS
static Q_DECL_DEPRECATED QgsVectorFileWriter::WriterError writeAsVectorFormatV2(QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename=nullptr, QString *newLayer=nullptr, QString *errorMessage=nullptr)
Writes a layer out to a vector file.
QFlags< EditionCapability > EditionCapabilities
Combination of CanAddNewLayer, CanAppendToExistingLayer, CanAddNewFieldsToExistingLayer or CanDeleteL...
double symbologyScale() const
Returns the reference scale for output.
static QString driverForExtension(const QString &extension)
Returns the OGR driver name for a specified file extension.
Qgis::VectorFileWriterCapabilities capabilities() const
Returns the capabilities supported by the writer.
friend class QgsVectorFileWriterTask
static QList< QgsVectorFileWriter::FilterFormatDetails > supportedFiltersAndFormats(VectorFormatOptions options=SortRecommended)
Returns a list or pairs, with format filter string as first element and OGR format key as second elem...
OGRSpatialReferenceH mOgrRef
static QgsVectorFileWriter * create(const QString &fileName, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &srs, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QString *newFilename=nullptr, QString *newLayer=nullptr)
Create a new vector file writer.
QString driver() const
Returns the GDAL (short) driver name associated with the output file.
Q_DECL_DEPRECATED QgsVectorFileWriter(const QString &vectorFileName, const QString &fileEncoding, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &srs=QgsCoordinateReferenceSystem(), const QString &driverName="GPKG", const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QString *newFilename=nullptr, Qgis::FeatureSymbologyExport symbologyExport=Qgis::FeatureSymbologyExport::NoSymbology, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QString *newLayer=nullptr, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext(), FieldNameSource fieldNameSource=Original)
Create a new vector file writer.
QMap< int, int > mAttrIdxToProviderIdx
Map attribute indices to OGR provider field indexes such as they are after the OGR provider has been ...
static Q_DECL_DEPRECATED QgsVectorFileWriter::WriterError writeAsVectorFormat(QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateReferenceSystem &destCRS=QgsCoordinateReferenceSystem(), const QString &driverName="GPKG", bool onlySelected=false, QString *errorMessage=nullptr, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=nullptr, Qgis::FeatureSymbologyExport symbologyExport=Qgis::FeatureSymbologyExport::NoSymbology, double symbologyScale=1.0, const QgsRectangle *filterExtent=nullptr, Qgis::WkbType overrideGeometryType=Qgis::WkbType::Unknown, bool forceMulti=false, bool includeZ=false, const QgsAttributeList &attributes=QgsAttributeList(), QgsVectorFileWriter::FieldValueConverter *fieldValueConverter=nullptr, QString *newLayer=nullptr)
Write contents of vector layer to an (OGR supported) vector format.
static QString filterForDriver(const QString &driverName)
Creates a filter for an OGR driver key.
QgsVectorFileWriter::WriterError hasError() const
Checks whether there were any errors in constructor.
VectorFormatOption
Options for sorting and filtering vector formats.
@ SupportsMultipleLayers
Filter to only formats which support multiple layers.
@ SkipNonSpatialFormats
Filter out any formats which do not have spatial support (e.g. those which cannot save geometries).
@ SortRecommended
Use recommended sort order, with extremely commonly used formats listed first.
static QList< QgsVectorFileWriter::DriverDetails > ogrDriverList(VectorFormatOptions options=SortRecommended)
Returns the driver list that can be used for dialogs.
QString driverLongName() const
Returns the GDAL long driver name associated with the output file.
QFlags< VectorFormatOption > VectorFormatOptions
WriterError mError
Contains error value if construction was not successful.
Qgis::FeatureSymbologyExport symbologyExport() const
Returns the feature symbology export handling for the writer.
FieldNameSource
Source for exported field names.
@ PreferAlias
Use the field alias as the exported field name, wherever one is set. Otherwise use the original field...
@ Original
Use original field names.
friend class TestQgsVectorFileWriter
bool mIncludeConstraints
Whether to transfer field constraints to output.
bool addFeatureWithStyle(QgsFeature &feature, QgsFeatureRenderer *renderer, Qgis::DistanceUnit outputUnit=Qgis::DistanceUnit::Meters)
Adds a feature to the currently opened data source, using the style from a specified renderer.
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
bool mSetFieldDomains
Whether to set field domains to output.
static QString convertCodecNameForEncodingOption(const QString &codecName)
Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile.
FieldValueConverter * mFieldValueConverter
Field value converter.
QString errorMessage() const
Retrieves error message.
QMap< int, int > mAttrIdxToOgrLayerIdx
Map attribute indices to OGR layer field indexes such as they are on the current mLayer instance.
void setSymbologyExport(Qgis::FeatureSymbologyExport symExport)
Sets the feature symbology export handling for the writer.
QMap< QgsSymbolLayer *, QString > mSymbolLayerTable
static QString fileFilterString(VectorFormatOptions options=SortRecommended)
Returns filter string that can be used for dialogs.
QMap< int, int > sourceFieldIndexToWriterFieldIndex() const
Returns the map of original source field index to output file field index.
ActionOnExistingFile
Enumeration to describe how to handle existing files.
@ CreateOrOverwriteLayer
Create or overwrite layer.
@ CreateOrOverwriteFile
Create or overwrite file.
@ AppendToLayerNoNewFields
Append features to existing layer, but do not create new fields.
@ AppendToLayerAddFields
Append features to existing layer, and create new fields if needed.
Represents a vector layer which manages a vector based dataset.
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:274
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_FACTORY
Definition qgis_sip.h:83
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds
QList< int > QgsAttributeList
Definition qgsfield.h:30
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)
Details of available driver formats.
QString longName
Descriptive, user friendly name for the driver.
QString driverName
Unique driver name.
Details of available filters and formats.
QString filterString
Filter string for file picker dialogs.
QStringList globs
Matching glob patterns for format, e.g.
MetaData(const QString &longName, const QString &trLongName, const QString &glob, const QString &ext, const QMap< QString, QgsVectorFileWriter::Option * > &driverOptions, const QMap< QString, QgsVectorFileWriter::Option * > &layerOptions, const QString &compulsoryEncoding=QString())
QMap< QString, QgsVectorFileWriter::Option * > driverOptions
QMap< QString, QgsVectorFileWriter::Option * > layerOptions
QString compulsoryEncoding
Some formats require a compulsory encoding, typically UTF-8. If no compulsory encoding,...