Skip to content

Commit 299d152

Browse files
committed
Workaround for GDAL ticket 5170, fixes #8356
1 parent 763d505 commit 299d152

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri, bool update )
139139
QString gdalUri = dataSourceUri();
140140

141141
CPLErrorReset();
142-
mGdalBaseDataset = GDALOpen( TO8F( gdalUri ), mUpdate ? GA_Update : GA_ReadOnly );
142+
mGdalBaseDataset = gdalOpen( TO8F( gdalUri ), mUpdate ? GA_Update : GA_ReadOnly );
143143

144144
if ( !mGdalBaseDataset )
145145
{
@@ -1424,12 +1424,12 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> & theRaste
14241424
GDALClose( mGdalDataset );
14251425
//mGdalBaseDataset = GDALOpen( QFile::encodeName( dataSourceUri() ).constData(), GA_Update );
14261426

1427-
mGdalBaseDataset = GDALOpen( TO8F( dataSourceUri() ), GA_Update );
1427+
mGdalBaseDataset = gdalOpen( TO8F( dataSourceUri() ), GA_Update );
14281428

14291429
// if the dataset couldn't be opened in read / write mode, tell the user
14301430
if ( !mGdalBaseDataset )
14311431
{
1432-
mGdalBaseDataset = GDALOpen( TO8F( dataSourceUri() ), GA_ReadOnly );
1432+
mGdalBaseDataset = gdalOpen( TO8F( dataSourceUri() ), GA_ReadOnly );
14331433
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
14341434
mGdalDataset = mGdalBaseDataset;
14351435
return "ERROR_WRITE_FORMAT";
@@ -1521,7 +1521,7 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> & theRaste
15211521
//something bad happenend
15221522
//QString myString = QString (CPLGetLastError());
15231523
GDALClose( mGdalBaseDataset );
1524-
mGdalBaseDataset = GDALOpen( TO8F( dataSourceUri() ), mUpdate ? GA_Update : GA_ReadOnly );
1524+
mGdalBaseDataset = gdalOpen( TO8F( dataSourceUri() ), mUpdate ? GA_Update : GA_ReadOnly );
15251525
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
15261526
mGdalDataset = mGdalBaseDataset;
15271527

@@ -1576,7 +1576,7 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> & theRaste
15761576
QgsDebugMsg( "Reopening dataset ..." );
15771577
//close the gdal dataset and reopen it in read only mode
15781578
GDALClose( mGdalBaseDataset );
1579-
mGdalBaseDataset = GDALOpen( TO8F( dataSourceUri() ), mUpdate ? GA_Update : GA_ReadOnly );
1579+
mGdalBaseDataset = gdalOpen( TO8F( dataSourceUri() ), mUpdate ? GA_Update : GA_ReadOnly );
15801580
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
15811581
mGdalDataset = mGdalBaseDataset;
15821582
}
@@ -2029,7 +2029,7 @@ QGISEXTERN bool isValidRasterFileName( QString const & theFileNameQString, QStri
20292029

20302030
//open the file using gdal making sure we have handled locale properly
20312031
//myDataset = GDALOpen( QFile::encodeName( theFileNameQString ).constData(), GA_ReadOnly );
2032-
myDataset = GDALOpen( TO8F( fileName ), GA_ReadOnly );
2032+
myDataset = QgsGdalProviderBase::gdalOpen( TO8F( fileName ), GA_ReadOnly );
20332033
if ( !myDataset )
20342034
{
20352035
if ( CPLGetLastErrorNo() != CPLE_OpenFailed )

src/providers/gdal/qgsgdalproviderbase.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* *
1616
***************************************************************************/
1717

18+
#define CPL_SUPRESS_CPLUSPLUS
19+
#include "cpl_conv.h"
20+
1821
#include "qgsapplication.h"
1922
#include "qgslogger.h"
2023
#include "qgsgdalproviderbase.h"
@@ -273,3 +276,21 @@ QgsRectangle QgsGdalProviderBase::extent( GDALDatasetH gdalDataset )const
273276
QgsRectangle extent( myGeoTransform[0], myYMin, myXMax, myGeoTransform[3] );
274277
return extent;
275278
}
279+
280+
GDALDatasetH QgsGdalProviderBase::gdalOpen( const char *pszFilename, GDALAccess eAccess )
281+
{
282+
// See http://hub.qgis.org/issues/8356 and http://trac.osgeo.org/gdal/ticket/5170
283+
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
284+
char* pszOldVal = CPLStrdup( CPLGetConfigOption( "VSI_CACHE", "FALSE" ) );
285+
CPLSetThreadLocalConfigOption( "VSI_CACHE", "FALSE" );
286+
#endif
287+
288+
GDALDatasetH hDS = GDALOpen( pszFilename, eAccess );
289+
290+
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
291+
CPLSetThreadLocalConfigOption( "VSI_CACHE", pszOldVal );
292+
CPLFree( pszOldVal );
293+
#endif
294+
295+
return hDS;
296+
}

src/providers/gdal/qgsgdalproviderbase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class QgsGdalProviderBase
4646

4747
/** \brief ensures that GDAL drivers are registered, but only once */
4848
static void registerGdalDrivers();
49+
50+
/** Wrapper function for GDALOpen to get around possible bugs in GDAL */
51+
static GDALDatasetH gdalOpen( const char *pszFilename, GDALAccess eAccess );
4952
protected:
5053

5154
QGis::DataType dataTypeFromGdal( int theGdalDataType ) const;

0 commit comments

Comments
 (0)