Showing with 18 additions and 0 deletions.
  1. +2 −0 src/core/qgsrasterdataprovider.cpp
  2. +16 −0 src/core/raster/qgsrasterlayer.cpp
2 changes: 2 additions & 0 deletions src/core/qgsrasterdataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo )
double myNoDataValue = noDataValue();
QgsRasterBandStats myRasterBandStats;
myRasterBandStats.elementCount = 0; // because we'll be counting only VALID pixels later
myRasterBandStats.bandName = generateBandName( theBandNo );
myRasterBandStats.bandNumber = theBandNo;

int myDataType = dataType( theBandNo );

Expand Down
16 changes: 16 additions & 0 deletions src/core/raster/qgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,14 @@ void QgsRasterLayer::setDataProvider( QString const & provider,
mRasterType = GrayOrUndefined;
}

// Set min/max values for single band if we have them ready (no need to calculate which is slow)
// don't set min/max on multiband even if available because it would cause stretch of bands and thus colors distortion
if ( mDataProvider->bandCount() == 1 && ( mDataProvider->capabilities() & QgsRasterDataProvider::ExactMinimumMaximum ) )
{
setMinimumValue( 1, mDataProvider->minimumValue( 1 ) );
setMaximumValue( 1, mDataProvider->maximumValue( 1 ) );
}

QgsDebugMsg( "mRasterType = " + QString::number( mRasterType ) );
if ( mRasterType == ColorLayer )
{
Expand Down Expand Up @@ -2426,6 +2434,14 @@ void QgsRasterLayer::setDataProvider( QString const & provider,
mDrawingStyle = SingleBandGray; //sensible default
mGrayBandName = bandName( 1 );

// If we have min/max available (without calculation), it is better to use StretchToMinimumMaximum
// TODO: in GUI there is 'Contrast enhancement - Default' which is overwritten here
// and that is confusing
if ( mDataProvider->capabilities() & QgsRasterDataProvider::ExactMinimumMaximum )
{
setContrastEnhancementAlgorithm( QgsContrastEnhancement::StretchToMinimumMaximum );
}

// read standard deviations
if ( mContrastEnhancementAlgorithm == QgsContrastEnhancement::StretchToMinimumMaximum )
{
Expand Down