From 13a43de935fe222effcf35d7fca9b04463e5c64e Mon Sep 17 00:00:00 2001 From: Clemens Raffler Date: Tue, 17 Nov 2020 22:10:49 +0100 Subject: [PATCH] Fix Cell Statistics NoData handling Fixes #40092 --- .../processing/qgsalgorithmcellstatistics.cpp | 7 ++++++- tests/src/analysis/testqgsprocessingalgs.cpp | 11 +++++++++++ .../cellstatistics_sum_result_ignoreNoData.tif | Bin 0 -> 388 bytes tests/testdata/raster/statisticsRas4_float64.asc | 10 ++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/testdata/control_images/expected_cellStatistics/cellstatistics_sum_result_ignoreNoData.tif create mode 100644 tests/testdata/raster/statisticsRas4_float64.asc diff --git a/src/analysis/processing/qgsalgorithmcellstatistics.cpp b/src/analysis/processing/qgsalgorithmcellstatistics.cpp index ef375760870..ab3eac663fa 100644 --- a/src/analysis/processing/qgsalgorithmcellstatistics.cpp +++ b/src/analysis/processing/qgsalgorithmcellstatistics.cpp @@ -277,7 +277,7 @@ QVariantMap QgsCellStatisticsAlgorithm::processAlgorithm( const QVariantMap &par outputBlock->setValue( row, col, mNoDataValue ); } } - else if ( !noDataInStack || mIgnoreNoData ) + else if ( !noDataInStack || ( mIgnoreNoData && cellValueStackSize > 0 ) ) { switch ( method ) { @@ -320,6 +320,11 @@ QVariantMap QgsCellStatisticsAlgorithm::processAlgorithm( const QVariantMap &par } outputBlock->setValue( row, col, result ); } + else + { + //result is NoData if cellValueStack contains no valid values, eg. all cellValues are NoData + outputBlock->setValue( row, col, mNoDataValue ); + } } } provider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop ); diff --git a/tests/src/analysis/testqgsprocessingalgs.cpp b/tests/src/analysis/testqgsprocessingalgs.cpp index 81b24a25c1e..f1c077c4d3d 100644 --- a/tests/src/analysis/testqgsprocessingalgs.cpp +++ b/tests/src/analysis/testqgsprocessingalgs.cpp @@ -2360,6 +2360,17 @@ void TestQgsProcessingAlgs::cellStatistics_data() << QStringLiteral( "/cellstatistics_median_result_fourLayers_float32.tif" ) << Qgis::Float32; + /* + * Testcase 19: sum with raster cell stacks containing only nodata + */ + QTest::newRow( "testcase_19" ) + << QStringList( {"/raster/statisticsRas1_float64.asc", "/raster/statisticsRas1_float64.asc"} ) + << QStringLiteral( "/raster/statisticsRas3_int32.tif" ) + << 0 + << true + << QStringLiteral( "/cellstatistics_sum_result_ignoreNoData.tif" ) + << Qgis::Float64; + } void TestQgsProcessingAlgs::cellStatistics() diff --git a/tests/testdata/control_images/expected_cellStatistics/cellstatistics_sum_result_ignoreNoData.tif b/tests/testdata/control_images/expected_cellStatistics/cellstatistics_sum_result_ignoreNoData.tif new file mode 100644 index 0000000000000000000000000000000000000000..af010b8b9f44571c001637920b4f6ce0d1d897f0 GIT binary patch literal 388 zcmebD)MDUZU|`^5U|?isU<9&QfS3`9%>-pT0L7W1Y>+xOB(@+U3q&taLKI0{49W(X zDGpWB0A$M`sR@R%nStVb%{&YsdK(ZoweT>o0qJu<+_;1Z%>D;tBftlH2pdKtWbm^D s9ANUgmOudGz(ip*OdL!%v>!eI6>xyiFjHW(0#qET3QpmahpBe}07#P&QUCw| literal 0 HcmV?d00001 diff --git a/tests/testdata/raster/statisticsRas4_float64.asc b/tests/testdata/raster/statisticsRas4_float64.asc new file mode 100644 index 00000000000..ccbdabb9ba3 --- /dev/null +++ b/tests/testdata/raster/statisticsRas4_float64.asc @@ -0,0 +1,10 @@ +ncols 4 +nrows 4 +xllcorner 0 +yllcorner 0 +cellsize 1 +nodata_value -9999.0 + 1.0 1.0 0.0 0.0 + -9999.0 1.0 2.0 2.0 + 4.0 0.0 0.0 2.0 + 4.0 0.0 1.0 1.0