QGIS API Documentation 3.43.0-Master (261ee7da134)
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"
20
21#include "qgscolorutils.h"
23#include "qgssldexportcontext.h"
24
25#include <QCoreApplication>
26#include <QDomDocument>
27#include <QDomElement>
28#include <QImage>
29#include <QPainter>
30
31// See #9101 before any change of NODATA_COLOR!
32const QRgb QgsRasterRenderer::NODATA_COLOR = qRgba( 0, 0, 0, 0 );
33
35 : QgsRasterInterface( input )
36 , mType( type )
37{
38}
39
44
46{
47 if ( mOn ) return 1;
48
49 if ( mInput ) return mInput->bandCount();
50
51 return 0;
52}
53
55{
56 QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
57
59
60 if ( mInput ) return mInput->dataType( bandNo );
61
63}
64
69
71{
72 return false;
73}
74
76{
77 // Renderer can only work with numerical values in at least 1 band
78 if ( !input ) return false;
79
80 if ( !mOn )
81 {
82 // In off mode we can connect to anything
83 mInput = input;
84 return true;
85 }
86
87 for ( int i = 1; i <= input->bandCount(); i++ )
88 {
89 const Qgis::DataType bandType = input->dataType( i );
90 // we always allow unknown data types to connect - otherwise invalid layers cannot setup
91 // their original rendering pipe and this information is lost
92 if ( bandType != Qgis::DataType::UnknownDataType && !QgsRasterBlock::typeIsNumeric( bandType ) )
93 {
94 return false;
95 }
96 }
97 mInput = input;
98 return true;
99}
100
102{
103 return -1;
104}
105
107{
108 return false;
109}
110
112{
113 if ( !mInput )
114 {
115 return true;
116 }
117 return ( mAlphaBand > 0 || ( mRasterTransparency && !mRasterTransparency->isEmpty() ) || !qgsDoubleNear( mOpacity, 1.0 ) );
118}
119
125
126QList< QPair< QString, QColor > > QgsRasterRenderer::legendSymbologyItems() const
127{
128 return QList< QPair< QString, QColor > >();
129}
130
131QList<QgsLayerTreeModelLegendNode *> QgsRasterRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
132{
133 QList<QgsLayerTreeModelLegendNode *> nodes;
134
135 const QList< QPair< QString, QColor > > rasterItemList = legendSymbologyItems();
136 if ( rasterItemList.isEmpty() )
137 return nodes;
138
139 // Paletted raster may have many colors, for example UInt16 may have 65536 colors
140 // and it is very slow, so we limit max count
141 int count = 0;
142 const int max_count = 1000;
143
144 for ( auto itemIt = rasterItemList.constBegin(); itemIt != rasterItemList.constEnd(); ++itemIt, ++count )
145 {
146 nodes << new QgsRasterSymbolLegendNode( nodeLayer, itemIt->second, itemIt->first );
147
148 if ( count == max_count )
149 {
150 const QString label = tr( "following %n item(s) not displayed", nullptr, rasterItemList.size() - max_count );
151 nodes << new QgsSimpleLegendNode( nodeLayer, label );
152 break;
153 }
154 }
155
156 return nodes;
157}
158
159void QgsRasterRenderer::_writeXml( QDomDocument &doc, QDomElement &rasterRendererElem ) const
160{
161 if ( rasterRendererElem.isNull() )
162 {
163 return;
164 }
165
166 rasterRendererElem.setAttribute( QStringLiteral( "type" ), mType );
167 rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
168 rasterRendererElem.setAttribute( QStringLiteral( "alphaBand" ), mAlphaBand );
169 rasterRendererElem.setAttribute( QStringLiteral( "nodataColor" ), mNodataColor.isValid() ? QgsColorUtils::colorToString( mNodataColor ) : QString() );
170
172 {
173 mRasterTransparency->writeXml( doc, rasterRendererElem );
174 }
175
176 QDomElement minMaxOriginElem = doc.createElement( QStringLiteral( "minMaxOrigin" ) );
177 mMinMaxOrigin.writeXml( doc, minMaxOriginElem );
178 rasterRendererElem.appendChild( minMaxOriginElem );
179}
180
182{
183 if ( !mNodataColor.isValid() )
184 return NODATA_COLOR;
185 else
186 return mNodataColor.rgba();
187}
188
189void QgsRasterRenderer::readXml( const QDomElement &rendererElem )
190{
191 if ( rendererElem.isNull() )
192 {
193 return;
194 }
195
196 mType = rendererElem.attribute( QStringLiteral( "type" ) );
197 mOpacity = rendererElem.attribute( QStringLiteral( "opacity" ), QStringLiteral( "1.0" ) ).toDouble();
198 mAlphaBand = rendererElem.attribute( QStringLiteral( "alphaBand" ), QStringLiteral( "-1" ) ).toInt();
199 const QString colorEncoded = rendererElem.attribute( QStringLiteral( "nodataColor" ) );
200 mNodataColor = !colorEncoded.isEmpty() ? QgsColorUtils::colorFromString( colorEncoded ) : QColor();
201
202 const QDomElement rasterTransparencyElem = rendererElem.firstChildElement( QStringLiteral( "rasterTransparency" ) );
203 if ( !rasterTransparencyElem.isNull() )
204 {
206
207 mRasterTransparency->readXml( rasterTransparencyElem );
208 }
209
210 const QDomElement minMaxOriginElem = rendererElem.firstChildElement( QStringLiteral( "minMaxOrigin" ) );
211 if ( !minMaxOriginElem.isNull() )
212 {
213 mMinMaxOrigin.readXml( minMaxOriginElem );
214 }
215}
216
217void QgsRasterRenderer::copyCommonProperties( const QgsRasterRenderer *other, bool copyMinMaxOrigin )
218{
219 if ( !other )
220 return;
221
222 setOpacity( other->opacity() );
223 setAlphaBand( other->alphaBand() );
225 setNodataColor( other->nodataColor() );
226 if ( copyMinMaxOrigin )
227 setMinMaxOrigin( other->minMaxOrigin() );
228}
229
230void QgsRasterRenderer::toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
231{
232 QgsSldExportContext context;
233 context.setExtraProperties( props );
234 toSld( doc, element, context );
235}
236
237bool QgsRasterRenderer::toSld( QDomDocument &doc, QDomElement &element, QgsSldExportContext & ) const
238{
239 QDomElement rasterSymbolizerElem = doc.createElement( QStringLiteral( "sld:RasterSymbolizer" ) );
240 element.appendChild( rasterSymbolizerElem );
241
242 // add opacity only is different from default
243 if ( !qgsDoubleNear( opacity(), 1.0 ) )
244 {
245 QDomElement opacityElem = doc.createElement( QStringLiteral( "sld:Opacity" ) );
246 opacityElem.appendChild( doc.createTextNode( QString::number( opacity() ) ) );
247 rasterSymbolizerElem.appendChild( opacityElem );
248 }
249 return true;
250}
251
253{
254 return true;
255}
256
268
269bool QgsRasterRenderer::refresh( const QgsRectangle &extent, const QList<double> &, const QList<double> &, bool forceRefresh )
270{
271 if ( !needsRefresh( extent ) && !forceRefresh )
272 {
273 return false;
274 }
275
277 return true;
278}
QFlags< RasterRendererFlag > RasterRendererFlags
Flags which control behavior of raster renderers.
Definition qgis.h:1475
@ NotSet
User defined.
DataType
Raster data types.
Definition qgis.h:351
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ UnknownDataType
Unknown or unspecified type.
@ UpdatedCanvas
Constantly updated extent of the canvas is used to compute statistics.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Layer tree node points to a map layer.
static bool typeIsNumeric(Qgis::DataType type)
Returns true if a data type is numeric.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
virtual int bandCount() const =0
Gets number of bands.
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
Qgis::RasterRangeExtent extent() const
Returns the raster extent.
void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Serialize object.
void readXml(const QDomElement &elem)
Deserialize object.
Qgis::RasterRangeLimit limits() const
Returns the raster limits.
Raster renderer pipe that applies colors to a raster.
QColor nodataColor() const
Returns the color to use for shading nodata pixels.
QgsRasterRenderer(QgsRasterInterface *input=nullptr, const QString &type=QString())
Constructor for QgsRasterRenderer.
virtual bool canCreateRasterAttributeTable() const
Returns true if the renderer is suitable for attribute table creation.
double mOpacity
Global alpha value (0-1)
bool setInput(QgsRasterInterface *input) override
Set input.
void setMinMaxOrigin(const QgsRasterMinMaxOrigin &origin)
Sets origin of min/max values.
virtual int inputBand() const
Returns the input band for the renderer, or -1 if no input band is available.
int mAlphaBand
Read alpha value from band.
QgsRectangle mLastRectangleUsedByRefreshContrastEnhancementIfNeeded
To save computations and possible infinite cycle of notifications.
virtual bool setInputBand(int band)
Attempts to set the input band for the renderer.
QRgb renderColorForNodataPixel() const
Returns the color for the renderer to use to represent nodata pixels.
const QgsRasterTransparency * rasterTransparency() const
virtual Qgis::RasterRendererFlags flags() const
Returns flags which dictate renderer behavior.
std::unique_ptr< QgsRasterTransparency > mRasterTransparency
Raster transparency per color or value. Overwrites global alpha value.
const QgsRasterMinMaxOrigin & minMaxOrigin() const
Returns const reference to origin of min/max values.
void _writeXml(QDomDocument &doc, QDomElement &rasterRendererElem) const
Write upper class info into rasterrenderer element (called by writeXml method of subclasses)
int bandCount() const override
Gets number of bands.
virtual bool refresh(const QgsRectangle &extent, const QList< double > &min, const QList< double > &max, bool forceRefresh=false)
Refreshes the renderer according to the min and max values associated with the extent.
double opacity() const
Returns the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1....
void setAlphaBand(int band)
void setOpacity(double opacity)
Sets the opacity for the renderer, where opacity is a value between 0 (totally transparent) and 1....
QgsRasterMinMaxOrigin mMinMaxOrigin
Origin of min/max values.
virtual QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer)
Creates a set of legend nodes representing the renderer.
void setRasterTransparency(QgsRasterTransparency *t)
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
bool usesTransparency() const
void copyCommonProperties(const QgsRasterRenderer *other, bool copyMinMaxOrigin=true)
Copies common properties like opacity / transparency data from other renderer.
static const QRgb NODATA_COLOR
bool needsRefresh(const QgsRectangle &extent) const
Checks if the renderer needs to be refreshed according to extent.
virtual Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const
Used from subclasses to create SLD Rule elements following SLD v1.0 specs.
void setNodataColor(const QColor &color)
Sets the color to use for shading nodata pixels.
virtual QList< QPair< QString, QColor > > legendSymbologyItems() const
Returns symbology items if provided by renderer.
void readXml(const QDomElement &rendererElem) override
Sets base class members from xml. Usually called from create() methods of subclasses.
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
Implementation of legend node interface for displaying raster legend entries.
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
A rectangle specified with double values.
Implementation of legend node interface for displaying arbitrary labels with icons.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
An interface for classes which can visit style entity (e.g.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6302
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41