Skip to content

Commit ad1bba2

Browse files
authoredNov 8, 2018
Merge pull request #8438 from wonder-sk/fix-block-outside-raster-extent
Fix raster block output when block extent is outside of valid area
2 parents 528c8a6 + 872c9a1 commit ad1bba2

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,13 @@ QgsRasterBlock *QgsGdalProvider::block( int bandNo, const QgsRectangle &extent,
640640
return block;
641641
}
642642

643+
if ( !mExtent.intersects( extent ) )
644+
{
645+
// the requested extent is completely outside of the raster's extent - nothing to do
646+
block->setIsNoData();
647+
return block;
648+
}
649+
643650
if ( !mExtent.contains( extent ) )
644651
{
645652
QRect subRect = QgsRasterBlock::subRect( extent, width, height, mExtent );

‎tests/src/providers/testqgsgdalprovider.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class TestQgsGdalProvider : public QObject
4848
void scaleDataType(); //test resultant data types for int raster with float scale (#11573)
4949
void warpedVrt(); //test loading raster which requires a warped vrt
5050
void noData();
51+
void noDataOutsideExtent();
5152
void invalidNoDataInSourceIgnored();
5253
void isRepresentableValue();
5354
void mask();
@@ -151,6 +152,30 @@ void TestQgsGdalProvider::noData()
151152
delete provider;
152153
}
153154

155+
void TestQgsGdalProvider::noDataOutsideExtent()
156+
{
157+
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/band1_byte_ct_epsg4326.tif";
158+
QgsDataProvider *provider = QgsProviderRegistry::instance()->createProvider( QStringLiteral( "gdal" ), raster, QgsDataProvider::ProviderOptions() );
159+
QVERIFY( provider->isValid() );
160+
QgsRasterDataProvider *rp = dynamic_cast< QgsRasterDataProvider * >( provider );
161+
QVERIFY( rp );
162+
if ( rp )
163+
{
164+
std::unique_ptr<QgsRasterBlock> block( rp->block( 1, QgsRectangle( 10, 10, 12, 12 ), 16, 16 ) );
165+
QVERIFY( block );
166+
QCOMPARE( block->width(), 16 );
167+
QCOMPARE( block->height(), 16 );
168+
for ( int y = 0; y < 16; ++y )
169+
{
170+
for ( int x = 0; x < 16; ++x )
171+
{
172+
QVERIFY( block->isNoData( y, x ) );
173+
}
174+
}
175+
}
176+
delete provider;
177+
}
178+
154179
void TestQgsGdalProvider::invalidNoDataInSourceIgnored()
155180
{
156181
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/byte_with_nan_nodata.tif";

0 commit comments

Comments
 (0)
Please sign in to comment.