2016-02-14 03:50:23 +01:00
|
|
|
/** \ingroup core
|
|
|
|
* A convenience class for writing vector files to disk.
|
2007-04-11 11:46:35 +00:00
|
|
|
There are two possibilities how to use this class:
|
2016-05-03 18:53:21 +02:00
|
|
|
1. static call to QgsVectorFileWriter::writeAsVectorFormat(...) which saves the whole vector layer
|
2007-04-11 11:46:35 +00:00
|
|
|
2. create an instance of the class and issue calls to addFeature(...)
|
|
|
|
*/
|
|
|
|
class QgsVectorFileWriter
|
|
|
|
{
|
|
|
|
%TypeHeaderCode
|
|
|
|
#include <qgsvectorfilewriter.h>
|
|
|
|
#include <qgsfield.h>
|
|
|
|
%End
|
|
|
|
|
2012-09-24 02:28:15 +02:00
|
|
|
public:
|
2014-01-26 18:35:21 +01:00
|
|
|
enum OptionType
|
|
|
|
{
|
|
|
|
Set,
|
|
|
|
String,
|
|
|
|
Int,
|
|
|
|
Hidden
|
|
|
|
};
|
|
|
|
|
|
|
|
class Option
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Option( const QString& docString, QgsVectorFileWriter::OptionType type );
|
|
|
|
virtual ~Option();
|
|
|
|
|
|
|
|
QString docString;
|
|
|
|
QgsVectorFileWriter::OptionType type;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SetOption : QgsVectorFileWriter::Option
|
|
|
|
{
|
|
|
|
public:
|
2015-10-07 11:55:34 +11:00
|
|
|
SetOption( const QString& docString, const QStringList& values, const QString& defaultValue, bool allowNone = false );
|
2014-01-26 18:35:21 +01:00
|
|
|
|
|
|
|
QSet<QString> values;
|
|
|
|
QString defaultValue;
|
|
|
|
bool allowNone;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StringOption: QgsVectorFileWriter::Option
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StringOption( const QString& docString, const QString& defaultValue = QString() );
|
|
|
|
|
|
|
|
QString defaultValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IntOption: QgsVectorFileWriter::Option
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IntOption( const QString& docString, int defaultValue );
|
|
|
|
|
|
|
|
int defaultValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BoolOption : QgsVectorFileWriter::SetOption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BoolOption( const QString& docString, bool defaultValue );
|
|
|
|
};
|
|
|
|
|
|
|
|
class HiddenOption : QgsVectorFileWriter::Option
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HiddenOption( const QString& value );
|
|
|
|
|
|
|
|
QString mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MetaData
|
|
|
|
{
|
|
|
|
MetaData();
|
|
|
|
|
QgsVectorFileWriter::MetaData: add a compulsoryEncoding member
* Some formats require a compulsory encoding, typically UTF-8.
Change initMetadata() to indicate UTF-8 compulsory encoding for
GeoJSON, GeoRSS, GML, GPKG, GPX, KML, SQLite, SpatiaLite, FileGDB,
XLSX and ODS
* QgsVectorFileWriter::init(): make it override the user specified
encoding if the format has a compulsory encoding. And remove hard-coded
case for KML.
2016-05-19 11:38:42 +02:00
|
|
|
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() );
|
2014-01-26 18:35:21 +01:00
|
|
|
|
|
|
|
QString longName;
|
|
|
|
QString trLongName;
|
|
|
|
QString glob;
|
|
|
|
QString ext;
|
|
|
|
QMap<QString, QgsVectorFileWriter::Option*> driverOptions;
|
|
|
|
QMap<QString, QgsVectorFileWriter::Option*> layerOptions;
|
QgsVectorFileWriter::MetaData: add a compulsoryEncoding member
* Some formats require a compulsory encoding, typically UTF-8.
Change initMetadata() to indicate UTF-8 compulsory encoding for
GeoJSON, GeoRSS, GML, GPKG, GPX, KML, SQLite, SpatiaLite, FileGDB,
XLSX and ODS
* QgsVectorFileWriter::init(): make it override the user specified
encoding if the format has a compulsory encoding. And remove hard-coded
case for KML.
2016-05-19 11:38:42 +02:00
|
|
|
/** Some formats require a compulsory encoding, typically UTF-8. If no compulsory encoding, empty string */
|
|
|
|
QString compulsoryEncoding;
|
2014-01-26 18:35:21 +01:00
|
|
|
};
|
|
|
|
|
2007-04-11 11:46:35 +00:00
|
|
|
enum WriterError
|
|
|
|
{
|
2014-06-02 21:00:51 +02:00
|
|
|
NoError,
|
2007-04-11 11:46:35 +00:00
|
|
|
ErrDriverNotFound,
|
|
|
|
ErrCreateDataSource,
|
2011-08-04 19:48:54 +02:00
|
|
|
ErrCreateLayer,
|
|
|
|
ErrAttributeTypeUnsupported,
|
|
|
|
ErrAttributeCreationFailed,
|
2014-11-21 01:17:21 +01:00
|
|
|
ErrProjection,
|
|
|
|
ErrFeatureWriteFailed,
|
|
|
|
ErrInvalidLayer,
|
2007-04-11 11:46:35 +00:00
|
|
|
};
|
|
|
|
|
2013-10-31 16:07:27 +01:00
|
|
|
enum SymbologyExport
|
|
|
|
{
|
2014-06-02 21:00:51 +02:00
|
|
|
NoSymbology, //export only data
|
2013-10-31 16:07:27 +01:00
|
|
|
FeatureSymbology, //Keeps the number of features and export symbology per feature
|
|
|
|
SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
|
|
|
|
};
|
|
|
|
|
2016-05-03 18:53:21 +02:00
|
|
|
/** Interface to convert raw field values to their user-friendly value.
|
|
|
|
* @note Added in QGIS 2.16
|
|
|
|
*/
|
|
|
|
class FieldValueConverter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Constructor */
|
|
|
|
FieldValueConverter();
|
|
|
|
|
|
|
|
/** Destructor */
|
|
|
|
virtual ~FieldValueConverter();
|
|
|
|
|
|
|
|
/** Return a possibly modified field definition. Default implementation will return provided field unmodified.
|
|
|
|
* @param field original field definition
|
|
|
|
* @return possibly modified field definition
|
|
|
|
*/
|
|
|
|
virtual QgsField fieldDefinition( const QgsField& field );
|
|
|
|
|
|
|
|
/** Convert the provided value, for field fieldIdxInLayer. Default implementation will return provided value unmodified.
|
|
|
|
* @param fieldIdxInLayer field index
|
|
|
|
* @param value original raw value
|
|
|
|
* @return possibly modified value.
|
|
|
|
*/
|
|
|
|
virtual QVariant convert( int fieldIdxInLayer, const QVariant& value );
|
|
|
|
};
|
|
|
|
|
2014-01-26 18:35:21 +01:00
|
|
|
/** Write contents of vector layer to an (OGR supported) vector formt
|
2016-02-14 03:50:23 +01:00
|
|
|
* @param layer layer to write
|
|
|
|
* @param fileName file name to write to
|
|
|
|
* @param fileEncoding encoding to use
|
|
|
|
* @param destCRS pointer to CRS to reproject exported geometries to
|
|
|
|
* @param driverName OGR driver to use
|
|
|
|
* @param onlySelected write only selected features of layer
|
|
|
|
* @param errorMessage pointer to buffer fo error message
|
|
|
|
* @param datasourceOptions list of OGR data source creation options
|
|
|
|
* @param layerOptions list of OGR layer creation options
|
|
|
|
* @param skipAttributeCreation only write geometries
|
|
|
|
* @param newFilename QString pointer which will contain the new file name created (in case it is different to fileName).
|
|
|
|
* @param symbologyExport symbology to export
|
|
|
|
* @param symbologyScale scale of symbology
|
|
|
|
* @param filterExtent if not a null pointer, only features intersecting the extent will be saved (added in QGIS 2.4)
|
|
|
|
* @param overrideGeometryType set to a valid geometry type to override the default geometry type for the layer. This parameter
|
|
|
|
* allows for conversion of geometryless tables to null geometries, etc (added in QGIS 2.14)
|
|
|
|
* @param forceMulti set to true to force creation of multi* geometries (added in QGIS 2.14)
|
|
|
|
* @param includeZ set to true to include z dimension in output. This option is only valid if overrideGeometryType is set. (added in QGIS 2.14)
|
2016-04-03 01:46:33 +02:00
|
|
|
* @param attributes attributes to export (empty means all unless skipAttributeCreation is set)
|
2016-05-03 18:53:21 +02:00
|
|
|
* @param fieldValueConverter field value converter (added in QGIS 2.16)
|
2016-02-14 03:50:23 +01:00
|
|
|
*/
|
2013-10-31 16:07:27 +01:00
|
|
|
static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
|
|
|
|
const QString& fileName,
|
|
|
|
const QString& fileEncoding,
|
2014-01-26 18:35:21 +01:00
|
|
|
const QgsCoordinateReferenceSystem *destCRS,
|
2013-10-31 16:07:27 +01:00
|
|
|
const QString& driverName = "ESRI Shapefile",
|
|
|
|
bool onlySelected = false,
|
|
|
|
QString *errorMessage = 0,
|
2014-11-21 01:17:21 +01:00
|
|
|
const QStringList &datasourceOptions = QStringList(),
|
|
|
|
const QStringList &layerOptions = QStringList(),
|
|
|
|
bool skipAttributeCreation = false,
|
|
|
|
QString *newFilename = 0,
|
|
|
|
SymbologyExport symbologyExport = NoSymbology,
|
|
|
|
double symbologyScale = 1.0,
|
2015-11-24 06:30:15 +11:00
|
|
|
const QgsRectangle* filterExtent = 0,
|
|
|
|
QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
|
|
|
|
bool forceMulti = false,
|
2016-04-03 01:46:33 +02:00
|
|
|
bool includeZ = false,
|
2016-05-03 18:53:21 +02:00
|
|
|
QgsAttributeList attributes = QgsAttributeList(),
|
|
|
|
FieldValueConverter* fieldValueConverter = nullptr
|
2013-10-31 16:07:27 +01:00
|
|
|
);
|
|
|
|
|
2015-11-24 06:30:15 +11:00
|
|
|
/** Writes a layer out to a vector file.
|
|
|
|
* @param layer layer to write
|
|
|
|
* @param fileName file name to write to
|
|
|
|
* @param fileEncoding encoding to use
|
2016-04-03 01:46:33 +02:00
|
|
|
* @param ct pointer to coordinate transform to reproject exported geometries with
|
2015-11-24 06:30:15 +11:00
|
|
|
* @param driverName OGR driver to use
|
|
|
|
* @param onlySelected write only selected features of layer
|
|
|
|
* @param errorMessage pointer to buffer fo error message
|
|
|
|
* @param datasourceOptions list of OGR data source creation options
|
|
|
|
* @param layerOptions list of OGR layer creation options
|
|
|
|
* @param skipAttributeCreation only write geometries
|
|
|
|
* @param newFilename QString pointer which will contain the new file name created (in case it is different to fileName).
|
|
|
|
* @param symbologyExport symbology to export
|
|
|
|
* @param symbologyScale scale of symbology
|
|
|
|
* @param filterExtent if not a null pointer, only features intersecting the extent will be saved (added in QGIS 2.4)
|
|
|
|
* @param overrideGeometryType set to a valid geometry type to override the default geometry type for the layer. This parameter
|
|
|
|
* allows for conversion of geometryless tables to null geometries, etc (added in QGIS 2.14)
|
|
|
|
* @param forceMulti set to true to force creation of multi* geometries (added in QGIS 2.14)
|
|
|
|
* @param includeZ set to true to include z dimension in output. This option is only valid if overrideGeometryType is set. (added in QGIS 2.14)
|
2016-04-03 01:46:33 +02:00
|
|
|
* @param attributes attributes to export (empty means all unless skipAttributeCreation is set)
|
2016-05-03 18:53:21 +02:00
|
|
|
* @param fieldValueConverter field value converter (added in QGIS 2.16)
|
2016-04-03 01:46:33 +02:00
|
|
|
* @note added in 2.2
|
2015-11-24 06:30:15 +11:00
|
|
|
*/
|
2010-03-18 22:24:18 +00:00
|
|
|
static WriterError writeAsVectorFormat( QgsVectorLayer* layer,
|
|
|
|
const QString& fileName,
|
|
|
|
const QString& fileEncoding,
|
2014-01-26 18:35:21 +01:00
|
|
|
const QgsCoordinateTransform* ct,
|
2010-03-18 22:24:18 +00:00
|
|
|
const QString& driverName = "ESRI Shapefile",
|
2012-09-24 02:28:15 +02:00
|
|
|
bool onlySelected = false,
|
2010-08-28 23:14:16 +00:00
|
|
|
QString *errorMessage = 0,
|
2014-03-03 17:56:10 +07:00
|
|
|
const QStringList &datasourceOptions = QStringList(),
|
|
|
|
const QStringList &layerOptions = QStringList(),
|
|
|
|
bool skipAttributeCreation = false,
|
|
|
|
QString *newFilename = 0,
|
|
|
|
SymbologyExport symbologyExport = NoSymbology,
|
|
|
|
double symbologyScale = 1.0,
|
2015-11-24 06:30:15 +11:00
|
|
|
const QgsRectangle* filterExtent = 0,
|
|
|
|
QgsWKBTypes::Type overrideGeometryType = QgsWKBTypes::Unknown,
|
|
|
|
bool forceMulti = false,
|
2016-04-03 01:46:33 +02:00
|
|
|
bool includeZ = false,
|
2016-05-03 18:53:21 +02:00
|
|
|
QgsAttributeList attributes = QgsAttributeList(),
|
|
|
|
FieldValueConverter* fieldValueConverter = nullptr
|
2012-09-24 02:28:15 +02:00
|
|
|
);
|
2010-03-18 22:24:18 +00:00
|
|
|
|
2016-02-14 03:50:23 +01:00
|
|
|
/** Create a new vector file writer */
|
2012-09-24 02:28:15 +02:00
|
|
|
QgsVectorFileWriter( const QString& vectorFileName,
|
|
|
|
const QString& fileEncoding,
|
2012-10-20 22:19:55 +02:00
|
|
|
const QgsFields& fields,
|
2012-09-24 02:28:15 +02:00
|
|
|
QGis::WkbType geometryType,
|
|
|
|
const QgsCoordinateReferenceSystem* srs,
|
|
|
|
const QString& driverName = "ESRI Shapefile",
|
2014-11-21 01:17:21 +01:00
|
|
|
const QStringList &datasourceOptions = QStringList(),
|
|
|
|
const QStringList &layerOptions = QStringList(),
|
2016-01-08 17:35:19 +01:00
|
|
|
QString *newFilename = nullptr,
|
|
|
|
SymbologyExport symbologyExport = NoSymbology
|
|
|
|
);
|
|
|
|
|
|
|
|
/** Create a new vector file writer */
|
|
|
|
QgsVectorFileWriter( const QString& vectorFileName,
|
|
|
|
const QString& fileEncoding,
|
|
|
|
const QgsFields& fields,
|
|
|
|
QgsWKBTypes::Type geometryType,
|
|
|
|
const QgsCoordinateReferenceSystem* srs,
|
|
|
|
const QString& driverName = "ESRI Shapefile",
|
|
|
|
const QStringList &datasourceOptions = QStringList(),
|
|
|
|
const QStringList &layerOptions = QStringList(),
|
|
|
|
QString *newFilename = nullptr,
|
|
|
|
SymbologyExport symbologyExport = NoSymbology
|
2012-11-22 10:35:20 +02:00
|
|
|
);
|
2010-03-18 22:24:18 +00:00
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Returns map with format filter string as key and OGR format key as value*/
|
2010-03-18 22:24:18 +00:00
|
|
|
static QMap< QString, QString> supportedFiltersAndFormats();
|
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Returns driver list that can be used for dialogs. It contains all OGR drivers
|
2014-01-26 18:35:21 +01:00
|
|
|
* + some additional internal QGIS driver names to distinguish between more
|
|
|
|
* supported formats of the same OGR driver
|
|
|
|
*/
|
2012-09-24 02:28:15 +02:00
|
|
|
static QMap< QString, QString> ogrDriverList();
|
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Returns filter string that can be used for dialogs*/
|
2010-03-18 22:24:18 +00:00
|
|
|
static QString fileFilterString();
|
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Creates a filter for an OGR driver key*/
|
2010-03-18 22:24:18 +00:00
|
|
|
static QString filterForDriver( const QString& driverName );
|
2012-09-24 02:28:15 +02:00
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Converts codec name to string passed to ENCODING layer creation option of OGR Shapefile*/
|
2014-01-26 18:35:21 +01:00
|
|
|
static QString convertCodecNameForEncodingOption( const QString &codecName );
|
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Checks whether there were any errors in constructor */
|
2007-04-11 11:46:35 +00:00
|
|
|
WriterError hasError();
|
2010-02-06 19:27:36 +00:00
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Retrieves error message */
|
2010-02-06 19:27:36 +00:00
|
|
|
QString errorMessage();
|
2012-09-24 02:28:15 +02:00
|
|
|
|
2016-02-14 03:50:23 +01:00
|
|
|
/** Add feature to the currently opened data source */
|
2014-01-26 18:35:21 +01:00
|
|
|
bool addFeature( QgsFeature& feature, QgsFeatureRendererV2* renderer = 0, QGis::UnitType outputUnit = QGis::Meters );
|
2012-09-24 02:28:15 +02:00
|
|
|
|
2014-01-26 18:35:21 +01:00
|
|
|
//! @note not available in python bindings
|
2012-09-24 02:28:15 +02:00
|
|
|
// QMap<int, int> attrIdxToOgrIdx();
|
|
|
|
|
2015-07-29 11:52:14 +02:00
|
|
|
/** Close opened shapefile for writing */
|
2007-04-11 11:46:35 +00:00
|
|
|
~QgsVectorFileWriter();
|
2012-09-24 02:28:15 +02:00
|
|
|
|
2008-06-08 00:55:29 +00:00
|
|
|
/** Delete a shapefile (and its accompanying shx / dbf / prf)
|
2012-09-24 02:28:15 +02:00
|
|
|
* @param theFileName /path/to/file.shp
|
2008-06-08 00:55:29 +00:00
|
|
|
* @return bool true if the file was deleted successfully
|
2012-09-24 02:28:15 +02:00
|
|
|
*/
|
2015-10-07 11:55:34 +11:00
|
|
|
static bool deleteShapeFile( const QString& theFileName );
|
2012-09-24 02:28:15 +02:00
|
|
|
|
2014-01-26 18:35:21 +01:00
|
|
|
SymbologyExport symbologyExport() const;
|
|
|
|
void setSymbologyExport( SymbologyExport symExport );
|
|
|
|
|
|
|
|
double symbologyScaleDenominator() const;
|
|
|
|
void setSymbologyScaleDenominator( double d );
|
|
|
|
|
|
|
|
static bool driverMetadata( const QString& driverName, MetaData& driverMetadata );
|
|
|
|
|
2012-09-24 02:28:15 +02:00
|
|
|
protected:
|
2014-05-27 23:22:50 +02:00
|
|
|
//! @note not available in python bindings
|
2012-09-24 02:28:15 +02:00
|
|
|
// OGRGeometryH createEmptyGeometry( QGis::WkbType wkbType );
|
2016-01-22 18:45:45 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
QgsVectorFileWriter( const QgsVectorFileWriter& rh );
|
2007-04-11 11:46:35 +00:00
|
|
|
};
|