QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgsrasterdataprovider.h"
20 #include "qgsrastershader.h"
21 #include "qgsrastertransparency.h"
28 #include "qgshillshaderenderer.h"
29 #include "qgsapplication.h"
30 #include "qgssettings.h"
31 
32 #include <QIcon>
33 
34 QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry( const QString &name, const QString &visibleName,
35  QgsRasterRendererCreateFunc rendererFunction,
36  QgsRasterRendererWidgetCreateFunc widgetFunction )
37  : name( name )
38  , visibleName( visibleName )
39  , rendererCreateFunction( rendererFunction )
40  , widgetCreateFunction( widgetFunction )
41 {
42 }
43 
45 {
46  return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
47 }
48 
50 {
51  // insert items in a particular order, which is returned in renderersList()
52  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "multibandcolor" ), QObject::tr( "Multiband color" ),
54  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "paletted" ), QObject::tr( "Paletted/Unique values" ), QgsPalettedRasterRenderer::create, nullptr ) );
55  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandgray" ), QObject::tr( "Singleband gray" ),
57  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandpseudocolor" ), QObject::tr( "Singleband pseudocolor" ),
59  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandcolordata" ), QObject::tr( "Singleband color data" ),
61  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "hillshade" ), QObject::tr( "Hillshade" ),
62  QgsHillshadeRenderer::create, nullptr ) );
63  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "contour" ), QObject::tr( "Contours" ),
65 }
66 
68 {
69  mEntries.insert( entry.name, entry );
70  mSortedEntries.append( entry.name );
71 }
72 
74 {
75  if ( !mEntries.contains( rendererName ) )
76  {
77  return;
78  }
79  mEntries[rendererName].widgetCreateFunction = func;
80 }
81 
82 bool QgsRasterRendererRegistry::rendererData( const QString &rendererName, QgsRasterRendererRegistryEntry &data ) const
83 {
84  const QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.find( rendererName );
85  if ( it == mEntries.constEnd() )
86  {
87  return false;
88  }
89  data = it.value();
90  return true;
91 }
92 
94 {
95  return mSortedEntries;
96 }
97 
98 QList< QgsRasterRendererRegistryEntry > QgsRasterRendererRegistry::entries() const
99 {
100  QList< QgsRasterRendererRegistryEntry > result;
101 
102  QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constBegin();
103  for ( ; it != mEntries.constEnd(); ++it )
104  {
105  result.push_back( it.value() );
106  }
107  return result;
108 }
109 
111 {
112  if ( !provider || provider->bandCount() < 1 )
113  {
114  return nullptr;
115  }
116 
117 
118  QgsRasterRenderer *renderer = nullptr;
119  switch ( drawingStyle )
120  {
122  {
123  const int grayBand = 1; //reasonable default
125  renderer = new QgsPalettedRasterRenderer( provider,
126  grayBand,
127  classes );
128  }
129  break;
132  {
133  const int grayBand = 1;
134  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
135 
137  provider->dataType( grayBand ) ) );
138 
139 // 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).
140  ( ( QgsSingleBandGrayRenderer * )renderer )->setContrastEnhancement( ce );
141  break;
142  }
144  {
145  const int bandNo = 1;
146  double minValue = 0;
147  double maxValue = 0;
148  // TODO: avoid calculating statistics if not necessary (default style loaded)
149  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
150  QgsRasterShader *shader = new QgsRasterShader( minValue, maxValue );
151  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
152  break;
153  }
155  {
156  const QgsSettings s;
157 
158  int redBand = s.value( QStringLiteral( "/Raster/defaultRedBand" ), 1 ).toInt();
159  if ( redBand < 0 || redBand > provider->bandCount() )
160  {
161  redBand = -1;
162  }
163  int greenBand = s.value( QStringLiteral( "/Raster/defaultGreenBand" ), 2 ).toInt();
164  if ( greenBand < 0 || greenBand > provider->bandCount() )
165  {
166  greenBand = -1;
167  }
168  int blueBand = s.value( QStringLiteral( "/Raster/defaultBlueBand" ), 3 ).toInt();
169  if ( blueBand < 0 || blueBand > provider->bandCount() )
170  {
171  blueBand = -1;
172  }
173 
174  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
175  break;
176  }
178  {
179  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
180  break;
181  }
182  default:
183  return nullptr;
184  }
185 
186  QgsRasterTransparency *tr = new QgsRasterTransparency(); //renderer takes ownership
187  const int bandCount = renderer->usesBands().size();
188  if ( bandCount == 1 )
189  {
190  const QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
191  tr->setTransparentSingleValuePixelList( transparentSingleList );
192  }
193  else if ( bandCount == 3 )
194  {
195  const QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
196  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
197  }
198  renderer->setRasterTransparency( tr );
199  return renderer;
200 }
201 
202 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider *provider, double &minValue, double &maxValue ) const
203 {
204  if ( !provider )
205  {
206  return false;
207  }
208 
209  minValue = 0;
210  maxValue = 0;
211 
212  const QgsSettings s;
213  if ( s.value( QStringLiteral( "/Raster/useStandardDeviation" ), false ).toBool() )
214  {
216 
217  const double stdDevFactor = s.value( QStringLiteral( "/Raster/defaultStandardDeviation" ), 2.0 ).toDouble();
218  const double diff = stdDevFactor * stats.stdDev;
219  minValue = stats.mean - diff;
220  maxValue = stats.mean + diff;
221  }
222  else
223  {
225  minValue = stats.minimumValue;
226  maxValue = stats.maximumValue;
227  }
228  return true;
229 }
DataType
Raster data types.
Definition: qgis.h:121
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Factory method to create a new renderer.
Renderer for multiband images with the color components.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Renderer for paletted raster images.
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of 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.
The RasterBandStats struct is a container for statistics about a single raster band.
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.
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
virtual int bandCount() const =0
Gets number of bands.
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
void insertWidgetFunction(const QString &rendererName, QgsRasterRendererWidgetCreateFunc func)
QgsRasterRenderer * defaultRendererForDrawingStyle(QgsRaster::DrawingStyle drawingStyle, QgsRasterDataProvider *provider) const
Creates a default renderer for a raster drawing style (considering user options such as default contr...
QList< QgsRasterRendererRegistryEntry > entries() const
void insert(const QgsRasterRendererRegistryEntry &entry)
bool rendererData(const QString &rendererName, QgsRasterRendererRegistryEntry &data) const
Raster renderer pipe that applies colors to a raster.
void setRasterTransparency(QgsRasterTransparency *t)
virtual QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
Interface for all raster shaders.
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
void setTransparentSingleValuePixelList(const QList< QgsRasterTransparency::TransparentSingleValuePixel > &newList)
Sets the transparent single value pixel list, replacing the whole existing list.
void setTransparentThreeValuePixelList(const QList< QgsRasterTransparency::TransparentThreeValuePixel > &newList)
Sets the transparent three value pixel list, replacing the whole existing list.
DrawingStyle
This enumerator describes the different kinds of drawing we can do.
Definition: qgsraster.h:90
@ SingleBandColorDataStyle
Definition: qgsraster.h:101
@ MultiBandColor
Definition: qgsraster.h:100
@ PalettedColor
Definition: qgsraster.h:94
@ SingleBandPseudoColor
Definition: qgsraster.h:93
@ SingleBandGray
Definition: qgsraster.h:92
@ MultiBandSingleBandGray
Definition: qgsraster.h:98
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Raster renderer pipe for single band color.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Raster renderer pipe for single band gray.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Raster renderer pipe for single band pseudocolor.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
QgsRasterRenderer *(* QgsRasterRendererCreateFunc)(const QDomElement &, QgsRasterInterface *input)
QgsRasterRendererWidget *(* QgsRasterRendererWidgetCreateFunc)(QgsRasterLayer *, const QgsRectangle &extent)
Registry for raster renderer entries.
QgsRasterRendererRegistryEntry()=default
Constructor for QgsRasterRendererRegistryEntry.