QGIS API Documentation  2.99.0-Master (23ddace)
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 )
33  , mRasterTransparency( nullptr )
34  , mAlphaBand( -1 ) //, mInvertColor( false )
35 {
36 }
37 
39 {
40  delete mRasterTransparency;
41 }
42 
44 {
45  if ( mOn ) return 1;
46 
47  if ( mInput ) return mInput->bandCount();
48 
49  return 0;
50 }
51 
53 {
54  QgsDebugMsgLevel( "Entered", 4 );
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( QStringLiteral( "type" ), mType );
109  rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
110  rasterRendererElem.setAttribute( QStringLiteral( "alphaBand" ), mAlphaBand );
111 
112  if ( mRasterTransparency )
113  {
114  mRasterTransparency->writeXml( doc, rasterRendererElem );
115  }
116 
117  QDomElement minMaxOriginElem = doc.createElement( QStringLiteral( "minMaxOrigin" ) );
118  mMinMaxOrigin.writeXml( doc, minMaxOriginElem );
119  rasterRendererElem.appendChild( minMaxOriginElem );
120 }
121 
122 void QgsRasterRenderer::readXml( const QDomElement &rendererElem )
123 {
124  if ( rendererElem.isNull() )
125  {
126  return;
127  }
128 
129  mType = rendererElem.attribute( QStringLiteral( "type" ) );
130  mOpacity = rendererElem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toDouble();
131  mAlphaBand = rendererElem.attribute( QStringLiteral( "alphaBand" ), QStringLiteral( "-1" ) ).toInt();
132 
133  QDomElement rasterTransparencyElem = rendererElem.firstChildElement( QStringLiteral( "rasterTransparency" ) );
134  if ( !rasterTransparencyElem.isNull() )
135  {
136  delete mRasterTransparency;
138  mRasterTransparency->readXml( rasterTransparencyElem );
139  }
140 
141  QDomElement minMaxOriginElem = rendererElem.firstChildElement( QStringLiteral( "minMaxOrigin" ) );
142  if ( !minMaxOriginElem.isNull() )
143  {
144  mMinMaxOrigin.readXml( minMaxOriginElem );
145  }
146 }
147 
148 void QgsRasterRenderer::copyCommonProperties( const QgsRasterRenderer *other, bool copyMinMaxOrigin )
149 {
150  if ( !other )
151  return;
152 
153  setOpacity( other->opacity() );
154  setAlphaBand( other->alphaBand() );
155  setRasterTransparency( other->rasterTransparency() ? new QgsRasterTransparency( *other->rasterTransparency() ) : nullptr );
156  if ( copyMinMaxOrigin )
157  setMinMaxOrigin( other->minMaxOrigin() );
158 }
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
Returns the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1...
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:74
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:203
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:89
static const QRgb NODATA_COLOR
virtual bool setInput(QgsRasterInterface *input) override
Set input.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:38
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:76
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)
Sets the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1...
QgsRasterInterface * mInput
void setRasterTransparency(QgsRasterTransparency *t)
Raster renderer pipe that applies colors to a raster.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Serialize object.