From e0d38ba3f912b7ae6b34e66a44e529ec37394458 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sat, 28 May 2016 10:52:36 +0200 Subject: [PATCH] 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. --- src/core/raster/qgsrasterchecker.cpp | 11 ++++++++--- src/core/raster/qgsrasterfilewriter.cpp | 7 +++++-- tests/testdata/raster/byte.tif | Bin 0 -> 736 bytes 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 tests/testdata/raster/byte.tif diff --git a/src/core/raster/qgsrasterchecker.cpp b/src/core/raster/qgsrasterchecker.cpp index 35d0541fa08..47d6a37c23e 100644 --- a/src/core/raster/qgsrasterchecker.cpp +++ b/src/core/raster/qgsrasterchecker.cpp @@ -95,8 +95,13 @@ bool QgsRasterChecker::runTest( const QString& theVerifiedKey, QString theVerifi compare( "Source data type", verifiedProvider->srcDataType( band ), expectedProvider->srcDataType( band ), mReport, typesOk ); compare( "Data type", verifiedProvider->dataType( band ), expectedProvider->dataType( band ), mReport, typesOk ); - // TODO: not yet sure if noDataValue() should exist at all - //compare( "No data (NULL) value", verifiedProvider->noDataValue( band ), expectedProvider->noDataValue( band ), mReport, typesOk ); + // Check nodata + bool noDataOk = true; + compare( "No data (NULL) value existence flag", verifiedProvider->srcHasNoDataValue( band ), expectedProvider->srcHasNoDataValue( band ), mReport, noDataOk ); + if ( verifiedProvider->srcHasNoDataValue( band ) && expectedProvider->srcHasNoDataValue( band ) ) + { + compare( "No data (NULL) value", verifiedProvider->srcNoDataValue( band ), expectedProvider->srcNoDataValue( band ), mReport, noDataOk ); + } bool statsOk = true; QgsRasterBandStats verifiedStats = verifiedProvider->bandStatistics( band ); @@ -122,7 +127,7 @@ bool QgsRasterChecker::runTest( const QString& theVerifiedKey, QString theVerifi mReport += ""; mReport += "
"; - if ( !statsOk || !typesOk ) + if ( !statsOk || !typesOk || !noDataOk ) { allOk = false; // create values table anyway so that values are available diff --git a/src/core/raster/qgsrasterfilewriter.cpp b/src/core/raster/qgsrasterfilewriter.cpp index 282fef481e7..9ea1990ba9a 100644 --- a/src/core/raster/qgsrasterfilewriter.cpp +++ b/src/core/raster/qgsrasterfilewriter.cpp @@ -342,7 +342,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( { iter->startRasterRead( i, nCols, nRows, outputExtent ); blockList.push_back( nullptr ); - if ( destProvider ) // no tiles + if ( destProvider && destHasNoDataValueList.value( i - 1 ) ) // no tiles { destProvider->setNoDataValue( i, destNoDataValueList.value( i - 1 ) ); } @@ -436,7 +436,10 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( //write data to output file. todo: loop over the data list for ( int i = 1; i <= nBands; ++i ) { - partDestProvider->setNoDataValue( i, destNoDataValueList.value( i - 1 ) ); + if ( destHasNoDataValueList.value( i - 1 ) ) + { + partDestProvider->setNoDataValue( i, destNoDataValueList.value( i - 1 ) ); + } partDestProvider->write( destBlockList[i - 1]->bits( 0 ), i, iterCols, iterRows, 0, 0 ); delete destBlockList[i - 1]; addToVRT( partFileName( fileIndex ), i, iterCols, iterRows, iterLeft, iterTop ); diff --git a/tests/testdata/raster/byte.tif b/tests/testdata/raster/byte.tif new file mode 100644 index 0000000000000000000000000000000000000000..6fddac1286241fd62383c20965a11c38f021d7bc GIT binary patch literal 736 zcmZuuJ&P1U5bfDr4}(P!ygQK#7K}8JGcmmblM`s4NH1($H)vMP%CnxvPmnJ1v6Ein6#x_x;;c%h zs8JDc6d4y&@I+WRn<|=Buvsz-idI5QL^Nxs#%x8`wdO1tePV^8<$}*^b~mUC=rN=! z7{PLA8RSW+C-^ZLEUFqI7n9w|2_p?2P*mI@R(R!%f>)$;a~u|><;qwwt!9b}@wzgk z$hmrsdlS@+90ovxH})3tE$rehVT`QHMIj>?P823vm34LR#9ERTIVj+9N)HEctB+yW zi*+zufB(5KcFopx8BfXBeP41ZrMGzTSVDX~rDmG&W718WW`rkOd=>fW76)%OJrrVa} zmuq>n!T;iA<$vht*X`rT|NQo3h=H5o23Jy=Z7}Q(xykHowHt(ae7}4>8{*wN%i~86 X9xm=LUOYctynpxh)#C7Q`62xUqeR@! literal 0 HcmV?d00001