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