Skip to content

Commit

Permalink
QgsGdalProvider::buildPyramids(): do not crash if an invalid option i…
Browse files Browse the repository at this point in the history
…s provided
  • Loading branch information
rouault committed May 27, 2016
1 parent a8be64e commit 2861cf1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -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 ) );
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions tests/src/core/testqgsrasterlayer.cpp
Expand Up @@ -24,6 +24,7 @@
#include <QDesktopServices>

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

//qgis includes...
#include <qgsrasterlayer.h>
Expand Down Expand Up @@ -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...
//
Expand All @@ -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>";
}
Expand Down

0 comments on commit 2861cf1

Please sign in to comment.