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