Skip to content
Permalink
Browse files

Set min/max from data type when GDAL stats fail

Fixes #41662

(cherry picked from commit 30b2166)
  • Loading branch information
elpaso authored and nyalldawson committed Mar 19, 2021
1 parent ef1fcb2 commit 03d20e01464a3a33ed0166242618d39c6e244c4a
Showing with 61 additions and 0 deletions.
  1. +61 −0 src/core/raster/qgsrasterlayer.cpp
@@ -982,6 +982,67 @@ void QgsRasterLayer::computeMinMax( int band,
if ( limits == QgsRasterMinMaxOrigin::MinMax )
{
QgsRasterBandStats myRasterBandStats = mDataProvider->bandStatistics( band, QgsRasterBandStats::Min | QgsRasterBandStats::Max, extent, sampleSize );
// Check if statistics were actually gathered, 0 means a failure
if ( myRasterBandStats.elementCount == 0 )
{
// Best guess we can do
switch ( mDataProvider->dataType( band ) )
{
case Qgis::DataType::Byte:
{
myRasterBandStats.minimumValue = 0;
myRasterBandStats.maximumValue = 255;
break;
}
case Qgis::DataType::UInt16:
{
myRasterBandStats.minimumValue = 0;
myRasterBandStats.maximumValue = std::numeric_limits<uint16_t>::max();
break;
}
case Qgis::DataType::UInt32:
{
myRasterBandStats.minimumValue = 0;
myRasterBandStats.maximumValue = std::numeric_limits<uint32_t>::max();
break;
}
case Qgis::DataType::Int16:
case Qgis::DataType::CInt16:
{
myRasterBandStats.minimumValue = std::numeric_limits<int16_t>::lowest();
myRasterBandStats.maximumValue = std::numeric_limits<int16_t>::max();
break;
}
case Qgis::DataType::Int32:
case Qgis::DataType::CInt32:
{
myRasterBandStats.minimumValue = std::numeric_limits<int32_t>::lowest();
myRasterBandStats.maximumValue = std::numeric_limits<int32_t>::max();
break;
}
case Qgis::DataType::Float32:
case Qgis::DataType::CFloat32:
{
myRasterBandStats.minimumValue = std::numeric_limits<float_t>::lowest();
myRasterBandStats.maximumValue = std::numeric_limits<float_t>::max();
break;
}
case Qgis::DataType::Float64:
case Qgis::DataType::CFloat64:
{
myRasterBandStats.minimumValue = std::numeric_limits<double_t>::lowest();
myRasterBandStats.maximumValue = std::numeric_limits<double_t>::max();
break;
}
case Qgis::DataType::ARGB32:
case Qgis::DataType::ARGB32_Premultiplied:
case Qgis::DataType::UnknownDataType:
{
// Nothing to guess
break;
}
}
}
min = myRasterBandStats.minimumValue;
max = myRasterBandStats.maximumValue;
}

0 comments on commit 03d20e0

Please sign in to comment.