Skip to content

Commit

Permalink
Respect nodata values in ::sample() for gdal provider
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 4, 2018
1 parent 3463271 commit 8af1c3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -1138,6 +1138,13 @@ double QgsGdalProvider::sample( const QgsPointXY &point, int band, bool *ok, con
if ( err != CE_None )
return std::numeric_limits<double>::quiet_NaN();

if ( ( sourceHasNoDataValue( band ) && useSourceNoDataValue( band ) &&
( std::isnan( value ) || qgsDoubleNear( static_cast< double >( value ), sourceNoDataValue( band ) ) ) ) ||
( QgsRasterRange::contains( static_cast< double >( value ), userNoDataValues( band ) ) ) )
{
return std::numeric_limits<double>::quiet_NaN();
}

if ( ok )
*ok = true;

Expand Down
12 changes: 12 additions & 0 deletions tests/src/core/testqgsrasterlayer.cpp
Expand Up @@ -794,6 +794,18 @@ void TestQgsRasterLayer::sample()
QVERIFY( ok );
QCOMPARE( rl->dataProvider()->sample( QgsPointXY( 17.943731, 30.230791 ), 2 ), 139.0 );
QCOMPARE( rl->dataProvider()->sample( QgsPointXY( 17.943731, 30.230791 ), 3 ), 111.0 );

// src no data
rl->dataProvider()->setNoDataValue( 3, 111.0 );
ok = false;
QVERIFY( std::isnan( rl->dataProvider()->sample( QgsPointXY( 17.943731, 30.230791 ), 3, &ok ) ) );
QVERIFY( !ok );
rl->dataProvider()->setUseSourceNoDataValue( 3, false );
QCOMPARE( rl->dataProvider()->sample( QgsPointXY( 17.943731, 30.230791 ), 3 ), 111.0 );

rl->dataProvider()->setUserNoDataValue( 2, QgsRasterRangeList() << QgsRasterRange( 130, 140 ) );
QVERIFY( std::isnan( rl->dataProvider()->sample( QgsPointXY( 17.943731, 30.230791 ), 2, &ok ) ) );
QVERIFY( !ok );
}

QGSTEST_MAIN( TestQgsRasterLayer )
Expand Down

0 comments on commit 8af1c3f

Please sign in to comment.