QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 {
34 }
35 
37 {
38  delete mRasterTransparency;
39 }
40 
42 {
43  if ( mOn ) return 1;
44 
45  if ( mInput ) return mInput->bandCount();
46 
47  return 0;
48 }
49 
51 {
52  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
53 
54  if ( mOn ) return Qgis::ARGB32_Premultiplied;
55 
56  if ( mInput ) return mInput->dataType( bandNo );
57 
58  return Qgis::UnknownDataType;
59 }
60 
62 {
63  // Renderer can only work with numerical values in at least 1 band
64  if ( !input ) return false;
65 
66  if ( !mOn )
67  {
68  // In off mode we can connect to anything
69  mInput = input;
70  return true;
71  }
72 
73  for ( int i = 1; i <= input->bandCount(); i++ )
74  {
75  const Qgis::DataType bandType = input->dataType( i );
76  // we always allow unknown data types to connect - overwise invalid layers cannot setup
77  // their original rendering pipe and this information is lost
78  if ( bandType != Qgis::UnknownDataType && !QgsRasterBlock::typeIsNumeric( bandType ) )
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( QStringLiteral( "type" ), mType );
110  rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
111  rasterRendererElem.setAttribute( QStringLiteral( "alphaBand" ), mAlphaBand );
112 
113  if ( mRasterTransparency )
114  {
115  mRasterTransparency->writeXml( doc, rasterRendererElem );
116  }
117 
118  QDomElement minMaxOriginElem = doc.createElement( QStringLiteral( "minMaxOrigin" ) );
119  mMinMaxOrigin.writeXml( doc, minMaxOriginElem );
120  rasterRendererElem.appendChild( minMaxOriginElem );
121 }
122 
123 void QgsRasterRenderer::readXml( const QDomElement &rendererElem )
124 {
125  if ( rendererElem.isNull() )
126  {
127  return;
128  }
129 
130  mType = rendererElem.attribute( QStringLiteral( "type" ) );
131  mOpacity = rendererElem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toDouble();
132  mAlphaBand = rendererElem.attribute( QStringLiteral( "alphaBand" ), QStringLiteral( "-1" ) ).toInt();
133 
134  QDomElement rasterTransparencyElem = rendererElem.firstChildElement( QStringLiteral( "rasterTransparency" ) );
135  if ( !rasterTransparencyElem.isNull() )
136  {
137  delete mRasterTransparency;
139  mRasterTransparency->readXml( rasterTransparencyElem );
140  }
141 
142  QDomElement minMaxOriginElem = rendererElem.firstChildElement( QStringLiteral( "minMaxOrigin" ) );
143  if ( !minMaxOriginElem.isNull() )
144  {
145  mMinMaxOrigin.readXml( minMaxOriginElem );
146  }
147 }
148 
149 void QgsRasterRenderer::copyCommonProperties( const QgsRasterRenderer *other, bool copyMinMaxOrigin )
150 {
151  if ( !other )
152  return;
153 
154  setOpacity( other->opacity() );
155  setAlphaBand( other->alphaBand() );
156  setRasterTransparency( other->rasterTransparency() ? new QgsRasterTransparency( *other->rasterTransparency() ) : nullptr );
157  if ( copyMinMaxOrigin )
158  setMinMaxOrigin( other->minMaxOrigin() );
159 }
160 
161 void QgsRasterRenderer::toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap & ) const
162 {
163  QDomElement rasterSymbolizerElem = doc.createElement( QStringLiteral( "sld:RasterSymbolizer" ) );
164  element.appendChild( rasterSymbolizerElem );
165 
166  // add opacity only is different from default
167  if ( !qgsDoubleNear( opacity(), 1.0 ) )
168  {
169  QDomElement opacityElem = doc.createElement( QStringLiteral( "sld:Opacity" ) );
170  opacityElem.appendChild( doc.createTextNode( QString::number( opacity() ) ) );
171  rasterSymbolizerElem.appendChild( opacityElem );
172  }
173 }
174 
176 {
177  return true;
178 }
virtual int bandCount() const =0
Gets number of bands.
bool isEmpty() const
True if there are no entries in the pixel lists except the nodata value.
virtual void toSld(QDomDocument &doc, QDomElement &element, const QgsStringMap &props=QgsStringMap()) const
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
double opacity() const
Returns the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:280
static bool typeIsNumeric(Qgis::DataType type)
Returns true if data type is numeric.
virtual QgsRasterInterface * input() const
Current input.
QgsRasterRenderer(QgsRasterInterface *input=nullptr, const QString &type=QString())
Constructor for QgsRasterRenderer.
DataType
Raster data types.
Definition: qgis.h:80
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.
An interface for classes which can visit style entity (e.g.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:612
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:95
static const QRgb NODATA_COLOR
bool setInput(QgsRasterInterface *input) override
Set input.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
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:82
void readXml(const QDomElement &elem)
Reads the transparency information from an XML document.
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.
int bandCount() const override
Gets number of bands.
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.
~QgsRasterRenderer() override
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Writes the transparency information to an XML document.
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.