Skip to content

Commit

Permalink
add support for correct output raster datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
root676 authored and nyalldawson committed Jul 2, 2020
1 parent 56a590e commit b2b8fef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/analysis/processing/qgsalgorithmcellstatistics.cpp
Expand Up @@ -71,7 +71,9 @@ QString QgsCellStatisticsAlgorithm::shortHelpString() const
" <li>Variety (count of unique values)</li>"
"</ul> "
"Input raster layers that do not match the cell size of the reference raster layer will be "
"resampled using nearest neighbor resampling.\n"
"resampled using nearest neighbor resampling. The output raster data type will be set to "
"the most complex data type present in the input datasets except when using the functions "
"Mean and Standard deviation (data type is always Float32) or Count and Variety (data type is always Int32).\n"
"<i>Calculation details - general:</i> NoData values in any of the input layers will result in a NoData cell output if the Ignore NoData parameter is not set.\n"
"<i>Calculation details - Count:</i> Count will always result in the number of cells without NoData values at the current cell location."
"<i>Calculation details - Median:</i> If the number of input layers is even, the median will be calculated as the "
Expand Down Expand Up @@ -167,6 +169,31 @@ QVariantMap QgsCellStatisticsAlgorithm::processAlgorithm( const QVariantMap &par
//obtain statistic method
int statisticMethodIdx = parameterAsInt( parameters, QStringLiteral( "STATISTIC" ), context );

//determine output raster data type
//initially raster data type to most primitive data type that is possible
mDataType = Qgis::Byte;
for ( const QgsRasterAnalysisUtils::RasterLogicInput &i : mInputs )
{
for ( int band : i.bands )
{
Qgis::DataType inputDataType = i.interface->dataType( band );
if( static_cast<int>(mDataType) < static_cast<int>(inputDataType) )
mDataType = inputDataType; //if raster data type is more potent, set it as new data type
}
}

//force data types on specific functions if input data types don't match
if( statisticMethodIdx == 2 || statisticMethodIdx == 4 ) //mean, stddev
{
if( static_cast<int>(mDataType) < 6 )
mDataType = Qgis::Float32; //force float on mean and stddev if all inputs are integer
}
else if ( statisticMethodIdx == 1 || statisticMethodIdx == 11 ) //count, variety
{
if( static_cast<int>(mDataType) > 5 ) //if is floating point type
mDataType = Qgis::Int32; //force integer on variety if all inputs are float or complex
}

const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmcellstatistics.h
Expand Up @@ -53,7 +53,7 @@ class QgsCellStatisticsAlgorithm : public QgsProcessingAlgorithm
private:
std::vector< QgsRasterAnalysisUtils::RasterLogicInput > mInputs;
bool mIgnoreNoData;
Qgis::DataType mDataType = Qgis::Float32;
Qgis::DataType mDataType;
double mNoDataValue = -9999;
int mLayerWidth;
int mLayerHeight;
Expand Down

0 comments on commit b2b8fef

Please sign in to comment.