Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix when GDALGetRasterScale returns 0 (gdal 2.3 affected for some dat…
…asets)
  • Loading branch information
PeterPetrik authored and nyalldawson committed Jan 31, 2019
1 parent c3819e8 commit 7a12f11
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -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;
Expand All @@ -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 )
Expand Down
12 changes: 12 additions & 0 deletions tests/src/providers/testqgsgdalprovider.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 added tests/testdata/raster/scale0ingdal23.tif
Binary file not shown.

0 comments on commit 7a12f11

Please sign in to comment.