Skip to content
Permalink
Browse files

Create color ramp legend nodes for pseudocolor or grayscale rasters or

point clouds using a Linear color ramp based renderer
  • Loading branch information
nyalldawson committed Dec 16, 2020
1 parent e4a91e7 commit d7ce1a316792473a2d7a8683044925f92a8db238
@@ -56,6 +56,8 @@ Takes ownership

virtual QList< QPair< QString, QColor > > legendSymbologyItems() const;

virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) /Factory/;


virtual QList<int> usesBands() const;

@@ -77,6 +77,8 @@ Creates a color ramp shader

virtual QList< QPair< QString, QColor > > legendSymbologyItems() const;

virtual QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) /Factory/;

virtual QList<int> usesBands() const;

virtual void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const;
@@ -21,6 +21,7 @@
#include "qgscolorramp.h"
#include "qgssymbollayerutils.h"
#include "qgslayertreemodellegendnode.h"
#include "qgscolorramplegendnode.h"

QgsPointCloudAttributeByRampRenderer::QgsPointCloudAttributeByRampRenderer()
{
@@ -167,16 +168,31 @@ QSet<QString> QgsPointCloudAttributeByRampRenderer::usedAttributes( const QgsPoi

QList<QgsLayerTreeModelLegendNode *> QgsPointCloudAttributeByRampRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
{
QList<QgsLayerTreeModelLegendNode *> nodes;

QList< QPair< QString, QColor > > items;
mColorRampShader.legendSymbologyItems( items );
for ( const QPair< QString, QColor > &item : qgis::as_const( items ) )
QList<QgsLayerTreeModelLegendNode *> res;
switch ( mColorRampShader.colorRampType() )
{
nodes << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
case QgsColorRampShader::Interpolated:
// for interpolated shaders we use a ramp legend node
res << new QgsColorRampLegendNode( nodeLayer, mColorRampShader.sourceColorRamp()->clone(),
QString::number( mColorRampShader.minimumValue() ),
QString::number( mColorRampShader.maximumValue() ) );
break;

case QgsColorRampShader::Discrete:
case QgsColorRampShader::Exact:
{
// for all others we use itemised lists
QList< QPair< QString, QColor > > items;
mColorRampShader.legendSymbologyItems( items );
res.reserve( items.size() );
for ( const QPair< QString, QColor > &item : qgis::as_const( items ) )
{
res << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
}
break;
}
}

return nodes;
return res;
}

QString QgsPointCloudAttributeByRampRenderer::attribute() const
@@ -18,6 +18,7 @@
#include "qgssinglebandgrayrenderer.h"
#include "qgscontrastenhancement.h"
#include "qgsrastertransparency.h"
#include "qgscolorramplegendnode.h"
#include <QDomDocument>
#include <QDomElement>
#include <QImage>
@@ -211,6 +212,20 @@ QList<QPair<QString, QColor> > QgsSingleBandGrayRenderer::legendSymbologyItems()
return symbolItems;
}

QList<QgsLayerTreeModelLegendNode *> QgsSingleBandGrayRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
{
QList<QgsLayerTreeModelLegendNode *> res;
if ( mContrastEnhancement && mContrastEnhancement->contrastEnhancementAlgorithm() != QgsContrastEnhancement::NoEnhancement )
{
const QColor minColor = ( mGradient == BlackToWhite ) ? Qt::black : Qt::white;
const QColor maxColor = ( mGradient == BlackToWhite ) ? Qt::white : Qt::black;
res << new QgsColorRampLegendNode( nodeLayer, new QgsGradientColorRamp( minColor, maxColor ),
QString::number( mContrastEnhancement->minimumValue() ),
QString::number( mContrastEnhancement->maximumValue() ) );
}
return res;
}

QList<int> QgsSingleBandGrayRenderer::usesBands() const
{
QList<int> bandList;
@@ -64,6 +64,7 @@ class CORE_EXPORT QgsSingleBandGrayRenderer: public QgsRasterRenderer
void writeXml( QDomDocument &doc, QDomElement &parentElem ) const override;

QList< QPair< QString, QColor > > legendSymbologyItems() const override;
QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) SIP_FACTORY override;

QList<int> usesBands() const override;

@@ -22,6 +22,7 @@
#include "qgsrastertransparency.h"
#include "qgsrasterviewport.h"
#include "qgsstyleentityvisitor.h"
#include "qgscolorramplegendnode.h"

#include <QDomDocument>
#include <QDomElement>
@@ -422,3 +423,35 @@ bool QgsSingleBandPseudoColorRenderer::accept( QgsStyleEntityVisitorInterface *v

return true;
}

QList<QgsLayerTreeModelLegendNode *> QgsSingleBandPseudoColorRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
{
const QgsColorRampShader *rampShader = dynamic_cast<const QgsColorRampShader *>( mShader->rasterShaderFunction() );
if ( !rampShader )
return QList<QgsLayerTreeModelLegendNode *>();

QList<QgsLayerTreeModelLegendNode *> res;
switch ( rampShader->colorRampType() )
{
case QgsColorRampShader::Interpolated:
// for interpolated shaders we use a ramp legend node
res << new QgsColorRampLegendNode( nodeLayer, rampShader->sourceColorRamp()->clone(),
QString::number( rampShader->minimumValue() ),
QString::number( rampShader->maximumValue() ) );
break;

case QgsColorRampShader::Discrete:
case QgsColorRampShader::Exact:
{
// for all others we use itemised lists
const QList< QPair< QString, QColor > > items = legendSymbologyItems();
res.reserve( items.size() );
for ( const QPair< QString, QColor > &item : items )
{
res << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
}
break;
}
}
return res;
}
@@ -78,6 +78,7 @@ class CORE_EXPORT QgsSingleBandPseudoColorRenderer: public QgsRasterRenderer

void writeXml( QDomDocument &doc, QDomElement &parentElem ) const override;
QList< QPair< QString, QColor > > legendSymbologyItems() const override;
QList<QgsLayerTreeModelLegendNode *> createLegendNodes( QgsLayerTreeLayer *nodeLayer ) SIP_FACTORY override;
QList<int> usesBands() const override;
void toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props = QgsStringMap() ) const override;
bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;

0 comments on commit d7ce1a3

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