Navigation Menu

Skip to content

Commit

Permalink
fix when GDALGetRasterScale returns 0 (gdal 2.3 affected for some dat…
Browse files Browse the repository at this point in the history
…asets)

(cherry picked from commit 7a12f11)
  • Loading branch information
PeterPetrik authored and nyalldawson committed Feb 1, 2019
1 parent 20cd215 commit 8edc570
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 ); GDALRasterBandH myGdalBand = getBand( bandNo );
int bGotScale; int bGotScale;
double myScale = GDALGetRasterScale( myGdalBand, &bGotScale ); double myScale = GDALGetRasterScale( myGdalBand, &bGotScale );
if ( bGotScale )
// if scale==0, ignore both scale and offset
if ( bGotScale && !qgsDoubleNear( myScale, 0.0 ) )
return myScale; return myScale;
else else
return 1.0; return 1.0;
Expand All @@ -1293,6 +1295,13 @@ double QgsGdalProvider::bandOffset( int bandNo ) const
return 0.0; return 0.0;


GDALRasterBandH myGdalBand = getBand( bandNo ); 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; int bGotOffset;
double myOffset = GDALGetRasterOffset( myGdalBand, &bGotOffset ); double myOffset = GDALGetRasterOffset( myGdalBand, &bGotOffset );
if ( 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 bandNameNoDescription(); // test band name for when no description or tags available (#16047)
void bandNameWithDescription(); // test band name for when description 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 interactionBetweenRasterChangeAndCache(); // test that updading a raster invalidates the GDAL dataset cache (#20104)
void scale0(); //test when data has scale 0 (#20493)


private: private:
QString mTestDataDir; QString mTestDataDir;
Expand Down Expand Up @@ -346,5 +347,16 @@ void TestQgsGdalProvider::interactionBetweenRasterChangeAndCache()
delete provider; 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 ) QGSTEST_MAIN( TestQgsGdalProvider )
#include "testqgsgdalprovider.moc" #include "testqgsgdalprovider.moc"
Binary file added tests/testdata/raster/scale0ingdal23.tif
Binary file not shown.

0 comments on commit 8edc570

Please sign in to comment.