Skip to content

Commit e0d38ba

Browse files
committedMay 28, 2016
QgsRasterFileWriter::writeDataRaster(): do not set nodata on output when unneeded
Currently if the source raster has no nodata value, the writer will still write nan as the output nodata value, ignoring the flag that specifies if there is a nodata value or not. On a raster with byte data type, this will cause confusion on reading since the nodata value will be somehow cast as 0. Make QgsRasterChecker check for nodata consistency between source and target, and add a test file that shows the issue.
1 parent 3d95712 commit e0d38ba

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed
 

‎src/core/raster/qgsrasterchecker.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ bool QgsRasterChecker::runTest( const QString& theVerifiedKey, QString theVerifi
9595
compare( "Source data type", verifiedProvider->srcDataType( band ), expectedProvider->srcDataType( band ), mReport, typesOk );
9696
compare( "Data type", verifiedProvider->dataType( band ), expectedProvider->dataType( band ), mReport, typesOk );
9797

98-
// TODO: not yet sure if noDataValue() should exist at all
99-
//compare( "No data (NULL) value", verifiedProvider->noDataValue( band ), expectedProvider->noDataValue( band ), mReport, typesOk );
98+
// Check nodata
99+
bool noDataOk = true;
100+
compare( "No data (NULL) value existence flag", verifiedProvider->srcHasNoDataValue( band ), expectedProvider->srcHasNoDataValue( band ), mReport, noDataOk );
101+
if ( verifiedProvider->srcHasNoDataValue( band ) && expectedProvider->srcHasNoDataValue( band ) )
102+
{
103+
compare( "No data (NULL) value", verifiedProvider->srcNoDataValue( band ), expectedProvider->srcNoDataValue( band ), mReport, noDataOk );
104+
}
100105

101106
bool statsOk = true;
102107
QgsRasterBandStats verifiedStats = verifiedProvider->bandStatistics( band );
@@ -122,7 +127,7 @@ bool QgsRasterChecker::runTest( const QString& theVerifiedKey, QString theVerifi
122127
mReport += "</table>";
123128
mReport += "<br>";
124129

125-
if ( !statsOk || !typesOk )
130+
if ( !statsOk || !typesOk || !noDataOk )
126131
{
127132
allOk = false;
128133
// create values table anyway so that values are available

‎src/core/raster/qgsrasterfilewriter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster(
342342
{
343343
iter->startRasterRead( i, nCols, nRows, outputExtent );
344344
blockList.push_back( nullptr );
345-
if ( destProvider ) // no tiles
345+
if ( destProvider && destHasNoDataValueList.value( i - 1 ) ) // no tiles
346346
{
347347
destProvider->setNoDataValue( i, destNoDataValueList.value( i - 1 ) );
348348
}
@@ -436,7 +436,10 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster(
436436
//write data to output file. todo: loop over the data list
437437
for ( int i = 1; i <= nBands; ++i )
438438
{
439-
partDestProvider->setNoDataValue( i, destNoDataValueList.value( i - 1 ) );
439+
if ( destHasNoDataValueList.value( i - 1 ) )
440+
{
441+
partDestProvider->setNoDataValue( i, destNoDataValueList.value( i - 1 ) );
442+
}
440443
partDestProvider->write( destBlockList[i - 1]->bits( 0 ), i, iterCols, iterRows, 0, 0 );
441444
delete destBlockList[i - 1];
442445
addToVRT( partFileName( fileIndex ), i, iterCols, iterRows, iterLeft, iterTop );

‎tests/testdata/raster/byte.tif

736 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.