QGIS API Documentation  2.17.0-Master (eef6f05)
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 <QDomDocument>
24 #include <QDomElement>
25 
26 QgsRasterShader::QgsRasterShader( double theMinimumValue, double theMaximumValue )
27 {
28  QgsDebugMsgLevel( "called.", 4 );
29 
30  mMinimumValue = theMinimumValue;
31  mMaximumValue = theMaximumValue;
32  mRasterShaderFunction = new QgsRasterShaderFunction( mMinimumValue, mMaximumValue );
33 }
34 
36 {
37  delete mRasterShaderFunction;
38 }
39 
50 bool QgsRasterShader::shade( double theValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int *theReturnAlpha )
51 {
52  if ( mRasterShaderFunction )
53  {
54  return mRasterShaderFunction->shade( theValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlpha );
55  }
56 
57  return false;
58 }
73 bool QgsRasterShader::shade( double theRedValue, double theGreenValue, double theBlueValue, double theAlphaValue, int* theReturnRedValue, int* theReturnGreenValue, int* theReturnBlueValue, int* theReturnAlphaValue )
74 {
75  if ( mRasterShaderFunction )
76  {
77  return mRasterShaderFunction->shade( theRedValue, theGreenValue, theBlueValue, theAlphaValue, theReturnRedValue, theReturnGreenValue, theReturnBlueValue, theReturnAlphaValue );
78  }
79 
80  return false;
81 }
82 
89 {
90  QgsDebugMsgLevel( "called.", 4 );
91 
92  if ( mRasterShaderFunction == theFunction )
93  return;
94 
95  if ( theFunction )
96  {
97  delete mRasterShaderFunction;
98  mRasterShaderFunction = theFunction;
99  }
100 }
101 
107 void QgsRasterShader::setMaximumValue( double theValue )
108 {
109  QgsDebugMsgLevel( "Value = " + QString::number( theValue ), 4 );
110 
111  mMaximumValue = theValue;
112  if ( mRasterShaderFunction )
113  {
114  mRasterShaderFunction->setMaximumValue( theValue );
115  }
116 }
117 
123 void QgsRasterShader::setMinimumValue( double theValue )
124 {
125  QgsDebugMsgLevel( "Value = " + QString::number( theValue ), 4 );
126 
127  mMinimumValue = theValue;
128  if ( mRasterShaderFunction )
129  {
130  mRasterShaderFunction->setMinimumValue( theValue );
131  }
132 }
133 
135 {
136  if ( parent.isNull() || !mRasterShaderFunction )
137  {
138  return;
139  }
140 
141  QDomElement rasterShaderElem = doc.createElement( "rastershader" );
142  QgsColorRampShader* colorRampShader = dynamic_cast<QgsColorRampShader*>( mRasterShaderFunction );
143  if ( colorRampShader )
144  {
145  QDomElement colorRampShaderElem = doc.createElement( "colorrampshader" );
146  colorRampShaderElem.setAttribute( "colorRampType", colorRampShader->colorRampTypeAsQString() );
147  colorRampShaderElem.setAttribute( "clip", colorRampShader->clip() );
148  //items
149  QList<QgsColorRampShader::ColorRampItem> itemList = colorRampShader->colorRampItemList();
151  for ( ; itemIt != itemList.constEnd(); ++itemIt )
152  {
153  QDomElement itemElem = doc.createElement( "item" );
154  itemElem.setAttribute( "label", itemIt->label );
155  itemElem.setAttribute( "value", QgsRasterBlock::printValue( itemIt->value ) );
156  itemElem.setAttribute( "color", itemIt->color.name() );
157  itemElem.setAttribute( "alpha", itemIt->color.alpha() );
158  colorRampShaderElem.appendChild( itemElem );
159  }
160  rasterShaderElem.appendChild( colorRampShaderElem );
161  }
162  parent.appendChild( rasterShaderElem );
163 }
164 
166 {
167  //only colorrampshader
168  QDomElement colorRampShaderElem = elem.firstChildElement( "colorrampshader" );
169  if ( !colorRampShaderElem.isNull() )
170  {
171  QgsColorRampShader* colorRampShader = new QgsColorRampShader();
172  colorRampShader->setColorRampType( colorRampShaderElem.attribute( "colorRampType", "INTERPOLATED" ) );
173  colorRampShader->setClip( colorRampShaderElem.attribute( "clip", "0" ) == "1" );
174 
176  QDomElement itemElem;
177  QString itemLabel;
178  double itemValue;
179  QColor itemColor;
180 
181  QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( "item" );
182  itemList.reserve( itemNodeList.size() );
183  for ( int i = 0; i < itemNodeList.size(); ++i )
184  {
185  itemElem = itemNodeList.at( i ).toElement();
186  itemValue = itemElem.attribute( "value" ).toDouble();
187  itemLabel = itemElem.attribute( "label" );
188  itemColor.setNamedColor( itemElem.attribute( "color" ) );
189  itemColor.setAlpha( itemElem.attribute( "alpha", "255" ).toInt() );
190 
191  itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
192  }
193  colorRampShader->setColorRampItemList( itemList );
194  setRasterShaderFunction( colorRampShader );
195  }
196 }
QDomNodeList elementsByTagName(const QString &tagname) const
void setMaximumValue(double)
Set the maximum value.
static QString printValue(double value)
Print double value with all necessary significant digits.
QDomNode appendChild(const QDomNode &newChild)
void push_back(const T &value)
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.
QString attribute(const QString &name, const QString &defValue) const
void reserve(int alloc)
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 setAlpha(int alpha)
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.
void setNamedColor(const QString &name)
double toDouble(bool *ok) const
void writeXML(QDomDocument &doc, QDomElement &parent) const
QDomElement toElement() const
QString number(int n, int base)
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:34
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
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
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.
bool isNull() const
QDomElement firstChildElement(const QString &tagName) 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)
void readXML(const QDomElement &elem)
int size() const
const_iterator constEnd() const
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
virtual void setMaximumValue(double)
Set the maximum value.
QDomNode at(int index) const