QGIS API Documentation  2.99.0-Master (009e47e)
qgsrastershader.cpp
Go to the documentation of this file.
1 /* **************************************************************************
2  qgsrastershader.cpp - description
3  -------------------
4 begin : Fri Dec 28 2007
5 copyright : (C) 2007 by Peter J. Ersts
6 email : [email protected]
7 
8 ****************************************************************************/
9 
10 /* **************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "qgslogger.h"
20 #include "qgscolorrampshader.h"
21 #include "qgsrastershader.h"
22 #include "qgsrasterblock.h"
23 #include "qgssymbollayerutils.h"
24 
25 #include <QDomDocument>
26 #include <QDomElement>
27 
28 QgsRasterShader::QgsRasterShader( double minimumValue, double maximumValue )
29  : mMinimumValue( minimumValue )
30  , mMaximumValue( maximumValue )
31  , mRasterShaderFunction( new QgsRasterShaderFunction( mMinimumValue, mMaximumValue ) )
32 {
33  QgsDebugMsgLevel( "called.", 4 );
34 }
35 
46 bool QgsRasterShader::shade( double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha )
47 {
48  if ( mRasterShaderFunction )
49  {
50  return mRasterShaderFunction->shade( value, returnRedValue, returnGreenValue, returnBlueValue, returnAlpha );
51  }
52 
53  return false;
54 }
55 
70 bool QgsRasterShader::shade( double redValue, double greenValue, double blueValue, double alphaValue, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlphaValue )
71 {
72  if ( mRasterShaderFunction )
73  {
74  return mRasterShaderFunction->shade( redValue, greenValue, blueValue, alphaValue, returnRedValue, returnGreenValue, returnBlueValue, returnAlphaValue );
75  }
76 
77  return false;
78 }
79 
86 {
87  QgsDebugMsgLevel( "called.", 4 );
88 
89  if ( mRasterShaderFunction.get() == function )
90  return;
91 
92  if ( function )
93  {
94  mRasterShaderFunction.reset( function );
95  }
96 }
97 
104 {
105  QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
106 
107  mMaximumValue = value;
108  if ( mRasterShaderFunction )
109  {
110  mRasterShaderFunction->setMaximumValue( value );
111  }
112 }
113 
120 {
121  QgsDebugMsgLevel( "Value = " + QString::number( value ), 4 );
122 
123  mMinimumValue = value;
124  if ( mRasterShaderFunction )
125  {
126  mRasterShaderFunction->setMinimumValue( value );
127  }
128 }
129 
130 void QgsRasterShader::writeXml( QDomDocument &doc, QDomElement &parent ) const
131 {
132  if ( parent.isNull() || !mRasterShaderFunction )
133  {
134  return;
135  }
136 
137  QDomElement rasterShaderElem = doc.createElement( QStringLiteral( "rastershader" ) );
138  QgsColorRampShader *colorRampShader = dynamic_cast<QgsColorRampShader *>( mRasterShaderFunction.get() );
139  if ( colorRampShader )
140  {
141  QDomElement colorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
142  colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
143  colorRampShaderElem.setAttribute( "classificationMode", colorRampShader->classificationMode() );
144  colorRampShaderElem.setAttribute( QStringLiteral( "clip" ), colorRampShader->clip() );
145 
146  // save source color ramp
147  if ( colorRampShader->sourceColorRamp() )
148  {
149  QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), colorRampShader->sourceColorRamp(), doc );
150  colorRampShaderElem.appendChild( colorRampElem );
151  }
152 
153  //items
154  QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
155  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
156  for ( ; itemIt != itemList.constEnd(); ++itemIt )
157  {
158  QDomElement itemElem = doc.createElement( QStringLiteral( "item" ) );
159  itemElem.setAttribute( QStringLiteral( "label" ), itemIt->label );
160  itemElem.setAttribute( QStringLiteral( "value" ), QgsRasterBlock::printValue( itemIt->value ) );
161  itemElem.setAttribute( QStringLiteral( "color" ), itemIt->color.name() );
162  itemElem.setAttribute( QStringLiteral( "alpha" ), itemIt->color.alpha() );
163  colorRampShaderElem.appendChild( itemElem );
164  }
165  rasterShaderElem.appendChild( colorRampShaderElem );
166  }
167  parent.appendChild( rasterShaderElem );
168 }
169 
170 void QgsRasterShader::readXml( const QDomElement &elem )
171 {
172  //only colorrampshader
173  QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
174  if ( !colorRampShaderElem.isNull() )
175  {
176  QgsColorRampShader *colorRampShader = new QgsColorRampShader();
177 
178  // try to load color ramp (optional)
179  QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral( "colorramp" ) );
180  if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
181  {
182  colorRampShader->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
183  }
184 
185  colorRampShader->setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) );
186  colorRampShader->setClassificationMode( static_cast< QgsColorRampShader::ClassificationMode >( colorRampShaderElem.attribute( QStringLiteral( "classificationMode" ), QStringLiteral( "1" ) ).toInt() ) );
187  colorRampShader->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
188 
189  QList<QgsColorRampShader::ColorRampItem> itemList;
190  QDomElement itemElem;
191  QString itemLabel;
192  double itemValue;
193  QColor itemColor;
194 
195  QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral( "item" ) );
196  itemList.reserve( itemNodeList.size() );
197  for ( int i = 0; i < itemNodeList.size(); ++i )
198  {
199  itemElem = itemNodeList.at( i ).toElement();
200  itemValue = itemElem.attribute( QStringLiteral( "value" ) ).toDouble();
201  itemLabel = itemElem.attribute( QStringLiteral( "label" ) );
202  itemColor.setNamedColor( itemElem.attribute( QStringLiteral( "color" ) ) );
203  itemColor.setAlpha( itemElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt() );
204 
205  itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
206  }
207  colorRampShader->setColorRampItemList( itemList );
208  setRasterShaderFunction( colorRampShader );
209  }
210 }
void setMaximumValue(double)
Set the maximum value.
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &list)
Set custom colormap.
static QString printValue(double value)
Print double value with all necessary significant digits.
void setMinimumValue(double)
Return the minimum value.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
static QDomElement saveColorRamp(const QString &name, QgsColorRamp *ramp, QDomDocument &doc)
Encodes a color ramp&#39;s settings to an XML element.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Get the custom colormap.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
void setColorRampType(QgsColorRampShader::Type colorRampType)
Set the color ramp type.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:38
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
void setRasterShaderFunction(QgsRasterShaderFunction *function)
A public method that allows the user to set their own shader function.
static QgsColorRamp * loadColorRamp(QDomElement &element)
Creates a color ramp from the settings encoded in an XML element.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
bool shade(double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlpha)
generates and new RGBA value based on one input value
QString colorRampTypeAsQString()
Get the color ramp type as a string.
QgsRasterShader(double minimumValue=0.0, double maximumValue=255.0)
void writeXml(QDomDocument &doc, QDomElement &parent) const
Writes shader state to an XML element.
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
bool clip() const
Returns whether the shader will clip values which are out of range.
ClassificationMode classificationMode() const
Returns the classification mode.
QgsColorRamp * sourceColorRamp() const
Get the source color ramp.
void readXml(const QDomElement &elem)
Reads shader state from an XML element.