Skip to content
Permalink
Browse files

QgsGdalProvider::buildPyramids(): do not crash if an invalid option i…

…s provided
  • Loading branch information
rouault committed May 27, 2016
1 parent a8be64e commit 2861cf1dc3e6daedea28d346a61e8a253343afce
Showing with 50 additions and 7 deletions.
  1. +14 −7 src/providers/gdal/qgsgdalprovider.cpp
  2. +36 −0 tests/src/core/testqgsrasterlayer.cpp
@@ -1598,13 +1598,20 @@ QString QgsGdalProvider::buildPyramids( const QList<QgsRasterPyramid> & theRaste
Q_FOREACH ( const QString& option, theConfigOptions )
{
QStringList opt = option.split( '=' );
QByteArray key = opt[0].toLocal8Bit();
QByteArray value = opt[1].toLocal8Bit();
// save previous value
myConfigOptionsOld[ opt[0] ] = QString( CPLGetConfigOption( key.data(), nullptr ) );
// set temp. value
CPLSetConfigOption( key.data(), value.data() );
QgsDebugMsg( QString( "set option %1=%2" ).arg( key.data(), value.data() ) );
if ( opt.size() == 2 )
{
QByteArray key = opt[0].toLocal8Bit();
QByteArray value = opt[1].toLocal8Bit();
// save previous value
myConfigOptionsOld[ opt[0] ] = QString( CPLGetConfigOption( key.data(), nullptr ) );
// set temp. value
CPLSetConfigOption( key.data(), value.data() );
QgsDebugMsg( QString( "set option %1=%2" ).arg( key.data(), value.data() ) );
}
else
{
QgsDebugMsg( QString( "invalid pyramid option: %1" ).arg( option ) );
}
}
}

@@ -24,6 +24,7 @@
#include <QDesktopServices>

#include "cpl_conv.h"
#include "gdal.h"

//qgis includes...
#include <qgsrasterlayer.h>
@@ -512,6 +513,7 @@ void TestQgsRasterLayer::buildExternalOverviews()
QString myResult =
mypLayer->dataProvider()->buildPyramids( myPyramidList, "NEAREST", myFormatFlag );
qDebug( "%s", myResult.toLocal8Bit().constData() );
QVERIFY( myResult.isEmpty() );
//
// Lets verify we have pyramids now...
//
@@ -526,8 +528,42 @@ void TestQgsRasterLayer::buildExternalOverviews()
// And that they were indeed in an external file...
//
QVERIFY( QFile::exists( myTempPath + "landsat.tif.ovr" ) );

//cleanup
delete mypLayer;

QFile::remove( myTempPath + "landsat.tif.ovr" );
mypLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
myRasterFileInfo.completeBaseName() );
myPyramidList = mypLayer->dataProvider()->buildPyramidList();
for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
{
//mark to be pyramided
myPyramidList[myCounterInt].build = true;
}

// Test with options
QStringList optionList;
optionList << "COMPRESS_OVERVIEW=DEFLATE";
optionList << "invalid";

myResult =
mypLayer->dataProvider()->buildPyramids( myPyramidList, "NEAREST", myFormatFlag, optionList );
qDebug( "%s", myResult.toLocal8Bit().constData() );
QVERIFY( myResult.isEmpty() );
QVERIFY( QFile::exists( myTempPath + "landsat.tif.ovr" ) );

//cleanup
delete mypLayer;

// Check that the overview is Deflate compressed
QString ovrFilename( myTempPath + "landsat.tif.ovr" );
GDALDatasetH hDS = GDALOpen( ovrFilename.toLocal8Bit().constData(), GA_ReadOnly );
QVERIFY( hDS );
const char* pszCompression = GDALGetMetadataItem( hDS, "COMPRESSION", "IMAGE_STRUCTURE" );
QVERIFY( pszCompression && EQUAL( pszCompression, "DEFLATE" ) );
GDALClose( hDS );

mReport += "<h2>Check Overviews</h2>\n";
mReport += "<p>Passed</p>";
}

0 comments on commit 2861cf1

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