QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsrasterrendererregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterrendererregistry.cpp
3 -----------------------------
4 begin : January 2012
5 copyright : (C) 2012 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
19
20#include "qgsapplication.h"
21#include "qgscolorrampimpl.h"
28#include "qgsrastershader.h"
31#include "qgssettings.h"
35
36#include <QIcon>
37#include <QString>
38
39using namespace Qt::StringLiterals;
40
51
53{
54 return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
55}
56
58{
59 // insert items in a particular order, which is returned in renderersList()
60 insert( QgsRasterRendererRegistryEntry( u"multibandcolor"_s, QObject::tr( "Multiband color" ),
63 insert( QgsRasterRendererRegistryEntry( u"paletted"_s, QObject::tr( "Paletted/Unique values" ), QgsPalettedRasterRenderer::create, nullptr ) );
64 insert( QgsRasterRendererRegistryEntry( u"singlebandgray"_s, QObject::tr( "Singleband gray" ),
66 insert( QgsRasterRendererRegistryEntry( u"singlebandpseudocolor"_s, QObject::tr( "Singleband pseudocolor" ),
68 insert( QgsRasterRendererRegistryEntry( u"singlebandcolordata"_s, QObject::tr( "Singleband color data" ),
70 insert( QgsRasterRendererRegistryEntry( u"singlecolor"_s, QObject::tr( "Single color" ),
72 insert( QgsRasterRendererRegistryEntry( u"hillshade"_s, QObject::tr( "Hillshade" ),
74 insert( QgsRasterRendererRegistryEntry( u"contour"_s, QObject::tr( "Contours" ),
76}
77
79{
80 mEntries.insert( entry.name, entry );
81 mSortedEntries.append( entry.name );
82}
83
85{
86 if ( !mEntries.contains( rendererName ) )
87 {
88 return;
89 }
90 mEntries[rendererName].widgetCreateFunction = func;
91}
92
93bool QgsRasterRendererRegistry::rendererData( const QString &rendererName, QgsRasterRendererRegistryEntry &data ) const
94{
95 const QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.find( rendererName );
96 if ( it == mEntries.constEnd() )
97 {
98 return false;
99 }
100 data = it.value();
101 return true;
102}
103
105{
106 return mSortedEntries;
107}
108
109QList< QgsRasterRendererRegistryEntry > QgsRasterRendererRegistry::entries() const
110{
111 QList< QgsRasterRendererRegistryEntry > result;
112
113 QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constBegin();
114 for ( ; it != mEntries.constEnd(); ++it )
115 {
116 result.push_back( it.value() );
117 }
118 return result;
119}
120
122{
123 const QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constFind( rendererName );
124 if ( it != mEntries.constEnd() )
125 {
126 return it.value().capabilities;
127 }
129}
130
132{
133 if ( !provider || provider->bandCount() < 1 )
134 {
135 return nullptr;
136 }
137
138 std::unique_ptr< QgsRasterRenderer > renderer;
139 switch ( drawingStyle )
140 {
142 {
143 const int grayBand = 1; //reasonable default
144
145 // first preference -- use attribute table to generate classes
146 if ( provider->attributeTable( grayBand ) )
147 {
148 std::unique_ptr<QgsColorRamp> ramp;
149 if ( ! provider->attributeTable( grayBand )->hasColor() )
150 {
151 ramp = std::make_unique< QgsRandomColorRamp >();
152 }
154 if ( !classes.empty() )
155 {
156 renderer = std::make_unique< QgsPalettedRasterRenderer >( provider, grayBand, classes );
157 }
158 }
159
160 // second preference -- use raster color table to generate classes
161 if ( !renderer )
162 {
164 if ( !classes.empty() )
165 {
166 renderer = std::make_unique< QgsPalettedRasterRenderer >( provider, grayBand, classes );
167 }
168 }
169
170 // last preference -- just fallback to single band gray renderer if we couldn't determine color palette
171 if ( ! renderer )
172 {
173 renderer = std::make_unique< QgsSingleBandGrayRenderer >( provider, grayBand );
174
176 provider->dataType( grayBand ) ) );
177
178 // Default contrast enhancement is set from QgsRasterLayer, it has already setContrastEnhancementAlgorithm(). Default enhancement must only be set if default style was not loaded (to avoid stats calculation).
179 qgis::down_cast< QgsSingleBandGrayRenderer * >( renderer.get() )->setContrastEnhancement( ce );
180 }
181 }
182 break;
183
186 {
187 const int grayBand = 1;
188
189 // If the raster band has an attribute table try to use it.
190 QString ratErrorMessage;
191 if ( QgsRasterAttributeTable *rat = provider->attributeTable( grayBand ); rat && rat->isValid( &ratErrorMessage ) )
192 {
193 renderer.reset( rat->createRenderer( provider, grayBand ) );
194 }
195
196 if ( ! ratErrorMessage.isEmpty() )
197 {
198 QgsDebugMsgLevel( u"Invalid RAT from band 1, RAT was not used to create the renderer: %1."_s.arg( ratErrorMessage ), 2 );
199 }
200
201 if ( ! renderer )
202 {
203 renderer = std::make_unique< QgsSingleBandGrayRenderer >( provider, grayBand );
204
206 provider->dataType( grayBand ) ) );
207
208 // Default contrast enhancement is set from QgsRasterLayer, it has already setContrastEnhancementAlgorithm(). Default enhancement must only be set if default style was not loaded (to avoid stats calculation).
209 qgis::down_cast< QgsSingleBandGrayRenderer * >( renderer.get() )->setContrastEnhancement( ce );
210 }
211 break;
212 }
213
215 {
216 const int bandNo = 1;
217 double minValue = 0;
218 double maxValue = 0;
219 // TODO: avoid calculating statistics if not necessary (default style loaded)
220 minMaxValuesForBand( bandNo, provider, minValue, maxValue );
221 QgsRasterShader *shader = new QgsRasterShader( minValue, maxValue );
222 renderer = std::make_unique< QgsSingleBandPseudoColorRenderer >( provider, bandNo, shader );
223 break;
224 }
226 {
227 const QgsSettings s;
228
229 int redBand = s.value( u"/Raster/defaultRedBand"_s, 1 ).toInt();
230 if ( redBand < 0 || redBand > provider->bandCount() )
231 {
232 redBand = -1;
233 }
234 int greenBand = s.value( u"/Raster/defaultGreenBand"_s, 2 ).toInt();
235 if ( greenBand < 0 || greenBand > provider->bandCount() )
236 {
237 greenBand = -1;
238 }
239 int blueBand = s.value( u"/Raster/defaultBlueBand"_s, 3 ).toInt();
240 if ( blueBand < 0 || blueBand > provider->bandCount() )
241 {
242 blueBand = -1;
243 }
244
245 renderer = std::make_unique< QgsMultiBandColorRenderer >( provider, redBand, greenBand, blueBand );
246 break;
247 }
249 {
250 renderer = std::make_unique< QgsSingleBandColorDataRenderer >( provider, 1 );
251 break;
252 }
253 default:
254 return nullptr;
255 }
256
257 auto tr = std::make_unique< QgsRasterTransparency >();
258 const int bandCount = renderer->usesBands().size();
259 if ( bandCount == 1 )
260 {
261 tr->setTransparentSingleValuePixelList( {} );
262 }
263 else if ( bandCount == 3 )
264 {
265 tr->setTransparentThreeValuePixelList( {} );
266 }
267 renderer->setRasterTransparency( tr.release() );
268 return renderer.release();
269}
270
271bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider *provider, double &minValue, double &maxValue ) const
272{
273 if ( !provider )
274 {
275 return false;
276 }
277
278 minValue = 0;
279 maxValue = 0;
280
281 const QgsSettings s;
282 if ( s.value( u"/Raster/useStandardDeviation"_s, false ).toBool() )
283 {
284 const QgsRasterBandStats stats = provider->bandStatistics( band, Qgis::RasterBandStatistic::Mean | Qgis::RasterBandStatistic::StdDev );
285
286 const double stdDevFactor = s.value( u"/Raster/defaultStandardDeviation"_s, 2.0 ).toDouble();
287 const double diff = stdDevFactor * stats.stdDev;
288 minValue = stats.mean - diff;
289 maxValue = stats.mean + diff;
290 }
291 else
292 {
293 const QgsRasterBandStats stats = provider->bandStatistics( band, Qgis::RasterBandStatistic::Min | Qgis::RasterBandStatistic::Max );
294 minValue = stats.minimumValue;
295 maxValue = stats.maximumValue;
296 }
297 return true;
298}
RasterDrawingStyle
Raster drawing styles.
Definition qgis.h:4851
@ SingleBandGray
A single band image drawn as a range of gray colors.
Definition qgis.h:4853
@ MultiBandSingleBandGray
A layer containing 2 or more bands, but a single band drawn as a range of gray colors.
Definition qgis.h:4859
@ SingleBandColorData
ARGB values rendered directly.
Definition qgis.h:4862
@ MultiBandColor
A layer containing 2 or more bands, mapped to RGB color space. In the case of a multiband with only t...
Definition qgis.h:4861
@ PalettedColor
A "Palette" image drawn using color table.
Definition qgis.h:4855
@ SingleBandPseudoColor
A single band image drawn using a pseudocolor algorithm.
Definition qgis.h:4854
QFlags< RasterRendererCapability > RasterRendererCapabilities
Raster renderer capabilities.
Definition qgis.h:1591
@ StdDev
Standard deviation.
Definition qgis.h:6212
DataType
Raster data types.
Definition qgis.h:379
@ UsesMultipleBands
The renderer utilizes multiple raster bands for color data (note that alpha bands are not considered ...
Definition qgis.h:1582
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Handles contrast enhancement and clipping.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Factory method to create a new renderer.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
static QgsPalettedRasterRenderer::MultiValueClassData rasterAttributeTableToClassData(const QgsRasterAttributeTable *attributeTable, int classificationColumn=-1, QgsColorRamp *ramp=nullptr)
Reads and returns classes from the Raster Attribute Table attributeTable, optionally classifying the ...
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of value to class properties.
QList< QgsPalettedRasterRenderer::MultiValueClass > MultiValueClassData
Map of multi value to class properties.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
static QgsPalettedRasterRenderer::ClassData colorTableToClassData(const QList< QgsColorRampShader::ColorRampItem > &table)
Converts a raster color table to paletted renderer class data.
Represents a Raster Attribute Table (RAT).
bool hasColor() const
Returns true if the Raster Attribute Table has color RGBA information.
bool isValid(QString *errorMessage=nullptr) const
Returns true if the Raster Attribute Table is valid, optionally reporting validity checks results in ...
double mean
The mean cell value for the band. NO_DATA values are excluded.
double stdDev
The standard deviation of the cell values.
double minimumValue
The minimum cell value in the raster band.
double maximumValue
The maximum cell value in the raster band.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Creates an instance of the renderer based on definition from XML (used by renderer registry).
Base class for raster data providers.
QgsRasterAttributeTable * attributeTable(int bandNumber) const
Returns the (possibly nullptr) attribute table for the specified bandNumber.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
Q_DECL_DEPRECATED QgsRasterBandStats bandStatistics(int bandNo, int stats, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
virtual int bandCount() const =0
Gets number of bands.
void insertWidgetFunction(const QString &rendererName, QgsRasterRendererWidgetCreateFunc func)
Sets the widget creation function for a renderer.
QgsRasterRendererRegistry()
Constructor for QgsRasterRendererRegistry.
QList< QgsRasterRendererRegistryEntry > entries() const
Returns the list of registered renderers.
void insert(const QgsRasterRendererRegistryEntry &entry)
Inserts a new entry into the registry.
bool rendererData(const QString &rendererName, QgsRasterRendererRegistryEntry &data) const
Retrieves renderer data from the registry.
QgsRasterRenderer * defaultRendererForDrawingStyle(Qgis::RasterDrawingStyle drawingStyle, QgsRasterDataProvider *provider) const
Creates a default renderer for a raster drawing style (considering user options such as default contr...
Qgis::RasterRendererCapabilities rendererCapabilities(const QString &rendererName) const
Returns the capabilities for the renderer with the specified name.
QStringList renderersList() const
Returns a list of the names of registered renderers.
Raster renderer pipe that applies colors to a raster.
Interface for all raster shaders.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Creates an instance of the renderer based on definition from XML (used by the renderer registry).
Stores settings for use within QGIS.
Definition qgssettings.h:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
QgsRasterRendererWidget *(* QgsRasterRendererWidgetCreateFunc)(QgsRasterLayer *, const QgsRectangle &extent)
QgsRasterRenderer *(* QgsRasterRendererCreateFunc)(const QDomElement &, QgsRasterInterface *input)
Registry for raster renderer entries.
QgsRasterRendererWidgetCreateFunc widgetCreateFunction
Qgis::RasterRendererCapabilities capabilities
Renderer capabilities.
QgsRasterRendererCreateFunc rendererCreateFunction