Skip to content

Commit 0078b33

Browse files
committedMay 19, 2016
Merge pull request #3082 from rouault/vectorfilewriter_cleanup_and_encoding_improvement
Vectorfilewriter cleanup and encoding improvement
2 parents a19741b + 1ab2977 commit 0078b33

File tree

4 files changed

+73
-269
lines changed

4 files changed

+73
-269
lines changed
 

‎python/core/qgsvectorfilewriter.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ class QgsVectorFileWriter
7474
{
7575
MetaData();
7676

77-
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 );
77+
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() );
7878

7979
QString longName;
8080
QString trLongName;
8181
QString glob;
8282
QString ext;
8383
QMap<QString, QgsVectorFileWriter::Option*> driverOptions;
8484
QMap<QString, QgsVectorFileWriter::Option*> layerOptions;
85+
/** Some formats require a compulsory encoding, typically UTF-8. If no compulsory encoding, empty string */
86+
QString compulsoryEncoding;
8587
};
8688

8789
enum WriterError

‎src/app/ogr/qgsvectorlayersaveasdialog.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx
228228

229229
if ( format() == "KML" )
230230
{
231-
mEncodingComboBox->setCurrentIndex( mEncodingComboBox->findText( "UTF-8" ) );
232-
mEncodingComboBox->setDisabled( true );
233231
mAttributesSelection->setEnabled( true );
234232
selectAllFields = false;
235233
}
@@ -240,7 +238,6 @@ void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx
240238
}
241239
else
242240
{
243-
mEncodingComboBox->setEnabled( true );
244241
mAttributesSelection->setEnabled( true );
245242
fieldsAsDisplayedValues = ( format() == "CSV" || format() == "XLS" || format() == "XLSX" || format() == "ODS" );
246243
}
@@ -372,6 +369,29 @@ void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx
372369
{
373370
mLayerOptionsGroupBox->setVisible( false );
374371
}
372+
373+
if ( driverMetaData.compulsoryEncoding.isEmpty() )
374+
{
375+
mEncodingComboBox->setEnabled( true );
376+
}
377+
else
378+
{
379+
int idx = mEncodingComboBox->findText( driverMetaData.compulsoryEncoding );
380+
if ( idx >= 0 )
381+
{
382+
mEncodingComboBox->setCurrentIndex( idx );
383+
mEncodingComboBox->setDisabled( true );
384+
}
385+
else
386+
{
387+
mEncodingComboBox->setEnabled( true );
388+
}
389+
}
390+
391+
}
392+
else
393+
{
394+
mEncodingComboBox->setEnabled( true );
375395
}
376396
}
377397

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 43 additions & 260 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ void QgsVectorFileWriter::init( QString vectorFileName,
200200
return;
201201
}
202202

203+
MetaData metadata;
204+
bool metadataFound = driverMetadata( driverName, metadata );
205+
203206
if ( mOgrDriverName == "ESRI Shapefile" )
204207
{
205208
if ( layerOptions.join( "" ).toUpper().indexOf( "ENCODING=" ) == -1 )
@@ -235,30 +238,11 @@ void QgsVectorFileWriter::init( QString vectorFileName,
235238

236239
deleteShapeFile( vectorFileName );
237240
}
238-
else if ( driverName == "KML" )
239-
{
240-
if ( !vectorFileName.endsWith( ".kml", Qt::CaseInsensitive ) )
241-
{
242-
vectorFileName += ".kml";
243-
}
244-
245-
if ( fileEncoding.compare( "UTF-8", Qt::CaseInsensitive ) != 0 )
246-
{
247-
QgsDebugMsg( "forced UTF-8 encoding for KML" );
248-
fileEncoding = "UTF-8";
249-
}
250-
251-
QFile::remove( vectorFileName );
252-
}
253241
else
254242
{
255-
QString longName;
256-
QString trLongName;
257-
QString glob;
258-
QString exts;
259-
if ( QgsVectorFileWriter::driverMetadata( driverName, longName, trLongName, glob, exts ) )
243+
if ( metadataFound )
260244
{
261-
QStringList allExts = exts.split( ' ', QString::SkipEmptyParts );
245+
QStringList allExts = metadata.ext.split( ' ', QString::SkipEmptyParts );
262246
bool found = false;
263247
Q_FOREACH ( const QString& ext, allExts )
264248
{
@@ -278,6 +262,16 @@ void QgsVectorFileWriter::init( QString vectorFileName,
278262
QFile::remove( vectorFileName );
279263
}
280264

265+
if ( metadataFound && !metadata.compulsoryEncoding.isEmpty() )
266+
{
267+
if ( fileEncoding.compare( metadata.compulsoryEncoding, Qt::CaseInsensitive ) != 0 )
268+
{
269+
QgsDebugMsg( QString( "forced %1 encoding for %2" ).arg( metadata.compulsoryEncoding ).arg( driverName ) );
270+
fileEncoding = metadata.compulsoryEncoding;
271+
}
272+
273+
}
274+
281275
char **options = nullptr;
282276
if ( !datasourceOptions.isEmpty() )
283277
{
@@ -866,7 +860,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
866860
"*.geojson",
867861
"geojson",
868862
datasetOptions,
869-
layerOptions
863+
layerOptions,
864+
"UTF-8"
870865
)
871866
);
872867

@@ -960,7 +955,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
960955
"*.xml",
961956
"xml",
962957
datasetOptions,
963-
layerOptions
958+
layerOptions,
959+
"UTF-8"
964960
)
965961
);
966962

@@ -1049,7 +1045,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
10491045
"*.gml",
10501046
"gml",
10511047
datasetOptions,
1052-
layerOptions
1048+
layerOptions,
1049+
"UTF-8"
10531050
)
10541051
);
10551052

@@ -1091,7 +1088,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
10911088
"*.gpkg",
10921089
"gpkg",
10931090
datasetOptions,
1094-
layerOptions
1091+
layerOptions,
1092+
"UTF-8"
10951093
)
10961094
);
10971095

@@ -1169,7 +1167,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
11691167
"*.gpx",
11701168
"gpx",
11711169
datasetOptions,
1172-
layerOptions
1170+
layerOptions,
1171+
"UTF-8"
11731172
)
11741173
);
11751174

@@ -1234,7 +1233,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
12341233
"*.kml",
12351234
"kml",
12361235
datasetOptions,
1237-
layerOptions
1236+
layerOptions,
1237+
"UTF-8"
12381238
)
12391239
);
12401240

@@ -1331,21 +1331,6 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
13311331
"" // Default value
13321332
) );
13331333

1334-
driverMetadata.insert( "DGN",
1335-
MetaData(
1336-
"Microstation DGN",
1337-
QObject::tr( "Microstation DGN" ),
1338-
"*.dgn",
1339-
"dgn",
1340-
datasetOptions,
1341-
layerOptions
1342-
)
1343-
);
1344-
1345-
// Microstation DGN
1346-
datasetOptions.clear();
1347-
layerOptions.clear();
1348-
13491334
driverMetadata.insert( "DGN",
13501335
MetaData(
13511336
"Microstation DGN",
@@ -1514,7 +1499,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
15141499
"*.sqlite",
15151500
"sqlite",
15161501
datasetOptions,
1517-
layerOptions
1502+
layerOptions,
1503+
"UTF-8"
15181504
)
15191505
);
15201506

@@ -1595,7 +1581,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
15951581
"*.sqlite",
15961582
"sqlite",
15971583
datasetOptions,
1598-
layerOptions
1584+
layerOptions,
1585+
"UTF-8"
15991586
)
16001587
);
16011588
// AutoCAD DXF
@@ -1685,7 +1672,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
16851672
"*.gdb",
16861673
"gdb",
16871674
datasetOptions,
1688-
layerOptions
1675+
layerOptions,
1676+
"UTF-8"
16891677
)
16901678
);
16911679

@@ -1710,7 +1698,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
17101698
"*.xlsx",
17111699
"xlsx",
17121700
datasetOptions,
1713-
layerOptions
1701+
layerOptions,
1702+
"UTF-8"
17141703
)
17151704
);
17161705

@@ -1735,7 +1724,8 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()
17351724
"*.ods",
17361725
"ods",
17371726
datasetOptions,
1738-
layerOptions
1727+
layerOptions,
1728+
"UTF-8"
17391729
)
17401730
);
17411731

@@ -2543,13 +2533,10 @@ QMap<QString, QString> QgsVectorFileWriter::ogrDriverList()
25432533

25442534
Q_FOREACH ( const QString& drvName, writableDrivers )
25452535
{
2546-
QString longName;
2547-
QString trLongName;
2548-
QString glob;
2549-
QString exts;
2550-
if ( QgsVectorFileWriter::driverMetadata( drvName, longName, trLongName, glob, exts ) && !trLongName.isEmpty() )
2536+
MetaData metadata;
2537+
if ( driverMetadata( drvName, metadata ) && !metadata.trLongName.isEmpty() )
25512538
{
2552-
resultMap.insert( trLongName, drvName );
2539+
resultMap.insert( metadata.trLongName, drvName );
25532540
}
25542541
}
25552542

@@ -2573,14 +2560,11 @@ QString QgsVectorFileWriter::fileFilterString()
25732560

25742561
QString QgsVectorFileWriter::filterForDriver( const QString& driverName )
25752562
{
2576-
QString longName;
2577-
QString trLongName;
2578-
QString glob;
2579-
QString exts;
2580-
if ( !driverMetadata( driverName, longName, trLongName, glob, exts ) || trLongName.isEmpty() || glob.isEmpty() )
2563+
MetaData metadata;
2564+
if ( !driverMetadata( driverName, metadata ) || metadata.trLongName.isEmpty() || metadata.glob.isEmpty() )
25812565
return "";
25822566

2583-
return trLongName + " [OGR] (" + glob.toLower() + ' ' + glob.toUpper() + ')';
2567+
return metadata.trLongName + " [OGR] (" + metadata.glob.toLower() + ' ' + metadata.glob.toUpper() + ')';
25842568
}
25852569

25862570
QString QgsVectorFileWriter::convertCodecNameForEncodingOption( const QString &codecName )
@@ -2600,207 +2584,6 @@ QString QgsVectorFileWriter::convertCodecNameForEncodingOption( const QString &c
26002584
return codecName;
26012585
}
26022586

2603-
bool QgsVectorFileWriter::driverMetadata( const QString& driverName, QString &longName, QString &trLongName, QString &glob, QString &ext )
2604-
{
2605-
if ( driverName.startsWith( "AVCE00" ) )
2606-
{
2607-
longName = "Arc/Info ASCII Coverage";
2608-
trLongName = QObject::tr( "Arc/Info ASCII Coverage" );
2609-
glob = "*.e00";
2610-
ext = "e00";
2611-
}
2612-
else if ( driverName.startsWith( "BNA" ) )
2613-
{
2614-
longName = "Atlas BNA";
2615-
trLongName = QObject::tr( "Atlas BNA" );
2616-
glob = "*.bna";
2617-
ext = "bna";
2618-
}
2619-
else if ( driverName.startsWith( "CSV" ) )
2620-
{
2621-
longName = "Comma Separated Value [CSV]";
2622-
trLongName = QObject::tr( "Comma Separated Value [CSV]" );
2623-
glob = "*.csv";
2624-
ext = "csv";
2625-
}
2626-
else if ( driverName.startsWith( "ESRI" ) )
2627-
{
2628-
longName = "ESRI Shapefile";
2629-
trLongName = QObject::tr( "ESRI Shapefile" );
2630-
glob = "*.shp";
2631-
ext = "shp";
2632-
}
2633-
else if ( driverName.startsWith( "DBF file" ) )
2634-
{
2635-
longName = "DBF File";
2636-
trLongName = QObject::tr( "DBF file" );
2637-
glob = "*.dbf";
2638-
ext = "dbf";
2639-
}
2640-
else if ( driverName.startsWith( "FMEObjects Gateway" ) )
2641-
{
2642-
longName = "FMEObjects Gateway";
2643-
trLongName = QObject::tr( "FMEObjects Gateway" );
2644-
glob = "*.fdd";
2645-
ext = "fdd";
2646-
}
2647-
else if ( driverName.startsWith( "GeoJSON" ) )
2648-
{
2649-
longName = "GeoJSON";
2650-
trLongName = QObject::tr( "GeoJSON" );
2651-
glob = "*.geojson";
2652-
ext = "geojson";
2653-
}
2654-
else if ( driverName.startsWith( "GPKG" ) )
2655-
{
2656-
longName = "GeoPackage";
2657-
trLongName = QObject::tr( "GeoPackage" );
2658-
glob = "*.gpkg";
2659-
ext = "gpkg";
2660-
}
2661-
else if ( driverName.startsWith( "GeoRSS" ) )
2662-
{
2663-
longName = "GeoRSS";
2664-
trLongName = QObject::tr( "GeoRSS" );
2665-
glob = "*.xml";
2666-
ext = "xml";
2667-
}
2668-
else if ( driverName.startsWith( "GML" ) )
2669-
{
2670-
longName = "Geography Markup Language [GML]";
2671-
trLongName = QObject::tr( "Geography Markup Language [GML]" );
2672-
glob = "*.gml";
2673-
ext = "gml";
2674-
}
2675-
else if ( driverName.startsWith( "GMT" ) )
2676-
{
2677-
longName = "Generic Mapping Tools [GMT]";
2678-
trLongName = QObject::tr( "Generic Mapping Tools [GMT]" );
2679-
glob = "*.gmt";
2680-
ext = "gmt";
2681-
}
2682-
else if ( driverName.startsWith( "GPX" ) )
2683-
{
2684-
longName = "GPS eXchange Format [GPX]";
2685-
trLongName = QObject::tr( "GPS eXchange Format [GPX]" );
2686-
glob = "*.gpx";
2687-
ext = "gpx";
2688-
}
2689-
else if ( driverName.startsWith( "Interlis 1" ) )
2690-
{
2691-
longName = "INTERLIS 1";
2692-
trLongName = QObject::tr( "INTERLIS 1" );
2693-
glob = "*.itf *.xml *.ili";
2694-
ext = "ili";
2695-
}
2696-
else if ( driverName.startsWith( "Interlis 2" ) )
2697-
{
2698-
longName = "INTERLIS 2";
2699-
trLongName = QObject::tr( "INTERLIS 2" );
2700-
glob = "*.itf *.xml *.ili";
2701-
ext = "ili";
2702-
}
2703-
else if ( driverName.startsWith( "KML" ) )
2704-
{
2705-
longName = "Keyhole Markup Language [KML]";
2706-
trLongName = QObject::tr( "Keyhole Markup Language [KML]" );
2707-
glob = "*.kml";
2708-
ext = "kml";
2709-
}
2710-
else if ( driverName.startsWith( "MapInfo File" ) )
2711-
{
2712-
longName = "Mapinfo TAB";
2713-
trLongName = QObject::tr( "Mapinfo TAB" );
2714-
glob = "*.tab";
2715-
ext = "tab";
2716-
}
2717-
// 'MapInfo MIF' is internal QGIS addition to distinguish between MITAB and MIF
2718-
else if ( driverName.startsWith( "MapInfo MIF" ) )
2719-
{
2720-
longName = "Mapinfo MIF";
2721-
trLongName = QObject::tr( "Mapinfo MIF" );
2722-
glob = "*.mif";
2723-
ext = "mif";
2724-
}
2725-
else if ( driverName.startsWith( "DGN" ) )
2726-
{
2727-
longName = "Microstation DGN";
2728-
trLongName = QObject::tr( "Microstation DGN" );
2729-
glob = "*.dgn";
2730-
ext = "dgn";
2731-
}
2732-
else if ( driverName.startsWith( "S57" ) )
2733-
{
2734-
longName = "S-57 Base file";
2735-
trLongName = QObject::tr( "S-57 Base file" );
2736-
glob = "*.000";
2737-
ext = "000";
2738-
}
2739-
else if ( driverName.startsWith( "SDTS" ) )
2740-
{
2741-
longName = "Spatial Data Transfer Standard [SDTS]";
2742-
trLongName = QObject::tr( "Spatial Data Transfer Standard [SDTS]" );
2743-
glob = "*catd.ddf";
2744-
ext = "ddf";
2745-
}
2746-
else if ( driverName.startsWith( "SQLite" ) )
2747-
{
2748-
longName = "SQLite";
2749-
trLongName = QObject::tr( "SQLite" );
2750-
glob = "*.sqlite";
2751-
ext = "sqlite";
2752-
}
2753-
// QGIS internal addition for SpatialLite
2754-
else if ( driverName.startsWith( "SpatiaLite" ) )
2755-
{
2756-
longName = "SpatiaLite";
2757-
trLongName = QObject::tr( "SpatiaLite" );
2758-
glob = "*.sqlite";
2759-
ext = "sqlite";
2760-
}
2761-
else if ( driverName.startsWith( "DXF" ) )
2762-
{
2763-
longName = "AutoCAD DXF";
2764-
trLongName = QObject::tr( "AutoCAD DXF" );
2765-
glob = "*.dxf";
2766-
ext = "dxf";
2767-
}
2768-
else if ( driverName.startsWith( "Geoconcept" ) )
2769-
{
2770-
longName = "Geoconcept";
2771-
trLongName = QObject::tr( "Geoconcept" );
2772-
glob = "*.gxt *.txt";
2773-
ext = "gxt";
2774-
}
2775-
else if ( driverName.startsWith( "FileGDB" ) )
2776-
{
2777-
longName = "ESRI FileGDB";
2778-
trLongName = QObject::tr( "ESRI FileGDB" );
2779-
glob = "*.gdb";
2780-
ext = "gdb";
2781-
}
2782-
else if ( driverName.startsWith( "XLSX" ) )
2783-
{
2784-
longName = "MS Office Open XML spreadsheet [XLSX]";
2785-
trLongName = QObject::tr( "MS Office Open XML spreadsheet [XLSX]" );
2786-
glob = "*.xlsx";
2787-
ext = "xlsx";
2788-
}
2789-
else if ( driverName.startsWith( "ODS" ) )
2790-
{
2791-
longName = "Open Document Spreadsheet";
2792-
trLongName = QObject::tr( "Open Document Spreadsheet [ODS]" );
2793-
glob = "*.ods";
2794-
ext = "ods";
2795-
}
2796-
else
2797-
{
2798-
return false;
2799-
}
2800-
2801-
return true;
2802-
}
2803-
28042587
void QgsVectorFileWriter::createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds )
28052588
{
28062589
if ( !vl || !ds )

‎src/core/qgsvectorfilewriter.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ class CORE_EXPORT QgsVectorFileWriter
120120
MetaData()
121121
{}
122122

123-
MetaData( const QString& longName, const QString& trLongName, const QString& glob, const QString& ext, const QMap<QString, Option*>& driverOptions, const QMap<QString, Option*>& layerOptions )
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() )
124124
: longName( longName )
125125
, trLongName( trLongName )
126126
, glob( glob )
127127
, ext( ext )
128128
, driverOptions( driverOptions )
129129
, layerOptions( layerOptions )
130+
, compulsoryEncoding( compulsoryEncoding )
130131
{}
131132

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

140143
enum WriterError
@@ -426,10 +429,6 @@ class CORE_EXPORT QgsVectorFileWriter
426429
QgsRenderContext mRenderContext;
427430

428431
static QMap<QString, MetaData> initMetaData();
429-
/**
430-
* @deprecated
431-
*/
432-
static bool driverMetadata( const QString& driverName, QString &longName, QString &trLongName, QString &glob, QString &ext );
433432
void createSymbolLayerTable( QgsVectorLayer* vl, const QgsCoordinateTransform* ct, OGRDataSourceH ds );
434433
OGRFeatureH createFeature( QgsFeature& feature );
435434
bool writeFeature( OGRLayerH layer, OGRFeatureH feature );

0 commit comments

Comments
 (0)
Please sign in to comment.