QGIS API Documentation  2.99.0-Master (0a63d1f)
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 theMinimumValue, double theMaximumValue )
29 {
30  QgsDebugMsgLevel( "called.", 4 );
31 
32  mMinimumValue = theMinimumValue;
33  mMaximumValue = theMaximumValue;
34  mRasterShaderFunction = new QgsRasterShaderFunction( mMinimumValue, mMaximumValue );
35 }
36 
38 {
39  delete mRasterShaderFunction;
40 }
41 
52 bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int *theReturnAlpha )
53 {
54  if ( mRasterShaderFunction )
55  {
56  return mRasterShaderFunction->shade( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlpha );
57  }
58 
59  return false;
60 }
61 
76 bool QgsRasterShader::shade( double theRedValue, double theGreenValue, double theBlueValue, double theAlphaValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int* theReturnAlphaValue )
77 {
78  if ( mRasterShaderFunction )
79  {
80  return mRasterShaderFunction->shade( theRedValue, theGreenValue, theBlueValue, theAlphaValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
81  }
82 
83  return false;
84 }
85 
92 {
93  QgsDebugMsgLevel( "called.", 4 );
94 
95  if ( mRasterShaderFunction == theFunction )
96  return;
97 
98  if ( theFunction )
99  {
100  delete mRasterShaderFunction;
101  mRasterShaderFunction = theFunction;
102  }
103 }
104 
110 void QgsRasterShader::setMaximumValue( double theValue )
111 {
112  QgsDebugMsgLevel( "Value = " + QString::number( theValue ), 4 );
113 
114  mMaximumValue = theValue;
115  if ( mRasterShaderFunction )
116  {
117  mRasterShaderFunction->setMaximumValue( theValue );
118  }
119 }
120 
126 void QgsRasterShader::setMinimumValue( double theValue )
127 {
128  QgsDebugMsgLevel( "Value = " + QString::number( theValue ), 4 );
129 
130  mMinimumValue = theValue;
131  if ( mRasterShaderFunction )
132  {
133  mRasterShaderFunction->setMinimumValue( theValue );
134  }
135 }
136 
137 void QgsRasterShader::writeXml( QDomDocument& doc, QDomElement& parent ) const
138 {
139  if ( parent.isNull() || !mRasterShaderFunction )
140  {
141  return;
142  }
143 
144  QDomElement rasterShaderElem = doc.createElement( QStringLiteral( "rastershader" ) );
145  QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( mRasterShaderFunction );
146  if ( colorRampShader )
147  {
148  QDomElement colorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
149  colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
150  colorRampShaderElem.setAttribute( "classificationMode", colorRampShader->classificationMode() );
151  colorRampShaderElem.setAttribute( QStringLiteral( "clip" ), colorRampShader->clip() );
152 
153  // save source color ramp
154  if ( colorRampShader->sourceColorRamp() )
155  {
156  QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), colorRampShader->sourceColorRamp(), doc );
157  colorRampShaderElem.appendChild( colorRampElem );
158  }
159 
160  //items
161  QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
162  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
163  for ( ; itemIt != itemList.constEnd(); ++itemIt )
164  {
165  QDomElement itemElem = doc.createElement( QStringLiteral( "item" ) );
166  itemElem.setAttribute( QStringLiteral( "label" ), itemIt->label );
167  itemElem.setAttribute( QStringLiteral( "value" ), QgsRasterBlock::printValue( itemIt->value ) );
168  itemElem.setAttribute( QStringLiteral( "color" ), itemIt->color.name() );
169  itemElem.setAttribute( QStringLiteral( "alpha" ), itemIt->color.alpha() );
170  colorRampShaderElem.appendChild( itemElem );
171  }
172  rasterShaderElem.appendChild( colorRampShaderElem );
173  }
174  parent.appendChild( rasterShaderElem );
175 }
176 
177 void QgsRasterShader::readXml( const QDomElement& elem )
178 {
179  //only colorrampshader
180  QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
181  if ( !colorRampShaderElem.isNull() )
182  {
183  QgsColorRampShader* colorRampShader = new QgsColorRampShader();
184 
185  // try to load color ramp (optional)
186  QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral( "colorramp" ) );
187  if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
188  {
189  colorRampShader->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
190  }
191 
192  colorRampShader->setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) );
193  colorRampShader->setClassificationMode( static_cast< QgsColorRampShader::ClassificationMode >( colorRampShaderElem.attribute( QStringLiteral( "classificationMode" ), QStringLiteral( "1" ) ).toInt() ) );
194  colorRampShader->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
195 
196  QList<QgsColorRampShader::ColorRampItem> itemList;
197  QDomElement itemElem;
198  QString itemLabel;
199  double itemValue;
200  QColor itemColor;
201 
202  QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral( "item" ) );
203  itemList.reserve( itemNodeList.size() );
204  for ( int i = 0; i < itemNodeList.size(); ++i )
205  {
206  itemElem = itemNodeList.at( i ).toElement();
207  itemValue = itemElem.attribute( QStringLiteral( "value" ) ).toDouble();
208  itemLabel = itemElem.attribute( QStringLiteral( "label" ) );
209  itemColor.setNamedColor( itemElem.attribute( QStringLiteral( "color" ) ) );
210  itemColor.setAlpha( itemElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt() );
211 
212  itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
213  }
214  colorRampShader->setColorRampItemList( itemList );
215  setRasterShaderFunction( colorRampShader );
216  }
217 }
void setMaximumValue(double)
Set the maximum value.
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.
virtual bool shade(double, int *, int *, int *, int *)
generates and new RGBA value based on one input value
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Get the custom colormap.
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &theList)
Set custom colormap.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:37
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
bool shade(double, int *, int *, int *, int *)
generates and new RGBA value based on one input value
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.
void setRasterShaderFunction(QgsRasterShaderFunction *)
A public method that allows the user to set their own shader function.
QString colorRampTypeAsQString()
Get the color ramp type as a string.
void writeXml(QDomDocument &doc, QDomElement &parent) const
Writes shader state to an XML element.
void setColorRampType(QgsColorRampShader::Type theColorRampType)
Set the color ramp type.
void setClassificationMode(ClassificationMode classificationMode)
Sets classification mode.
virtual void setMinimumValue(double)
Return the minimum value.
bool clip() const
Returns whether the shader will clip values which are out of range.
QgsRasterShader(double theMinimumValue=0.0, double theMaximumValue=255.0)
ClassificationMode classificationMode() const
Returns the classification mode.
QgsColorRamp * sourceColorRamp() const
Get the source color ramp.
virtual void setMaximumValue(double)
Set the maximum value.
void readXml(const QDomElement &elem)
Reads shader state from an XML element.