Skip to content
Permalink
Browse files

fix when GDALGetRasterScale returns 0 (gdal 2.3 affected for some dat…

…asets)

(cherry picked from commit 7a12f11)
  • Loading branch information
PeterPetrik authored and nyalldawson committed Feb 1, 2019
1 parent 20cd215 commit 8edc570110d72d8a080586eb7e7882a18c849cb7
@@ -1280,7 +1280,9 @@ double QgsGdalProvider::bandScale( int bandNo ) const
GDALRasterBandH myGdalBand = getBand( bandNo );
int bGotScale;
double myScale = GDALGetRasterScale( myGdalBand, &bGotScale );
if ( bGotScale )

// if scale==0, ignore both scale and offset
if ( bGotScale && !qgsDoubleNear( myScale, 0.0 ) )
return myScale;
else
return 1.0;
@@ -1293,6 +1295,13 @@ double QgsGdalProvider::bandOffset( int bandNo ) const
return 0.0;

GDALRasterBandH myGdalBand = getBand( bandNo );

// if scale==0, ignore both scale and offset
int bGotScale;
double myScale = GDALGetRasterScale( myGdalBand, &bGotScale );
if ( bGotScale && qgsDoubleNear( myScale, 0.0 ) )
return 0.0;

int bGotOffset;
double myOffset = GDALGetRasterOffset( myGdalBand, &bGotOffset );
if ( bGotOffset )
@@ -56,6 +56,7 @@ class TestQgsGdalProvider : public QObject
void bandNameNoDescription(); // test band name for when no description or tags available (#16047)
void bandNameWithDescription(); // test band name for when description available (#16047)
void interactionBetweenRasterChangeAndCache(); // test that updading a raster invalidates the GDAL dataset cache (#20104)
void scale0(); //test when data has scale 0 (#20493)

private:
QString mTestDataDir;
@@ -346,5 +347,16 @@ void TestQgsGdalProvider::interactionBetweenRasterChangeAndCache()
delete provider;
}

void TestQgsGdalProvider::scale0()
{
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/scale0ingdal23.tif";
QgsDataProvider *provider = QgsProviderRegistry::instance()->createProvider( QStringLiteral( "gdal" ), raster, QgsDataProvider::ProviderOptions() );
QgsRasterDataProvider *rp = dynamic_cast< QgsRasterDataProvider * >( provider );
QVERIFY( rp );
QCOMPARE( rp->bandScale( 1 ), 1.0 );
QCOMPARE( rp->bandOffset( 1 ), 0.0 );
delete provider;
}

QGSTEST_MAIN( TestQgsGdalProvider )
#include "testqgsgdalprovider.moc"
Binary file not shown.

0 comments on commit 8edc570

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