QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
48
50{
51 return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
52}
53
55{
56 // insert items in a particular order, which is returned in renderersList()
57 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "multibandcolor" ), QObject::tr( "Multiband color" ),
60 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "paletted" ), QObject::tr( "Paletted/Unique values" ), QgsPalettedRasterRenderer::create, nullptr ) );
61 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandgray" ), QObject::tr( "Singleband gray" ),
63 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandpseudocolor" ), QObject::tr( "Singleband pseudocolor" ),
65 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandcolordata" ), QObject::tr( "Singleband color data" ),
67 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlecolor" ), QObject::tr( "Single color" ),
69 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "hillshade" ), QObject::tr( "Hillshade" ),
71 insert( QgsRasterRendererRegistryEntry( QStringLiteral( "contour" ), QObject::tr( "Contours" ),
73}
74
76{
77 mEntries.insert( entry.name, entry );
78 mSortedEntries.append( entry.name );
79}
80
82{
83 if ( !mEntries.contains( rendererName ) )
84 {
85 return;
86 }
87 mEntries[rendererName].widgetCreateFunction = func;
88}
89
90bool QgsRasterRendererRegistry::rendererData( const QString &rendererName, QgsRasterRendererRegistryEntry &data ) const
91{
92 const QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.find( rendererName );
93 if ( it == mEntries.constEnd() )
94 {
95 return false;
96 }
97 data = it.value();
98 return true;
99}
100
102{
103 return mSortedEntries;
104}
105
106QList< QgsRasterRendererRegistryEntry > QgsRasterRendererRegistry::entries() const
107{
108 QList< QgsRasterRendererRegistryEntry > result;
109
110 QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constBegin();
111 for ( ; it != mEntries.constEnd(); ++it )
112 {
113 result.push_back( it.value() );
114 }
115 return result;
116}
117
119{
120 const QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constFind( rendererName );
121 if ( it != mEntries.constEnd() )
122 {
123 return it.value().capabilities;
124 }
126}
127
129{
130 if ( !provider || provider->bandCount() < 1 )
131 {
132 return nullptr;
133 }
134
135 std::unique_ptr< QgsRasterRenderer > renderer;
136 switch ( drawingStyle )
137 {
139 {
140 const int grayBand = 1; //reasonable default
141
142 // first preference -- use attribute table to generate classes
143 if ( provider->attributeTable( grayBand ) )
144 {
145 std::unique_ptr<QgsColorRamp> ramp;
146 if ( ! provider->attributeTable( grayBand )->hasColor() )
147 {
148 ramp = std::make_unique< QgsRandomColorRamp >();
149 }
151 if ( !classes.empty() )
152 {
153 renderer = std::make_unique< QgsPalettedRasterRenderer >( provider, grayBand, classes );
154 }
155 }
156
157 // second preference -- use raster color table to generate classes
158 if ( !renderer )
159 {
161 if ( !classes.empty() )
162 {
163 renderer = std::make_unique< QgsPalettedRasterRenderer >( provider, grayBand, classes );
164 }
165 }
166
167 // last preference -- just fallback to single band gray renderer if we couldn't determine color palette
168 if ( ! renderer )
169 {
170 renderer = std::make_unique< QgsSingleBandGrayRenderer >( provider, grayBand );
171
173 provider->dataType( grayBand ) ) );
174
175 // 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).
176 qgis::down_cast< QgsSingleBandGrayRenderer * >( renderer.get() )->setContrastEnhancement( ce );
177 }
178 }
179 break;
180
183 {
184 const int grayBand = 1;
185
186 // If the raster band has an attribute table try to use it.
187 QString ratErrorMessage;
188 if ( QgsRasterAttributeTable *rat = provider->attributeTable( grayBand ); rat && rat->isValid( &ratErrorMessage ) )
189 {
190 renderer.reset( rat->createRenderer( provider, grayBand ) );
191 }
192
193 if ( ! ratErrorMessage.isEmpty() )
194 {
195 QgsDebugMsgLevel( QStringLiteral( "Invalid RAT from band 1, RAT was not used to create the renderer: %1." ).arg( ratErrorMessage ), 2 );
196 }
197
198 if ( ! renderer )
199 {
200 renderer = std::make_unique< QgsSingleBandGrayRenderer >( provider, grayBand );
201
203 provider->dataType( grayBand ) ) );
204
205 // 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).
206 qgis::down_cast< QgsSingleBandGrayRenderer * >( renderer.get() )->setContrastEnhancement( ce );
207 }
208 break;
209 }
210
212 {
213 const int bandNo = 1;
214 double minValue = 0;
215 double maxValue = 0;
216 // TODO: avoid calculating statistics if not necessary (default style loaded)
217 minMaxValuesForBand( bandNo, provider, minValue, maxValue );
218 QgsRasterShader *shader = new QgsRasterShader( minValue, maxValue );
219 renderer = std::make_unique< QgsSingleBandPseudoColorRenderer >( provider, bandNo, shader );
220 break;
221 }
223 {
224 const QgsSettings s;
225
226 int redBand = s.value( QStringLiteral( "/Raster/defaultRedBand" ), 1 ).toInt();
227 if ( redBand < 0 || redBand > provider->bandCount() )
228 {
229 redBand = -1;
230 }
231 int greenBand = s.value( QStringLiteral( "/Raster/defaultGreenBand" ), 2 ).toInt();
232 if ( greenBand < 0 || greenBand > provider->bandCount() )
233 {
234 greenBand = -1;
235 }
236 int blueBand = s.value( QStringLiteral( "/Raster/defaultBlueBand" ), 3 ).toInt();
237 if ( blueBand < 0 || blueBand > provider->bandCount() )
238 {
239 blueBand = -1;
240 }
241
242 renderer = std::make_unique< QgsMultiBandColorRenderer >( provider, redBand, greenBand, blueBand );
243 break;
244 }
246 {
247 renderer = std::make_unique< QgsSingleBandColorDataRenderer >( provider, 1 );
248 break;
249 }
250 default:
251 return nullptr;
252 }
253
254 auto tr = std::make_unique< QgsRasterTransparency >();
255 const int bandCount = renderer->usesBands().size();
256 if ( bandCount == 1 )
257 {
258 tr->setTransparentSingleValuePixelList( {} );
259 }
260 else if ( bandCount == 3 )
261 {
262 tr->setTransparentThreeValuePixelList( {} );
263 }
264 renderer->setRasterTransparency( tr.release() );
265 return renderer.release();
266}
267
268bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider *provider, double &minValue, double &maxValue ) const
269{
270 if ( !provider )
271 {
272 return false;
273 }
274
275 minValue = 0;
276 maxValue = 0;
277
278 const QgsSettings s;
279 if ( s.value( QStringLiteral( "/Raster/useStandardDeviation" ), false ).toBool() )
280 {
281 const QgsRasterBandStats stats = provider->bandStatistics( band, Qgis::RasterBandStatistic::Mean | Qgis::RasterBandStatistic::StdDev );
282
283 const double stdDevFactor = s.value( QStringLiteral( "/Raster/defaultStandardDeviation" ), 2.0 ).toDouble();
284 const double diff = stdDevFactor * stats.stdDev;
285 minValue = stats.mean - diff;
286 maxValue = stats.mean + diff;
287 }
288 else
289 {
290 const QgsRasterBandStats stats = provider->bandStatistics( band, Qgis::RasterBandStatistic::Min | Qgis::RasterBandStatistic::Max );
291 minValue = stats.minimumValue;
292 maxValue = stats.maximumValue;
293 }
294 return true;
295}
RasterDrawingStyle
Raster drawing styles.
Definition qgis.h:4779
@ SingleBandGray
A single band image drawn as a range of gray colors.
Definition qgis.h:4781
@ MultiBandSingleBandGray
A layer containing 2 or more bands, but a single band drawn as a range of gray colors.
Definition qgis.h:4787
@ SingleBandColorData
ARGB values rendered directly.
Definition qgis.h:4790
@ 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:4789
@ PalettedColor
A "Palette" image drawn using color table.
Definition qgis.h:4783
@ SingleBandPseudoColor
A single band image drawn using a pseudocolor algorithm.
Definition qgis.h:4782
QFlags< RasterRendererCapability > RasterRendererCapabilities
Raster renderer capabilities.
Definition qgis.h:1533
@ StdDev
Standard deviation.
Definition qgis.h:5946
DataType
Raster data types.
Definition qgis.h:372
@ UsesMultipleBands
The renderer utilizes multiple raster bands for color data (note that alpha bands are not considered ...
Definition qgis.h:1524
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:65
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:61
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