Skip to content
Permalink
Browse files

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.
  • Loading branch information
rouault committed May 19, 2016
1 parent b1efb9a commit 3ee7d59e18ab5b5a235f2a4e0fdcfb55c54883d0
Showing with 39 additions and 28 deletions.
  1. +3 −1 python/core/qgsvectorfilewriter.sip
  2. +32 −26 src/core/qgsvectorfilewriter.cpp
  3. +4 −1 src/core/qgsvectorfilewriter.h
@@ -74,14 +74,16 @@ class QgsVectorFileWriter
{
MetaData();

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 );
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() );

QString longName;
QString trLongName;
QString glob;
QString ext;
QMap<QString, QgsVectorFileWriter::Option*> driverOptions;
QMap<QString, QgsVectorFileWriter::Option*> layerOptions;
/** Some formats require a compulsory encoding, typically UTF-8. If no compulsory encoding, empty string */
QString compulsoryEncoding;
};

enum WriterError
@@ -238,21 +238,6 @@ void QgsVectorFileWriter::init( QString vectorFileName,

deleteShapeFile( vectorFileName );
}
else if ( driverName == "KML" )
{
if ( !vectorFileName.endsWith( ".kml", Qt::CaseInsensitive ) )
{
vectorFileName += ".kml";
}

if ( fileEncoding.compare( "UTF-8", Qt::CaseInsensitive ) != 0 )
{
QgsDebugMsg( "forced UTF-8 encoding for KML" );
fileEncoding = "UTF-8";
}

QFile::remove( vectorFileName );
}
else
{
if ( metadataFound )
@@ -277,6 +262,16 @@ void QgsVectorFileWriter::init( QString vectorFileName,
QFile::remove( vectorFileName );
}

if ( metadataFound && !metadata.compulsoryEncoding.isEmpty() )
{
if ( fileEncoding.compare( metadata.compulsoryEncoding, Qt::CaseInsensitive ) != 0 )
{
QgsDebugMsg( QString( "forced %1 encoding for %2" ).arg( metadata.compulsoryEncoding ).arg( driverName ) );
fileEncoding = metadata.compulsoryEncoding;
}

}

char **options = nullptr;
if ( !datasourceOptions.isEmpty() )
{
@@ -865,7 +860,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.geojson",
"geojson",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -959,7 +955,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.xml",
"xml",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1048,7 +1045,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.gml",
"gml",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1090,7 +1088,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.gpkg",
"gpkg",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1168,7 +1167,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.gpx",
"gpx",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1233,7 +1233,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.kml",
"kml",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1498,7 +1499,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.sqlite",
"sqlite",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1579,7 +1581,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.sqlite",
"sqlite",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);
// AutoCAD DXF
@@ -1669,7 +1672,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.gdb",
"gdb",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1694,7 +1698,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.xlsx",
"xlsx",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -1719,7 +1724,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
"*.ods",
"ods",
datasetOptions,
layerOptions
layerOptions,
"UTF-8"
)
);

@@ -120,13 +120,14 @@ class CORE_EXPORT QgsVectorFileWriter
MetaData()
{}

MetaData( const QString& longName, const QString& trLongName, const QString& glob, const QString& ext, const QMap<QString, Option*>& driverOptions, const QMap<QString, Option*>& layerOptions )
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() )
: longName( longName )
, trLongName( trLongName )
, glob( glob )
, ext( ext )
, driverOptions( driverOptions )
, layerOptions( layerOptions )
, compulsoryEncoding( compulsoryEncoding )
{}

QString longName;
@@ -135,6 +136,8 @@ class CORE_EXPORT QgsVectorFileWriter
QString ext;
QMap<QString, Option*> driverOptions;
QMap<QString, Option*> layerOptions;
/** Some formats require a compulsory encoding, typically UTF-8. If no compulsory encoding, empty string */
QString compulsoryEncoding;
};

enum WriterError

0 comments on commit 3ee7d59

Please sign in to comment.
You can’t perform that action at this time.