Merge pull request #8438 from wonder-sk/fix-block-outside-raster-extent

Fix raster block output when block extent is outside of valid area
This commit is contained in:
Martin Dobias 2018-11-08 11:56:10 +01:00 committed by GitHub
commit ad1bba2ac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -640,6 +640,13 @@ QgsRasterBlock *QgsGdalProvider::block( int bandNo, const QgsRectangle &extent,
return block;
}
if ( !mExtent.intersects( extent ) )
{
// the requested extent is completely outside of the raster's extent - nothing to do
block->setIsNoData();
return block;
}
if ( !mExtent.contains( extent ) )
{
QRect subRect = QgsRasterBlock::subRect( extent, width, height, mExtent );

View File

@ -48,6 +48,7 @@ class TestQgsGdalProvider : public QObject
void scaleDataType(); //test resultant data types for int raster with float scale (#11573)
void warpedVrt(); //test loading raster which requires a warped vrt
void noData();
void noDataOutsideExtent();
void invalidNoDataInSourceIgnored();
void isRepresentableValue();
void mask();
@ -151,6 +152,30 @@ void TestQgsGdalProvider::noData()
delete provider;
}
void TestQgsGdalProvider::noDataOutsideExtent()
{
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/band1_byte_ct_epsg4326.tif";
QgsDataProvider *provider = QgsProviderRegistry::instance()->createProvider( QStringLiteral( "gdal" ), raster, QgsDataProvider::ProviderOptions() );
QVERIFY( provider->isValid() );
QgsRasterDataProvider *rp = dynamic_cast< QgsRasterDataProvider * >( provider );
QVERIFY( rp );
if ( rp )
{
std::unique_ptr<QgsRasterBlock> block( rp->block( 1, QgsRectangle( 10, 10, 12, 12 ), 16, 16 ) );
QVERIFY( block );
QCOMPARE( block->width(), 16 );
QCOMPARE( block->height(), 16 );
for ( int y = 0; y < 16; ++y )
{
for ( int x = 0; x < 16; ++x )
{
QVERIFY( block->isNoData( y, x ) );
}
}
}
delete provider;
}
void TestQgsGdalProvider::invalidNoDataInSourceIgnored()
{
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/byte_with_nan_nodata.tif";