Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix NoData handling in getCellValuesFromBlockStack()
  • Loading branch information
root676 authored and nyalldawson committed Jul 2, 2020
1 parent 4181149 commit 2d2ad2c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/analysis/processing/qgsalgorithmcellstatistics.cpp
Expand Up @@ -249,11 +249,11 @@ QVariantMap QgsCellStatisticsAlgorithm::processAlgorithm( const QVariantMap &par
for ( int col = 0; col < iterCols; col++ )
{
double result = 0;
bool hasNoData = false;
std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, hasNoData );
bool noDataInStack = false;
std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
int cellValueStackSize = cellValues.size();

if ( hasNoData && !mIgnoreNoData )
if ( noDataInStack && !mIgnoreNoData )
{
//output cell will always be NoData if NoData occurs in cellValueStack and NoData is not ignored
//this saves unnecessary iterations on the cellValueStack
Expand All @@ -264,7 +264,7 @@ QVariantMap QgsCellStatisticsAlgorithm::processAlgorithm( const QVariantMap &par
outputBlock->setValue( row, col, mNoDataValue );
}
}
else if ( !hasNoData || (hasNoData && mIgnoreNoData) )
else if ( !noDataInStack || (noDataInStack && mIgnoreNoData) )
{
switch ( statisticMethodIdx )
{
Expand Down
15 changes: 11 additions & 4 deletions src/analysis/processing/qgsrasteranalysisutils.cpp
Expand Up @@ -287,26 +287,33 @@ void QgsRasterAnalysisUtils::applyRasterLogicOperator( const std::vector< QgsRas
destinationRaster->setEditable( false );
}

std::vector<double> QgsRasterAnalysisUtils::getCellValuesFromBlockStack( const std::vector< std::unique_ptr< QgsRasterBlock > > &inputBlocks, int &row, int &col, bool &hasNoData )
std::vector<double> QgsRasterAnalysisUtils::getCellValuesFromBlockStack( const std::vector< std::unique_ptr< QgsRasterBlock > > &inputBlocks, int &row, int &col, bool &noDataInStack )
{
//get all values from inputBlocks
std::vector<double> cellValues;
bool hasNoData = false;
cellValues.reserve( inputBlocks.size() );

for ( auto &block : inputBlocks )
{
double value = 0;
if ( !block || !block->isValid() )
{
hasNoData = true;
noDataInStack = true;
break;
}
else
{
value = block->valueAndNoData( row, col, hasNoData );
if ( hasNoData )
break;
cellValues.push_back( value );
{
noDataInStack = true;
continue; //NoData is not included in the cell value vector
}
else
{
cellValues.push_back( value );
}
}
}
return cellValues;
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsrasteranalysisutils.h
Expand Up @@ -94,7 +94,7 @@ namespace QgsRasterAnalysisUtils
/**
* Returns a vector of double values obtained from a stack of input QgsRasterBlocks
*/
std::vector<double> getCellValuesFromBlockStack( const std::vector< std::unique_ptr< QgsRasterBlock > > &inputBlocks, int &row, int &col, bool &hasNoData );
std::vector<double> getCellValuesFromBlockStack( const std::vector< std::unique_ptr< QgsRasterBlock > > &inputBlocks, int &row, int &col, bool &noDataInStack );

/**
* Returns the arithmetic mean from a vector of cell values
Expand Down

0 comments on commit 2d2ad2c

Please sign in to comment.