QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #define tr( sourceText ) QCoreApplication::translate ( "QgsRasterRenderer", sourceText )
28 
29 // See #9101 before any change of NODATA_COLOR!
30 const QRgb QgsRasterRenderer::NODATA_COLOR = qRgba( 0, 0, 0, 0 );
31 
33  : QgsRasterInterface( input )
34  , mType( type ), mOpacity( 1.0 ), mRasterTransparency( 0 )
35  , mAlphaBand( -1 ) //, mInvertColor( false )
36 {
37 }
38 
40 {
41  delete mRasterTransparency;
42 }
43 
45 {
46  if ( mOn ) return 1;
47 
48  if ( mInput ) return mInput->bandCount();
49 
50  return 0;
51 }
52 
54 {
55  QgsDebugMsg( "Entered" );
56 
57  if ( mOn ) return QGis::ARGB32_Premultiplied;
58 
59  if ( mInput ) return mInput->dataType( bandNo );
60 
61  return QGis::UnknownDataType;
62 }
63 
65 {
66  // Renderer can only work with numerical values in at least 1 band
67  if ( !input ) return false;
68 
69  if ( !mOn )
70  {
71  // In off mode we can connect to anything
72  mInput = input;
73  return true;
74  }
75 
76  for ( int i = 1; i <= input->bandCount(); i++ )
77  {
78  if ( !QgsRasterBlock::typeIsNumeric( input->dataType( i ) ) )
79  {
80  return false;
81  }
82  }
83  mInput = input;
84  return true;
85 }
86 
88 {
89  if ( !mInput )
90  {
91  return true;
92  }
93  return ( mAlphaBand > 0 || ( mRasterTransparency && !mRasterTransparency->isEmpty() ) || !qgsDoubleNear( mOpacity, 1.0 ) );
94 }
95 
97 {
98  delete mRasterTransparency;
100 }
101 
102 void QgsRasterRenderer::_writeXML( QDomDocument& doc, QDomElement& rasterRendererElem ) const
103 {
104  if ( rasterRendererElem.isNull() )
105  {
106  return;
107  }
108 
109  rasterRendererElem.setAttribute( "type", mType );
110  rasterRendererElem.setAttribute( "opacity", QString::number( mOpacity ) );
111  rasterRendererElem.setAttribute( "alphaBand", mAlphaBand );
112 
113  if ( mRasterTransparency )
114  {
115  mRasterTransparency->writeXML( doc, rasterRendererElem );
116  }
117 }
118 
119 void QgsRasterRenderer::readXML( const QDomElement& rendererElem )
120 {
121  if ( rendererElem.isNull() )
122  {
123  return;
124  }
125 
126  mType = rendererElem.attribute( "type" );
127  mOpacity = rendererElem.attribute( "opacity", "1.0" ).toDouble();
128  mAlphaBand = rendererElem.attribute( "alphaBand", "-1" ).toInt();
129 
130  QDomElement rasterTransparencyElem = rendererElem.firstChildElement( "rasterTransparency" );
131  if ( !rasterTransparencyElem.isNull() )
132  {
133  delete mRasterTransparency;
135  mRasterTransparency->readXML( rasterTransparencyElem );
136  }
137 }
138 
140 {
141  if ( theOrigin == MinMaxUnknown )
142  {
143  return "Unknown";
144  }
145  else if ( theOrigin == MinMaxUser )
146  {
147  return "User";
148  }
149 
150  QString name;
151  if ( theOrigin & MinMaxMinMax )
152  {
153  name += "MinMax";
154  }
155  else if ( theOrigin & MinMaxCumulativeCut )
156  {
157  name += "CumulativeCut";
158  }
159  else if ( theOrigin & MinMaxStdDev )
160  {
161  name += "StdDev";
162  }
163 
164  if ( theOrigin & MinMaxFullExtent )
165  {
166  name += "FullExtent";
167  }
168  else if ( theOrigin & MinMaxSubExtent )
169  {
170  name += "SubExtent";
171  }
172 
173  if ( theOrigin & MinMaxEstimated )
174  {
175  name += "Estimated";
176  }
177  else if ( theOrigin & MinMaxExact )
178  {
179  name += "Exact";
180  }
181  return name;
182 }
183 
185 {
186  if ( theOrigin == MinMaxUnknown )
187  {
188  return tr( "Unknown" );
189  }
190  else if ( theOrigin == MinMaxUser )
191  {
192  return tr( "User defined" );
193  }
194 
195  QString label;
196  QString est_exact;
197  QString values;
198  QString extent;
199 
200  if ( theOrigin & MinMaxEstimated )
201  {
202  est_exact = tr( "Estimated" );
203  }
204  else if ( theOrigin & MinMaxExact )
205  {
206  est_exact = tr( "Exact" );
207  }
208 
209  if ( theOrigin & MinMaxMinMax )
210  {
211  values = tr( "min / max" );
212  }
213  else if ( theOrigin & MinMaxCumulativeCut )
214  {
215  values = tr( "cumulative cut" );
216  }
217  else if ( theOrigin & MinMaxStdDev )
218  {
219  values = tr( "standard deviation" );
220  }
221 
222  if ( theOrigin & MinMaxFullExtent )
223  {
224  extent = tr( "full extent" );
225  }
226  else if ( theOrigin & MinMaxSubExtent )
227  {
228  extent = tr( "sub extent" );
229  }
230 
231  label = QCoreApplication::translate( "QgsRasterRenderer", "%1 %2 of %3.",
232  "min/max origin label in raster properties, where %1 - estimated/exact, %2 - values (min/max, stddev, etc.), %3 - extent" )
233  .arg( est_exact )
234  .arg( values )
235  .arg( extent );
236  return label;
237 }
238 
240 {
241  if ( theName.contains( "Unknown" ) )
242  {
243  return MinMaxUnknown;
244  }
245  else if ( theName.contains( "User" ) )
246  {
247  return MinMaxUser;
248  }
249 
250  int origin = 0;
251 
252  if ( theName.contains( "MinMax" ) )
253  {
254  origin |= MinMaxMinMax;
255  }
256  else if ( theName.contains( "CumulativeCut" ) )
257  {
258  origin |= MinMaxCumulativeCut;
259  }
260  else if ( theName.contains( "StdDev" ) )
261  {
262  origin |= MinMaxStdDev;
263  }
264 
265  if ( theName.contains( "FullExtent" ) )
266  {
267  origin |= MinMaxFullExtent;
268  }
269  else if ( theName.contains( "SubExtent" ) )
270  {
271  origin |= MinMaxSubExtent;
272  }
273 
274  if ( theName.contains( "Estimated" ) )
275  {
276  origin |= MinMaxEstimated;
277  }
278  else if ( theName.contains( "Exact" ) )
279  {
280  origin |= MinMaxExact;
281  }
282  return origin;
283 }
virtual int bandCount() const =0
Get number of bands.
static int minMaxOriginFromName(QString theName)
QString attribute(const QString &name, const QString &defValue) const
static bool typeIsNumeric(QGis::DataType type)
Returns true if data type is numeric.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
QgsRasterRenderer(QgsRasterInterface *input=0, const QString &type="")
virtual QgsRasterInterface * input() const
Current input.
double toDouble(bool *ok) const
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:350
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)
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.
DataType
Raster data types.
Definition: qgis.h:204
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.
Unknown or unspecified type.
Definition: qgis.h:206
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)
void readXML(const QDomElement &elem)
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
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:221
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.
#define tr(sourceText)