QGIS API Documentation  2.99.0-Master (e077efd)
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( QStringLiteral( "clip" ), colorRampShader->clip() );
151 
152  // save source color ramp
153  if ( colorRampShader->sourceColorRamp() )
154  {
155  QDomElement colorRampElem = QgsSymbolLayerUtils::saveColorRamp( QStringLiteral( "[source]" ), colorRampShader->sourceColorRamp(), doc );
156  colorRampShaderElem.appendChild( colorRampElem );
157  }
158 
159  //items
160  QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
161  QList<QgsColorRampShader::ColorRampItem>::const_iterator itemIt = itemList.constBegin();
162  for ( ; itemIt != itemList.constEnd(); ++itemIt )
163  {
164  QDomElement itemElem = doc.createElement( QStringLiteral( "item" ) );
165  itemElem.setAttribute( QStringLiteral( "label" ), itemIt->label );
166  itemElem.setAttribute( QStringLiteral( "value" ), QgsRasterBlock::printValue( itemIt->value ) );
167  itemElem.setAttribute( QStringLiteral( "color" ), itemIt->color.name() );
168  itemElem.setAttribute( QStringLiteral( "alpha" ), itemIt->color.alpha() );
169  colorRampShaderElem.appendChild( itemElem );
170  }
171  rasterShaderElem.appendChild( colorRampShaderElem );
172  }
173  parent.appendChild( rasterShaderElem );
174 }
175 
176 void QgsRasterShader::readXml( const QDomElement& elem )
177 {
178  //only colorrampshader
179  QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
180  if ( !colorRampShaderElem.isNull() )
181  {
182  QgsColorRampShader* colorRampShader = new QgsColorRampShader();
183 
184  // try to load color ramp (optional)
185  QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral( "colorramp" ) );
186  if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
187  {
188  colorRampShader->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
189  }
190 
191  colorRampShader->setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) );
192  colorRampShader->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
193 
194  QList<QgsColorRampShader::ColorRampItem> itemList;
195  QDomElement itemElem;
196  QString itemLabel;
197  double itemValue;
198  QColor itemColor;
199 
200  QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral( "item" ) );
201  itemList.reserve( itemNodeList.size() );
202  for ( int i = 0; i < itemNodeList.size(); ++i )
203  {
204  itemElem = itemNodeList.at( i ).toElement();
205  itemValue = itemElem.attribute( QStringLiteral( "value" ) ).toDouble();
206  itemLabel = itemElem.attribute( QStringLiteral( "label" ) );
207  itemColor.setNamedColor( itemElem.attribute( QStringLiteral( "color" ) ) );
208  itemColor.setAlpha( itemElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt() );
209 
210  itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
211  }
212  colorRampShader->setColorRampItemList( itemList );
213  setRasterShaderFunction( colorRampShader );
214  }
215 }
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:34
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.
void setColorRampType(QgsColorRampShader::ColorRamp_TYPE theColorRampType)
Set the color ramp type.
QString colorRampTypeAsQString()
Get the color ramp type as a string.
void writeXml(QDomDocument &doc, QDomElement &parent) const
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)
QgsColorRamp * sourceColorRamp() const
Get the source color ramp.
virtual void setMaximumValue(double)
Set the maximum value.
void readXml(const QDomElement &elem)