Skip to content

Commit

Permalink
Workaround for GDAL ticket 5170, fixes #8356
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Jul 27, 2013
1 parent 763d505 commit 299d152
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -139,7 +139,7 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri, bool update )
QString gdalUri = dataSourceUri();

CPLErrorReset();
mGdalBaseDataset = GDALOpen( TO8F( gdalUri ), mUpdate ? GA_Update : GA_ReadOnly );
mGdalBaseDataset = gdalOpen( TO8F( gdalUri ), mUpdate ? GA_Update : GA_ReadOnly );

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

mGdalBaseDataset = GDALOpen( TO8F( dataSourceUri() ), GA_Update );
mGdalBaseDataset = gdalOpen( TO8F( dataSourceUri() ), GA_Update );

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

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

//open the file using gdal making sure we have handled locale properly
//myDataset = GDALOpen( QFile::encodeName( theFileNameQString ).constData(), GA_ReadOnly );
myDataset = GDALOpen( TO8F( fileName ), GA_ReadOnly );
myDataset = QgsGdalProviderBase::gdalOpen( TO8F( fileName ), GA_ReadOnly );
if ( !myDataset )
{
if ( CPLGetLastErrorNo() != CPLE_OpenFailed )
Expand Down
21 changes: 21 additions & 0 deletions src/providers/gdal/qgsgdalproviderbase.cpp
Expand Up @@ -15,6 +15,9 @@
* *
***************************************************************************/

#define CPL_SUPRESS_CPLUSPLUS
#include "cpl_conv.h"

#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsgdalproviderbase.h"
Expand Down Expand Up @@ -273,3 +276,21 @@ QgsRectangle QgsGdalProviderBase::extent( GDALDatasetH gdalDataset )const
QgsRectangle extent( myGeoTransform[0], myYMin, myXMax, myGeoTransform[3] );
return extent;
}

GDALDatasetH QgsGdalProviderBase::gdalOpen( const char *pszFilename, GDALAccess eAccess )
{
// See http://hub.qgis.org/issues/8356 and http://trac.osgeo.org/gdal/ticket/5170
#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
char* pszOldVal = CPLStrdup( CPLGetConfigOption( "VSI_CACHE", "FALSE" ) );
CPLSetThreadLocalConfigOption( "VSI_CACHE", "FALSE" );
#endif

GDALDatasetH hDS = GDALOpen( pszFilename, eAccess );

#if GDAL_VERSION_MAJOR == 1 && ( (GDAL_VERSION_MINOR == 9 && GDAL_VERSION_REV <= 2) || (GDAL_VERSION_MINOR == 10 && GDAL_VERSION_REV <= 0) )
CPLSetThreadLocalConfigOption( "VSI_CACHE", pszOldVal );
CPLFree( pszOldVal );
#endif

return hDS;
}
3 changes: 3 additions & 0 deletions src/providers/gdal/qgsgdalproviderbase.h
Expand Up @@ -46,6 +46,9 @@ class QgsGdalProviderBase

/** \brief ensures that GDAL drivers are registered, but only once */
static void registerGdalDrivers();

/** Wrapper function for GDALOpen to get around possible bugs in GDAL */
static GDALDatasetH gdalOpen( const char *pszFilename, GDALAccess eAccess );
protected:

QGis::DataType dataTypeFromGdal( int theGdalDataType ) const;
Expand Down

0 comments on commit 299d152

Please sign in to comment.