QGIS API Documentation  2.15.0-Master (972fc9f)
qgsrasterrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterrenderer.cpp
3  ---------------------
4  begin : December 2011
5  copyright : (C) 2011 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 
18 #include "qgsrasterrenderer.h"
19 #include "qgsrastertransparency.h"
20 
21 #include <QCoreApplication>
22 #include <QDomDocument>
23 #include <QDomElement>
24 #include <QImage>
25 #include <QPainter>
26 
27 // See #9101 before any change of NODATA_COLOR!
28 const QRgb QgsRasterRenderer::NODATA_COLOR = qRgba( 0, 0, 0, 0 );
29 
31  : QgsRasterInterface( input )
32  , mType( type ), mOpacity( 1.0 ), mRasterTransparency( nullptr )
33  , mAlphaBand( -1 ) //, mInvertColor( false )
34 {
35 }
36 
38 {
39  delete mRasterTransparency;
40 }
41 
43 {
44  if ( mOn ) return 1;
45 
46  if ( mInput ) return mInput->bandCount();
47 
48  return 0;
49 }
50 
52 {
53  QgsDebugMsgLevel( "Entered", 4 );
54 
55  if ( mOn ) return QGis::ARGB32_Premultiplied;
56 
57  if ( mInput ) return mInput->dataType( bandNo );
58 
59  return QGis::UnknownDataType;
60 }
61 
63 {
64  // Renderer can only work with numerical values in at least 1 band
65  if ( !input ) return false;
66 
67  if ( !mOn )
68  {
69  // In off mode we can connect to anything
70  mInput = input;
71  return true;
72  }
73 
74  for ( int i = 1; i <= input->bandCount(); i++ )
75  {
76  if ( !QgsRasterBlock::typeIsNumeric( input->dataType( i ) ) )
77  {
78  return false;
79  }
80  }
81  mInput = input;
82  return true;
83 }
84 
86 {
87  if ( !mInput )
88  {
89  return true;
90  }
91  return ( mAlphaBand > 0 || ( mRasterTransparency && !mRasterTransparency->isEmpty() ) || !qgsDoubleNear( mOpacity, 1.0 ) );
92 }
93 
95 {
96  delete mRasterTransparency;
98 }
99 
100 void QgsRasterRenderer::_writeXML( QDomDocument& doc, QDomElement& rasterRendererElem ) const
101 {
102  if ( rasterRendererElem.isNull() )
103  {
104  return;
105  }
106 
107  rasterRendererElem.setAttribute( "type", mType );
108  rasterRendererElem.setAttribute( "opacity", QString::number( mOpacity ) );
109  rasterRendererElem.setAttribute( "alphaBand", mAlphaBand );
110 
111  if ( mRasterTransparency )
112  {
113  mRasterTransparency->writeXML( doc, rasterRendererElem );
114  }
115 }
116 
117 void QgsRasterRenderer::readXML( const QDomElement& rendererElem )
118 {
119  if ( rendererElem.isNull() )
120  {
121  return;
122  }
123 
124  mType = rendererElem.attribute( "type" );
125  mOpacity = rendererElem.attribute( "opacity", "1.0" ).toDouble();
126  mAlphaBand = rendererElem.attribute( "alphaBand", "-1" ).toInt();
127 
128  QDomElement rasterTransparencyElem = rendererElem.firstChildElement( "rasterTransparency" );
129  if ( !rasterTransparencyElem.isNull() )
130  {
131  delete mRasterTransparency;
133  mRasterTransparency->readXML( rasterTransparencyElem );
134  }
135 }
136 
138 {
139  if ( theOrigin == MinMaxUnknown )
140  {
141  return "Unknown";
142  }
143  else if ( theOrigin == MinMaxUser )
144  {
145  return "User";
146  }
147 
148  QString name;
149  if ( theOrigin & MinMaxMinMax )
150  {
151  name += "MinMax";
152  }
153  else if ( theOrigin & MinMaxCumulativeCut )
154  {
155  name += "CumulativeCut";
156  }
157  else if ( theOrigin & MinMaxStdDev )
158  {
159  name += "StdDev";
160  }
161 
162  if ( theOrigin & MinMaxFullExtent )
163  {
164  name += "FullExtent";
165  }
166  else if ( theOrigin & MinMaxSubExtent )
167  {
168  name += "SubExtent";
169  }
170 
171  if ( theOrigin & MinMaxEstimated )
172  {
173  name += "Estimated";
174  }
175  else if ( theOrigin & MinMaxExact )
176  {
177  name += "Exact";
178  }
179  return name;
180 }
181 
183 {
184  if ( theOrigin == MinMaxUnknown )
185  {
186  return tr( "Unknown" );
187  }
188  else if ( theOrigin == MinMaxUser )
189  {
190  return tr( "User defined" );
191  }
192 
193  QString label;
194  QString est_exact;
195  QString values;
196  QString extent;
197 
198  if ( theOrigin & MinMaxEstimated )
199  {
200  est_exact = tr( "Estimated" );
201  }
202  else if ( theOrigin & MinMaxExact )
203  {
204  est_exact = tr( "Exact" );
205  }
206 
207  if ( theOrigin & MinMaxMinMax )
208  {
209  values = tr( "min / max" );
210  }
211  else if ( theOrigin & MinMaxCumulativeCut )
212  {
213  values = tr( "cumulative cut" );
214  }
215  else if ( theOrigin & MinMaxStdDev )
216  {
217  values = tr( "standard deviation" );
218  }
219 
220  if ( theOrigin & MinMaxFullExtent )
221  {
222  extent = tr( "full extent" );
223  }
224  else if ( theOrigin & MinMaxSubExtent )
225  {
226  extent = tr( "sub extent" );
227  }
228 
229  label = QCoreApplication::translate( "QgsRasterRenderer", "%1 %2 of %3.",
230  "min/max origin label in raster properties, where %1 - estimated/exact, %2 - values (min/max, stddev, etc.), %3 - extent" )
231  .arg( est_exact,
232  values,
233  extent );
234  return label;
235 }
236 
238 {
239  if ( theName.contains( "Unknown" ) )
240  {
241  return MinMaxUnknown;
242  }
243  else if ( theName.contains( "User" ) )
244  {
245  return MinMaxUser;
246  }
247 
248  int origin = 0;
249 
250  if ( theName.contains( "MinMax" ) )
251  {
252  origin |= MinMaxMinMax;
253  }
254  else if ( theName.contains( "CumulativeCut" ) )
255  {
256  origin |= MinMaxCumulativeCut;
257  }
258  else if ( theName.contains( "StdDev" ) )
259  {
260  origin |= MinMaxStdDev;
261  }
262 
263  if ( theName.contains( "FullExtent" ) )
264  {
265  origin |= MinMaxFullExtent;
266  }
267  else if ( theName.contains( "SubExtent" ) )
268  {
269  origin |= MinMaxSubExtent;
270  }
271 
272  if ( theName.contains( "Estimated" ) )
273  {
274  origin |= MinMaxEstimated;
275  }
276  else if ( theName.contains( "Exact" ) )
277  {
278  origin |= MinMaxExact;
279  }
280  return origin;
281 }
virtual int bandCount() const =0
Get number of bands.
Unknown or unspecified type.
Definition: qgis.h:135
QString attribute(const QString &name, const QString &defValue) const
static bool typeIsNumeric(QGis::DataType type)
Returns true if data type is numeric.
static int minMaxOriginFromName(const QString &theName)
virtual QgsRasterInterface * input() const
Current input.
double toDouble(bool *ok) const
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:352
void readXML(const QDomElement &rendererElem) override
Sets base class members from xml.
bool usesTransparency() const
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value.
static const QRgb NODATA_COLOR
virtual bool setInput(QgsRasterInterface *input) override
Set input.
QString number(int n, int base)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:150
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:34
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
static QString minMaxOriginLabel(int theOrigin)
bool isEmpty() const
True if there are no entries in the pixel lists except the nodata value.
int mAlphaBand
Read alpha value from band.
virtual QGis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
virtual int bandCount() const override
Get number of bands.
Base class for processing filters like renderers, reprojector, resampler etc.
bool contains(QChar ch, Qt::CaseSensitivity cs) const
bool isNull() const
virtual QgsRectangle extent()
Get the extent of the interface.
QDomElement firstChildElement(const QString &tagName) const
QString translate(const char *context, const char *sourceText, const char *disambiguation, Encoding encoding)
void writeXML(QDomDocument &doc, QDomElement &parentElem) const
static QString minMaxOriginName(int theOrigin)
DataType
Raster data types.
Definition: qgis.h:133
void readXML(const QDomElement &elem)
QgsRasterRenderer(QgsRasterInterface *input=nullptr, const QString &type="")
double mOpacity
Global alpha value (0-1)
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
void _writeXML(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXML method of subclasses) ...
QgsRasterInterface * mInput
void setRasterTransparency(QgsRasterTransparency *t)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
virtual QGis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.