Skip to content
Permalink
Browse files

Merge pull request #41670 from elpaso/bugfix-gh41662-raster-byte-noda…

…ta-diagonal

Set min/max from data type when GDAL stats fail
  • Loading branch information
nyalldawson authored and github-actions committed Feb 19, 2021
1 parent c2bf502 commit 3822a2281e51f785314868639dc307dd421aeb48
Showing with 65 additions and 0 deletions.
  1. +4 −0 src/core/providers/gdal/qgsgdalprovider.cpp
  2. +61 −0 src/core/raster/qgsrasterlayer.cpp
@@ -2863,6 +2863,10 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int bandNo, int stats, const
QgsDebugMsgLevel( QStringLiteral( "STDDEV %1" ).arg( myRasterBandStats.stdDev ), 2 );
#endif
}
else
{
myRasterBandStats.statsGathered = QgsRasterBandStats::Stats::None;
}

mStatistics.append( myRasterBandStats );
return myRasterBandStats;
@@ -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, None means a failure
if ( myRasterBandStats.statsGathered == QgsRasterBandStats::Stats::None )
{
// 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 3822a22

Please sign in to comment.