Skip to content
Permalink
Browse files

Implement default renderer creation in raster renderer registry

  • Loading branch information
mhugent committed Jun 22, 2012
1 parent e816b37 commit d3f870270290de8be97cf9c8b618405003244fec
@@ -594,6 +594,8 @@ QString QgsRasterLayer::contrastEnhancementAlgorithmAsString() const

void QgsRasterLayer::setRendererForDrawingStyle( const DrawingStyle & theDrawingStyle )
{
setRenderer( QgsRasterRendererRegistry::instance()->defaultRendererForDrawingStyle( theDrawingStyle, mDataProvider ) );
#if 0
QgsRasterRenderer* renderer = 0;

switch ( theDrawingStyle )
@@ -787,6 +789,7 @@ void QgsRasterLayer::setRendererForDrawingStyle( const DrawingStyle & theDrawin
}
renderer->setInvertColor( mInvertColor );
setRenderer( renderer );
#endif //0
}

/**
@@ -2387,7 +2390,7 @@ void QgsRasterLayer::setDataProvider( QString const & provider )
//Try to read the default contrast enhancement from the config file

QSettings myQSettings;
setContrastEnhancementAlgorithm( myQSettings.value( "/Raster/defaultContrastEnhancementAlgorithm", "NoEnhancement" ).toString() );
//setContrastEnhancementAlgorithm( myQSettings.value( "/Raster/defaultContrastEnhancementAlgorithm", "NoEnhancement" ).toString() );

//decide what type of layer this is...
//TODO Change this to look at the color interp and palette interp to decide which type of layer it is
@@ -21,6 +21,7 @@
#include "qgssinglebandcolordatarenderer.h"
#include "qgssinglebandgrayrenderer.h"
#include "qgssinglebandpseudocolorrenderer.h"
#include <QSettings>

QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry( const QString& theName, const QString& theVisibleName,
QgsRasterRendererCreateFunc rendererFunction,
@@ -104,4 +105,235 @@ QList< QgsRasterRendererRegistryEntry > QgsRasterRendererRegistry::entries() con
return result;
}

QgsRasterRenderer* QgsRasterRendererRegistry::defaultRendererForDrawingStyle( const QgsRasterLayer::DrawingStyle& theDrawingStyle, QgsRasterDataProvider* provider ) const
{
if ( !provider || provider->bandCount() < 1 )
{
return 0;
}


QgsRasterRenderer* renderer = 0;
switch ( theDrawingStyle )
{
case QgsRasterLayer::PalettedColor:
{
int grayBand = 1; //reasonable default
QList<QgsColorRampShader::ColorRampItem> colorEntries = provider->colorTable( grayBand );

//go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
int colorArraySize = 0;
QList<QgsColorRampShader::ColorRampItem>::const_iterator colorIt = colorEntries.constBegin();
for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
{
if ( colorIt->value > colorArraySize )
{
colorArraySize = ( int )( colorIt->value );
}
}

colorArraySize += 1; //usually starts at 0
QColor* colorArray = new QColor[ colorArraySize ];
colorIt = colorEntries.constBegin();
for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
{
colorArray[( int )( colorIt->value )] = colorIt->color;
}

renderer = new QgsPalettedRasterRenderer( provider,
grayBand,
colorArray,
colorArraySize );
}
break;
case QgsRasterLayer::MultiBandSingleBandGray:
case QgsRasterLayer::SingleBandGray:
{
int grayBand = 1;
renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
QgsContrastEnhancement* ce = new QgsContrastEnhancement(( QgsContrastEnhancement::QgsRasterDataType )(
provider->dataType( grayBand ) ) );

QSettings s;
QgsContrastEnhancement::ContrastEnhancementAlgorithm ceAlgorithm;
ceAlgorithm = ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )QgsRasterRendererRegistry::contrastEnhancementFromString(
s.value( "/Raster/defaultContrastEnhancementAlgorithm", "NoEnhancement" ).toString() );
ce->setContrastEnhancementAlgorithm( ceAlgorithm );

if ( ceAlgorithm != QgsContrastEnhancement::NoEnhancement )
{
double minValue = 0;
double maxValue = 0;
minMaxValuesForBand( grayBand, provider, minValue, maxValue );
ce->setMinimumValue( minValue );
ce->setMaximumValue( maxValue );
}
(( QgsSingleBandGrayRenderer* )renderer )->setContrastEnhancement( ce );
break;
}
case QgsRasterLayer::SingleBandPseudoColor:
{
int bandNo = 1;
double minValue = 0;
double maxValue = 0;
minMaxValuesForBand( bandNo, provider, minValue, maxValue );
QgsRasterShader* shader = new QgsRasterShader( minValue, maxValue );
renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
break;
}
case QgsRasterLayer::MultiBandColor:
{
QSettings s;
QgsContrastEnhancement::ContrastEnhancementAlgorithm ceAlgorithm;
ceAlgorithm = ( QgsContrastEnhancement::ContrastEnhancementAlgorithm )QgsRasterRendererRegistry::contrastEnhancementFromString(
s.value( "/Raster/defaultContrastEnhancementAlgorithm", "NoEnhancement" ).toString() );

QgsContrastEnhancement* redEnhancement = 0;
QgsContrastEnhancement* greenEnhancement = 0;
QgsContrastEnhancement* blueEnhancement = 0;

int redBand = s.value( "/Raster/defaultRedBand", 1 ).toInt();
if ( redBand < 0 || redBand > provider->bandCount() )
{
redBand = -1;
}
int greenBand = s.value( "/Raster/defaultGreenBand", 2 ).toInt();
if ( greenBand < 0 || greenBand > provider->bandCount() )
{
greenBand = -1;
}
int blueBand = s.value( "/Raster/defaultBlueBand", 3 ).toInt();
if ( blueBand < 0 || blueBand > provider->bandCount() )
{
blueBand = -1;
}

double minValue = 0;
double maxValue = 0;
if ( ceAlgorithm != QgsContrastEnhancement::NoEnhancement )
{
if ( redBand > 0 )
{
redEnhancement = new QgsContrastEnhancement(( QgsContrastEnhancement::QgsRasterDataType )(
provider->dataType( redBand ) ) );
minMaxValuesForBand( redBand, provider, minValue, maxValue );
redEnhancement->setMinimumValue( minValue );
redEnhancement->setMaximumValue( maxValue );
redEnhancement->setContrastEnhancementAlgorithm( ceAlgorithm );
}

if ( greenBand > 0 )
{
greenEnhancement = new QgsContrastEnhancement(( QgsContrastEnhancement::QgsRasterDataType )(
provider->dataType( greenBand ) ) );
minMaxValuesForBand( greenBand, provider, minValue, maxValue );
greenEnhancement->setMinimumValue( minValue );
greenEnhancement->setMaximumValue( maxValue );
greenEnhancement->setContrastEnhancementAlgorithm( ceAlgorithm );
}

if ( blueBand > 0 )
{
blueEnhancement = new QgsContrastEnhancement(( QgsContrastEnhancement::QgsRasterDataType )(
provider->dataType( blueBand ) ) );
minMaxValuesForBand( blueBand, provider, minValue, maxValue );
blueEnhancement->setMinimumValue( minValue );
blueEnhancement->setMaximumValue( maxValue );
blueEnhancement->setContrastEnhancementAlgorithm( ceAlgorithm );
}
}

renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand,
redEnhancement, greenEnhancement, blueEnhancement );
break;
}
case QgsRasterLayer::SingleBandColorDataStyle:
{
renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
break;
}
default:
break;
}

#if 0
if ( !renderer )
{
return;
}

renderer->setOpacity( mTransparencyLevel / 255.0 );

QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
if ( mDataProvider->bandCount() == 1 )
{
tr->setTransparentSingleValuePixelList( mRasterTransparency.transparentSingleValuePixelList() );
}
else if ( mDataProvider->bandCount() == 3 )
{
tr->setTransparentThreeValuePixelList( mRasterTransparency.transparentThreeValuePixelList() );
}
renderer->setRasterTransparency( tr );

if ( mTransparencyBandName != TRSTRING_NOT_SET )
{
int tBand = bandNumber( mTransparencyBandName );
if ( tBand > 0 )
{
renderer->setAlphaBand( tBand );
}
}
renderer->setInvertColor( mInvertColor );
#endif //0
return renderer;
}

bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider* provider, double& minValue, double& maxValue ) const
{
if ( !provider )
{
return false;
}


minValue = 0;
maxValue = 0;

QSettings s;
if ( s.value( "/Raster/useStandardDeviation", false ).toBool() )
{
double stdDevFactor = s.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble();
QgsRasterBandStats stats = provider->bandStatistics( band );
double diff = stdDevFactor * stats.stdDev;
minValue = stats.mean - diff;
maxValue = stats.mean + diff;
}
else
{
minValue = provider->minimumValue( band );
maxValue = provider->maximumValue( band );
}
return true;
}

int QgsRasterRendererRegistry::contrastEnhancementFromString( const QString& contrastEnhancementString )
{
if ( contrastEnhancementString == "StretchToMinimumMaximum" )
{
return ( int )QgsContrastEnhancement::StretchToMinimumMaximum;
}
else if ( contrastEnhancementString == "StretchAndClipToMinimumMaximum" )
{
return ( int )QgsContrastEnhancement::StretchAndClipToMinimumMaximum;
}
else if ( contrastEnhancementString == "ClipToMinimumMaximum" )
{
return ( int )QgsContrastEnhancement::ClipToMinimumMaximum;
}
else
{
return ( int )QgsContrastEnhancement::NoEnhancement;
}
}


@@ -18,6 +18,7 @@
#ifndef QGSRASTERRENDERERREGISTRY_H
#define QGSRASTERRENDERERREGISTRY_H

#include "qgsrasterlayer.h" //for DrawingStyle enum
#include <QHash>
#include <QString>

@@ -53,12 +54,20 @@ class CORE_EXPORT QgsRasterRendererRegistry
QStringList renderersList() const;
QList< QgsRasterRendererRegistryEntry > entries() const;

/**Creates a default renderer for a raster drawing style (considering user options such as default contrast enhancement).
Caller takes ownership*/
QgsRasterRenderer* defaultRendererForDrawingStyle( const QgsRasterLayer::DrawingStyle& theDrawingStyle, QgsRasterDataProvider* provider ) const;

protected:
QgsRasterRendererRegistry();

private:
static QgsRasterRendererRegistry* mInstance;
QHash< QString, QgsRasterRendererRegistryEntry > mEntries;

//read min/max values from
bool minMaxValuesForBand( int band, QgsRasterDataProvider* provider, double& minValue, double& maxValue ) const;
static int contrastEnhancementFromString( const QString& contrastEnhancementString );
};

#endif // QGSRASTERRENDERERREGISTRY_H

0 comments on commit d3f8702

Please sign in to comment.
You can’t perform that action at this time.