QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
24 #include "qgshillshaderenderer.h"
25 #include "qgsapplication.h"
26 
27 #include <QSettings>
28 #include <QIcon>
29 
31  QgsRasterRendererCreateFunc rendererFunction,
32  QgsRasterRendererWidgetCreateFunc widgetFunction )
33  : name( theName )
34  , visibleName( theVisibleName )
35  , rendererCreateFunction( rendererFunction )
36  , widgetCreateFunction( widgetFunction )
37 {
38 }
39 
41 {
42 }
43 
45 {
46  return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
47 }
48 
50 {
51  static QgsRasterRendererRegistry mInstance;
52  return &mInstance;
53 }
54 
56 {
57  // insert items in a particular order, which is returned in renderersList()
58  insert( QgsRasterRendererRegistryEntry( "multibandcolor", QObject::tr( "Multiband color" ),
60  insert( QgsRasterRendererRegistryEntry( "paletted", QObject::tr( "Paletted" ), QgsPalettedRasterRenderer::create, nullptr ) );
61  insert( QgsRasterRendererRegistryEntry( "singlebandgray", QObject::tr( "Singleband gray" ),
63  insert( QgsRasterRendererRegistryEntry( "singlebandpseudocolor", QObject::tr( "Singleband pseudocolor" ),
65  insert( QgsRasterRendererRegistryEntry( "singlebandcolordata", QObject::tr( "Singleband color data" ),
67  insert( QgsRasterRendererRegistryEntry( "hillshade", QObject::tr( "Hillshade" ),
68  QgsHillshadeRenderer::create, nullptr ) );
69 }
70 
72 {
73  mEntries.insert( entry.name, entry );
74  mSortedEntries.append( entry.name );
75 }
76 
78 {
79  if ( !mEntries.contains( rendererName ) )
80  {
81  return;
82  }
83  mEntries[rendererName].widgetCreateFunction = func;
84 }
85 
87 {
89  if ( it == mEntries.constEnd() )
90  {
91  return false;
92  }
93  data = it.value();
94  return true;
95 }
96 
98 {
99  return mSortedEntries;
100 }
101 
103 {
105 
107  for ( ; it != mEntries.constEnd(); ++it )
108  {
109  result.push_back( it.value() );
110  }
111  return result;
112 }
113 
115 {
116  if ( !provider || provider->bandCount() < 1 )
117  {
118  return nullptr;
119  }
120 
121 
122  QgsRasterRenderer* renderer = nullptr;
123  switch ( theDrawingStyle )
124  {
126  {
127  int grayBand = 1; //reasonable default
128  QList<QgsColorRampShader::ColorRampItem> colorEntries = provider->colorTable( grayBand );
129 
130  //go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
131  int colorArraySize = 0;
133  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
134  {
135  if ( colorIt->value > colorArraySize )
136  {
137  colorArraySize = ( int )( colorIt->value );
138  }
139  }
140 
141  colorArraySize += 1; //usually starts at 0
142  QColor* colorArray = new QColor[ colorArraySize ];
143  colorIt = colorEntries.constBegin();
144  QVector<QString> labels;
145  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
146  {
147  int idx = ( int )( colorIt->value );
148  colorArray[idx] = colorIt->color;
149  if ( !colorIt->label.isEmpty() )
150  {
151  if ( labels.size() <= idx ) labels.resize( idx + 1 );
152  labels[idx] = colorIt->label;
153  }
154  }
155 
156  renderer = new QgsPalettedRasterRenderer( provider,
157  grayBand,
158  colorArray,
159  colorArraySize,
160  labels );
161  }
162  break;
165  {
166  int grayBand = 1;
167  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
168 
170  provider->dataType( grayBand ) ) );
171 
172 // 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).
173  (( QgsSingleBandGrayRenderer* )renderer )->setContrastEnhancement( ce );
174  break;
175  }
177  {
178  int bandNo = 1;
179  double minValue = 0;
180  double maxValue = 0;
181  // TODO: avoid calculating statistics if not necessary (default style loaded)
182  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
183  QgsRasterShader* shader = new QgsRasterShader( minValue, maxValue );
184  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
185  break;
186  }
188  {
189  QSettings s;
190 
191  int redBand = s.value( "/Raster/defaultRedBand", 1 ).toInt();
192  if ( redBand < 0 || redBand > provider->bandCount() )
193  {
194  redBand = -1;
195  }
196  int greenBand = s.value( "/Raster/defaultGreenBand", 2 ).toInt();
197  if ( greenBand < 0 || greenBand > provider->bandCount() )
198  {
199  greenBand = -1;
200  }
201  int blueBand = s.value( "/Raster/defaultBlueBand", 3 ).toInt();
202  if ( blueBand < 0 || blueBand > provider->bandCount() )
203  {
204  blueBand = -1;
205  }
206 
207  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
208  break;
209  }
211  {
212  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
213  break;
214  }
215  default:
216  return nullptr;
217  }
218 
219  QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
220  int bandCount = renderer->usesBands().size();
221  if ( bandCount == 1 )
222  {
224  tr->setTransparentSingleValuePixelList( transparentSingleList );
225  }
226  else if ( bandCount == 3 )
227  {
229  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
230  }
231  renderer->setRasterTransparency( tr );
232  return renderer;
233 }
234 
235 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider* provider, double& minValue, double& maxValue ) const
236 {
237  if ( !provider )
238  {
239  return false;
240  }
241 
242  minValue = 0;
243  maxValue = 0;
244 
245  QSettings s;
246  if ( s.value( "/Raster/useStandardDeviation", false ).toBool() )
247  {
249 
250  double stdDevFactor = s.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble();
251  double diff = stdDevFactor * stats.stdDev;
252  minValue = stats.mean - diff;
253  maxValue = stats.mean + diff;
254  }
255  else
256  {
258  minValue = stats.minimumValue;
259  maxValue = stats.maximumValue;
260  }
261  return true;
262 }
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.
void push_back(const T &value)
DrawingStyle
This enumerator describes the different kinds of drawing we can do.
Definition: qgsraster.h:95
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
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
QgsRasterRenderer *(* QgsRasterRendererCreateFunc)(const QDomElement &, QgsRasterInterface *input)
double maximumValue
The maximum cell value in the raster band.
void setTransparentThreeValuePixelList(const QList< TransparentThreeValuePixel > &theNewList)
Mutator for transparentThreeValuePixelList.
Registry for raster renderers.
QString tr(const char *sourceText, const char *disambiguation, int n)
int size() const
Raster renderer pipe for single band color.
bool rendererData(const QString &rendererName, QgsRasterRendererRegistryEntry &data) const
virtual QgsRasterBandStats bandStatistics(int theBandNo, int theStats=QgsRasterBandStats::All, const QgsRectangle &theExtent=QgsRectangle(), int theSampleSize=0)
Get band statistics.
double stdDev
The standard deviation of the cell values.
The RasterBandStats struct is a container for statistics about a single raster band.
static QgsRasterRendererRegistry * instance()
double mean
The mean cell value for the band.
void resize(int size)
const_iterator constEnd() const
int toInt(bool *ok) const
void insert(const QgsRasterRendererRegistryEntry &entry)
Raster renderer pipe for single band pseudocolor.
Raster renderer pipe for single band gray.
QgsRasterRenderer * defaultRendererForDrawingStyle(QgsRaster::DrawingStyle theDrawingStyle, QgsRasterDataProvider *provider) const
Creates a default renderer for a raster drawing style (considering user options such as default contr...
QgsRasterRendererWidget *(* QgsRasterRendererWidgetCreateFunc)(QgsRasterLayer *, const QgsRectangle &extent)
const T value(const Key &key) const
iterator find(const Key &key)
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Registry for raster renderer entries.
virtual QGis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
QVariant value(const QString &key, const QVariant &defaultValue) const
const_iterator constBegin() const
void setTransparentSingleValuePixelList(const QList< TransparentSingleValuePixel > &theNewList)
Mutator for transparentSingleValuePixelList.
DataType
Raster data types.
Definition: qgis.h:133
bool toBool() const
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...
double toDouble(bool *ok) const
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
QList< QgsRasterRendererRegistryEntry > entries() const
const_iterator constEnd() const
const_iterator constBegin() const
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Factory method to create a new renderer.
int size() const
void setRasterTransparency(QgsRasterTransparency *t)
void insertWidgetFunction(const QString &rendererName, QgsRasterRendererWidgetCreateFunc func)
Raster renderer pipe that applies colors to a raster.
QgsRasterRendererCreateFunc rendererCreateFunction
Base class for raster data providers.