QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgssinglebandpseudocolorrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssinglebandpseudocolorrenderer.cpp
3  ------------------------------------
4  begin : January 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsrastershader.h"
20 #include "qgsrastertransparency.h"
21 #include "qgsrasterviewport.h"
22 #include <QDomDocument>
23 #include <QDomElement>
24 #include <QImage>
25 
27  QgsRasterRenderer( input, "singlebandpseudocolor" )
28  , mShader( shader )
29  , mBand( band )
30  , mClassificationMin( std::numeric_limits<double>::quiet_NaN() )
31  , mClassificationMax( std::numeric_limits<double>::quiet_NaN() )
32  , mClassificationMinMaxOrigin( QgsRasterRenderer::MinMaxUnknown )
33 {
34 }
35 
37 {
38  delete mShader;
39 }
40 
42 {
43  if ( bandNo > mInput->bandCount() || bandNo <= 0 )
44  {
45  return;
46  }
47  mBand = bandNo;
48 }
49 
51 {
52  QgsRasterShader *shader = nullptr;
53 
54  if ( mShader )
55  {
56  shader = new QgsRasterShader( mShader->minimumValue(), mShader->maximumValue() );
57 
58  // Shader function
59  const QgsColorRampShader* origColorRampShader = dynamic_cast<const QgsColorRampShader*>( mShader->rasterShaderFunction() );
60 
61  if ( origColorRampShader )
62  {
63  QgsColorRampShader * colorRampShader = new QgsColorRampShader( mShader->minimumValue(), mShader->maximumValue() );
64 
65  colorRampShader->setColorRampType( origColorRampShader->colorRampType() );
66  colorRampShader->setClip( origColorRampShader->clip() );
67  colorRampShader->setColorRampItemList( origColorRampShader->colorRampItemList() );
68  shader->setRasterShaderFunction( colorRampShader );
69  }
70  }
71  QgsSingleBandPseudoColorRenderer * renderer = new QgsSingleBandPseudoColorRenderer( nullptr, mBand, shader );
72  renderer->copyCommonProperties( this );
73 
74  return renderer;
75 }
76 
78 {
79  delete mShader;
80  mShader = shader;
81 }
82 
84 {
85  if ( elem.isNull() )
86  {
87  return nullptr;
88  }
89 
90  int band = elem.attribute( "band", "-1" ).toInt();
91  QgsRasterShader* shader = nullptr;
92  QDomElement rasterShaderElem = elem.firstChildElement( "rastershader" );
93  if ( !rasterShaderElem.isNull() )
94  {
95  shader = new QgsRasterShader();
96  shader->readXML( rasterShaderElem );
97  }
98 
100  r->readXML( elem );
101 
102  // TODO: add _readXML in superclass?
103  r->setClassificationMin( elem.attribute( "classificationMin", "NaN" ).toDouble() );
104  r->setClassificationMax( elem.attribute( "classificationMax", "NaN" ).toDouble() );
105  r->setClassificationMinMaxOrigin( QgsRasterRenderer::minMaxOriginFromName( elem.attribute( "classificationMinMaxOrigin", "Unknown" ) ) );
106 
107  return r;
108 }
109 
110 QgsRasterBlock* QgsSingleBandPseudoColorRenderer::block( int bandNo, QgsRectangle const & extent, int width, int height )
111 {
112  return block2( bandNo, extent, width, height );
113 }
114 
116 {
117  Q_UNUSED( bandNo );
118 
119  QgsRasterBlock *outputBlock = new QgsRasterBlock();
120  if ( !mInput || !mShader )
121  {
122  return outputBlock;
123  }
124 
125 
126  QgsRasterBlock *inputBlock = mInput->block2( mBand, extent, width, height, feedback );
127  if ( !inputBlock || inputBlock->isEmpty() )
128  {
129  QgsDebugMsg( "No raster data!" );
130  delete inputBlock;
131  return outputBlock;
132  }
133 
134  //rendering is faster without considering user-defined transparency
135  bool hasTransparency = usesTransparency();
136 
137  QgsRasterBlock *alphaBlock = nullptr;
138  if ( mAlphaBand > 0 && mAlphaBand != mBand )
139  {
140  alphaBlock = mInput->block2( mAlphaBand, extent, width, height, feedback );
141  if ( !alphaBlock || alphaBlock->isEmpty() )
142  {
143  delete inputBlock;
144  delete alphaBlock;
145  return outputBlock;
146  }
147  }
148  else if ( mAlphaBand == mBand )
149  {
150  alphaBlock = inputBlock;
151  }
152 
153  if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) )
154  {
155  delete inputBlock;
156  delete alphaBlock;
157  return outputBlock;
158  }
159 
160  QRgb myDefaultColor = NODATA_COLOR;
161 
162  for ( qgssize i = 0; i < ( qgssize )width*height; i++ )
163  {
164  if ( inputBlock->isNoData( i ) )
165  {
166  outputBlock->setColor( i, myDefaultColor );
167  continue;
168  }
169  double val = inputBlock->value( i );
170  int red, green, blue, alpha;
171  if ( !mShader->shade( val, &red, &green, &blue, &alpha ) )
172  {
173  outputBlock->setColor( i, myDefaultColor );
174  continue;
175  }
176 
177  if ( alpha < 255 )
178  {
179  // Working with premultiplied colors, so multiply values by alpha
180  red *= ( alpha / 255.0 );
181  blue *= ( alpha / 255.0 );
182  green *= ( alpha / 255.0 );
183  }
184 
185  if ( !hasTransparency )
186  {
187  outputBlock->setColor( i, qRgba( red, green, blue, alpha ) );
188  }
189  else
190  {
191  //opacity
192  double currentOpacity = mOpacity;
193  if ( mRasterTransparency )
194  {
195  currentOpacity = mRasterTransparency->alphaValue( val, mOpacity * 255 ) / 255.0;
196  }
197  if ( mAlphaBand > 0 )
198  {
199  currentOpacity *= alphaBlock->value( i ) / 255.0;
200  }
201 
202  outputBlock->setColor( i, qRgba( currentOpacity * red, currentOpacity * green, currentOpacity * blue, currentOpacity * alpha ) );
203  }
204  }
205 
206  delete inputBlock;
207  if ( mAlphaBand > 0 && mBand != mAlphaBand )
208  {
209  delete alphaBlock;
210  }
211 
212  return outputBlock;
213 }
214 
216 {
217  if ( parentElem.isNull() )
218  {
219  return;
220  }
221 
222  QDomElement rasterRendererElem = doc.createElement( "rasterrenderer" );
223  _writeXML( doc, rasterRendererElem );
224  rasterRendererElem.setAttribute( "band", mBand );
225  if ( mShader )
226  {
227  mShader->writeXML( doc, rasterRendererElem ); //todo: include color ramp items directly in this renderer
228  }
229  rasterRendererElem.setAttribute( "classificationMin", QgsRasterBlock::printValue( mClassificationMin ) );
230  rasterRendererElem.setAttribute( "classificationMax", QgsRasterBlock::printValue( mClassificationMax ) );
231  rasterRendererElem.setAttribute( "classificationMinMaxOrigin", QgsRasterRenderer::minMaxOriginName( mClassificationMinMaxOrigin ) );
232 
233  parentElem.appendChild( rasterRendererElem );
234 }
235 
237 {
238  if ( mShader )
239  {
240  QgsRasterShaderFunction* shaderFunction = mShader->rasterShaderFunction();
241  if ( shaderFunction )
242  {
243  shaderFunction->legendSymbologyItems( symbolItems );
244  }
245  }
246 }
247 
249 {
250  QList<int> bandList;
251  if ( mBand != -1 )
252  {
253  bandList << mBand;
254  }
255  return bandList;
256 }
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Interface for all raster shaders.
static QString printValue(double value)
Print double value with all necessary significant digits.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Get symbology items if provided by renderer.
QDomNode appendChild(const QDomNode &newChild)
QgsSingleBandPseudoColorRenderer * clone() const override
Clone itself, create deep copy.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
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
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
int alphaValue(double, int theGlobalTransparency=255) const
Returns the transparency value for a single value Pixel.
virtual QgsRasterInterface * input() const
Current input.
void copyCommonProperties(const QgsRasterRenderer *other)
Copies common properties like opacity / transparency data from other renderer.
static int minMaxOriginFromName(const QString &theName)
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.
double toDouble(bool *ok) const
virtual void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const
bool isNoData(int row, int column)
Check if value at position is no data.
void writeXML(QDomDocument &doc, QDomElement &parent) const
void setShader(QgsRasterShader *shader)
Takes ownership of the shader.
void readXML(const QDomElement &rendererElem) override
Sets base class members from xml.
void writeXML(QDomDocument &doc, QDomElement &parentElem) const override
Write base class members to xml.
virtual QgsRasterBlock * block2(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)
Read block of data using given extent and size.
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value.
bool setColor(int row, int column, QRgb color)
Set color on position.
static const QRgb NODATA_COLOR
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:150
Raster data container.
QgsSingleBandPseudoColorRenderer(QgsRasterInterface *input, int band, QgsRasterShader *shader)
Note: takes ownership of QgsRasterShader.
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
QgsRasterShaderFunction * rasterShaderFunction()
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height) override
Read block of data using given extent and size.
The raster shade function applies a shader to a pixel at render time - typically used to render grays...
double maximumValue()
Return the maximum value for the raster shader.
Raster renderer pipe for single band pseudocolor.
bool usesTransparency() const
bool shade(double, int *, int *, int *, int *)
generates and new RGBA value based on one input value
bool isEmpty() const
Returns true if block is empty, i.e.
void _writeXML(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXML method of subclasses) ...
QgsRasterBlock * block2(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
int mAlphaBand
Read alpha value from band.
QList< int > usesBands() const override
Returns a list of band numbers used by the renderer.
void setRasterShaderFunction(QgsRasterShaderFunction *)
A public method that allows the user to set their own shader function.
Base class for processing filters like renderers, reprojector, resampler etc.
void setColorRampType(QgsColorRampShader::ColorRamp_TYPE theColorRampType)
Set the color ramp type.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
Definition: qgis.h:500
bool reset(QGis::DataType theDataType, int theWidth, int theHeight)
Reset block.
bool isNull() const
virtual QgsRectangle extent()
Get the extent of the interface.
QDomElement firstChildElement(const QString &tagName) const
static QString minMaxOriginName(int theOrigin)
double minimumValue()
Return the minimum value for the raster shader.
double value(int row, int column) const
Read a single value if type of block is numeric.
void setBand(int bandNo)
Sets the band used by the renderer.
double mOpacity
Global alpha value (0-1)
void readXML(const QDomElement &elem)
QDomElement createElement(const QString &tagName)
QgsRasterInterface * mInput
Feedback object tailored for raster block reading.
Raster renderer pipe that applies colors to a raster.
int band() const
Returns the band used by the renderer.