Skip to content
Permalink
Browse files

Respect nodata values in ::sample() for gdal provider

  • Loading branch information
nyalldawson committed Sep 4, 2018
1 parent 3463271 commit 8af1c3f9f0012ae660a9541165c4ee9aac0be1f3
Showing with 19 additions and 0 deletions.
  1. +7 −0 src/providers/gdal/qgsgdalprovider.cpp
  2. +12 −0 tests/src/core/testqgsrasterlayer.cpp
@@ -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;

@@ -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 )

0 comments on commit 8af1c3f

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