QGIS API Documentation  3.13.0-Master (b73bd58cfb)
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  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  int grayBand = 1; //reasonable default
125  renderer = new QgsPalettedRasterRenderer( provider,
126  grayBand,
127  classes );
128  }
129  break;
132  {
133  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  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  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  int bandCount = renderer->usesBands().size();
188  if ( bandCount == 1 )
189  {
190  QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
191  tr->setTransparentSingleValuePixelList( transparentSingleList );
192  }
193  else if ( bandCount == 3 )
194  {
195  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  QgsSettings s;
213  if ( s.value( QStringLiteral( "/Raster/useStandardDeviation" ), false ).toBool() )
214  {
216 
217  double stdDevFactor = s.value( QStringLiteral( "/Raster/defaultStandardDeviation" ), 2.0 ).toDouble();
218  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 }
virtual int bandCount() const =0
Gets number of bands.
Interface for all raster shaders.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
void setTransparentThreeValuePixelList(const QList< QgsRasterTransparency::TransparentThreeValuePixel > &newList)
Sets the transparent three value pixel list, replacing the whole existing list.
Renderer for paletted raster images.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
DrawingStyle
This enumerator describes the different kinds of drawing we can do.
Definition: qgsraster.h:89
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.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsRasterRenderer *(* QgsRasterRendererCreateFunc)(const QDomElement &, QgsRasterInterface *input)
DataType
Raster data types.
Definition: qgis.h:101
double maximumValue
The maximum cell value in the raster band.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QgsRasterRendererRegistryEntry()=default
Constructor for QgsRasterRendererRegistryEntry.
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.
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of value to class properties.
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)
void setTransparentSingleValuePixelList(const QList< QgsRasterTransparency::TransparentSingleValuePixel > &newList)
Sets the transparent single value pixel list, replacing the whole existing list.
Registry for raster renderer entries.
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Creates an instance of the renderer based on definition from XML (used by renderer registry) ...
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< 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.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
Base class for raster data providers.