QGIS API Documentation  2.99.0-Master (a18066b)
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( QStringLiteral( "type" ), mType );
108  rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
109  rasterRendererElem.setAttribute( QStringLiteral( "alphaBand" ), mAlphaBand );
110 
111  if ( mRasterTransparency )
112  {
113  mRasterTransparency->writeXml( doc, rasterRendererElem );
114  }
115 
116  QDomElement minMaxOriginElem = doc.createElement( QStringLiteral( "minMaxOrigin" ) );
117  mMinMaxOrigin.writeXml( doc, minMaxOriginElem );
118  rasterRendererElem.appendChild( minMaxOriginElem );
119 }
120 
121 void QgsRasterRenderer::readXml( const QDomElement& rendererElem )
122 {
123  if ( rendererElem.isNull() )
124  {
125  return;
126  }
127 
128  mType = rendererElem.attribute( QStringLiteral( "type" ) );
129  mOpacity = rendererElem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toDouble();
130  mAlphaBand = rendererElem.attribute( QStringLiteral( "alphaBand" ), QStringLiteral( "-1" ) ).toInt();
131 
132  QDomElement rasterTransparencyElem = rendererElem.firstChildElement( QStringLiteral( "rasterTransparency" ) );
133  if ( !rasterTransparencyElem.isNull() )
134  {
135  delete mRasterTransparency;
137  mRasterTransparency->readXml( rasterTransparencyElem );
138  }
139 
140  QDomElement minMaxOriginElem = rendererElem.firstChildElement( QStringLiteral( "minMaxOrigin" ) );
141  if ( !minMaxOriginElem.isNull() )
142  {
143  mMinMaxOrigin.readXml( minMaxOriginElem );
144  }
145 }
146 
147 void QgsRasterRenderer::copyCommonProperties( const QgsRasterRenderer* other, bool copyMinMaxOrigin )
148 {
149  if ( !other )
150  return;
151 
152  setOpacity( other->opacity() );
153  setAlphaBand( other->alphaBand() );
154  setRasterTransparency( other->rasterTransparency() ? new QgsRasterTransparency( *other->rasterTransparency() ) : nullptr );
155  if ( copyMinMaxOrigin )
156  setMinMaxOrigin( other->minMaxOrigin() );
157 }
virtual int bandCount() const =0
Get number of bands.
bool isEmpty() const
True if there are no entries in the pixel lists except the nodata value.
double opacity() const
static bool typeIsNumeric(Qgis::DataType type)
Returns true if data type is numeric.
virtual QgsRasterInterface * input() const
Current input.
DataType
Raster data types.
Definition: qgis.h:61
QgsRasterMinMaxOrigin mMinMaxOrigin
Origin of min/max values.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:198
const QgsRasterTransparency * rasterTransparency() const
QgsRasterTransparency * mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:76
static const QRgb NODATA_COLOR
virtual bool setInput(QgsRasterInterface *input) override
Set input.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:37
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses) ...
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
void readXml(const QDomElement &elem)
Deserialize object.
bool usesTransparency() const
Unknown or unspecified type.
Definition: qgis.h:63
void readXml(const QDomElement &elem)
int mAlphaBand
Read alpha value from band.
void setAlphaBand(int band)
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
virtual int bandCount() const override
Get number of bands.
virtual Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
Base class for processing filters like renderers, reprojector, resampler etc.
void setMinMaxOrigin(const QgsRasterMinMaxOrigin &origin)
Sets origin of min/max values.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
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 setOpacity(double opacity)
QgsRasterInterface * mInput
void setRasterTransparency(QgsRasterTransparency *t)
Raster renderer pipe that applies colors to a raster.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Serialize object.