Skip to content

Commit

Permalink
VectorFileWriter/OGR provider: workaround GDAL 3.1.x bug regarding XL…
Browse files Browse the repository at this point in the history
…SX and ODS creation

GDAL 3.1.0 to 3.1.3 will create XLSX and ODS files with ZIP64 extensions,
which make them incompatible of current LibreOffice versions.

This has been fixed in GDAL now, but this can be workaround on QGIS side too
if using those buggy versions.
  • Loading branch information
rouault authored and nyalldawson committed Sep 18, 2020
1 parent dd9e0ba commit 3d0a14e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -4340,6 +4340,18 @@ void QgsOgrProviderUtils::GDALCloseWrapper( GDALDatasetH hDS )
GDALClose( hDS );
}
}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,1,0) && GDAL_VERSION_NUM <= GDAL_COMPUTE_VERSION(3,1,3)
else if ( mGDALDriverName == QLatin1String( "XLSX" ) ||
mGDALDriverName == QLatin1String( "ODS" ) )
{
// Workaround bug in GDAL 3.1.0 to 3.1.3 that creates XLSX and ODS files incompatible with LibreOffice due to use of ZIP64
CPLSetThreadLocalConfigOption( "CPL_CREATE_ZIP64", "NO" );
GDALClose( hDS );
CPLSetThreadLocalConfigOption( "CPL_CREATE_ZIP64", nullptr );
}
#endif

else
{
GDALClose( hDS );
Expand Down Expand Up @@ -6186,7 +6198,25 @@ OGRErr QgsOgrLayer::RollbackTransaction()
OGRErr QgsOgrLayer::SyncToDisk()
{
QMutexLocker locker( &ds->mutex );
return OGR_L_SyncToDisk( hLayer );

OGRErr eErr;
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,1,0) && GDAL_VERSION_NUM <= GDAL_COMPUTE_VERSION(3,1,3)
// Workaround bug in GDAL 3.1.0 to 3.1.3 that creates XLSX and ODS files incompatible with LibreOffice due to use of ZIP64
QString drvName = GDALGetDriverShortName( GDALGetDatasetDriver( ds->hDS ) );
if ( drvName == QLatin1String( "XLSX" ) ||
drvName == QLatin1String( "ODS" ) )
{
CPLSetThreadLocalConfigOption( "CPL_CREATE_ZIP64", "NO" );
eErr = OGR_L_SyncToDisk( hLayer );
CPLSetThreadLocalConfigOption( "CPL_CREATE_ZIP64", nullptr );
}
else
#endif
{
eErr = OGR_L_SyncToDisk( hLayer );
}

return eErr;
}

void QgsOgrLayer::ExecuteSQLNoReturn( const QByteArray &sql )
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2696,6 +2696,21 @@ QgsVectorFileWriter::~QgsVectorFileWriter()
}
}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,1,0) && GDAL_VERSION_NUM <= GDAL_COMPUTE_VERSION(3,1,3)
if ( mDS )
{
// Workaround bug in GDAL 3.1.0 to 3.1.3 that creates XLSX and ODS files incompatible with LibreOffice due to use of ZIP64
QString drvName = GDALGetDriverShortName( GDALGetDatasetDriver( mDS.get() ) );
if ( drvName == QLatin1String( "XLSX" ) ||
drvName == QLatin1String( "ODS" ) )
{
CPLSetThreadLocalConfigOption( "CPL_CREATE_ZIP64", "NO" );
mDS.reset();
CPLSetThreadLocalConfigOption( "CPL_CREATE_ZIP64", nullptr );
}
}
#endif

mDS.reset();

if ( mOgrRef )
Expand Down

0 comments on commit 3d0a14e

Please sign in to comment.