QGIS API Documentation  2.15.0-Master (972fc9f)
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 ), visibleName( theVisibleName ), rendererCreateFunction( rendererFunction ),
34  widgetCreateFunction( widgetFunction )
35 {
36 }
37 
39 {
40 }
41 
43 {
44  return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
45 }
46 
48 {
49  static QgsRasterRendererRegistry mInstance;
50  return &mInstance;
51 }
52 
54 {
55  // insert items in a particular order, which is returned in renderersList()
56  insert( QgsRasterRendererRegistryEntry( "multibandcolor", QObject::tr( "Multiband color" ),
58  insert( QgsRasterRendererRegistryEntry( "paletted", QObject::tr( "Paletted" ), QgsPalettedRasterRenderer::create, nullptr ) );
59  insert( QgsRasterRendererRegistryEntry( "singlebandgray", QObject::tr( "Singleband gray" ),
61  insert( QgsRasterRendererRegistryEntry( "singlebandpseudocolor", QObject::tr( "Singleband pseudocolor" ),
63  insert( QgsRasterRendererRegistryEntry( "singlebandcolordata", QObject::tr( "Singleband color data" ),
65  insert( QgsRasterRendererRegistryEntry( "hillshade", QObject::tr( "Hillshade renderer" ),
66  QgsHillshadeRenderer::create, nullptr ) );
67 }
68 
70 {
71  mEntries.insert( entry.name, entry );
72  mSortedEntries.append( entry.name );
73 }
74 
76 {
77  if ( !mEntries.contains( rendererName ) )
78  {
79  return;
80  }
81  mEntries[rendererName].widgetCreateFunction = func;
82 }
83 
85 {
87  if ( it == mEntries.constEnd() )
88  {
89  return false;
90  }
91  data = it.value();
92  return true;
93 }
94 
96 {
97  return mSortedEntries;
98 }
99 
101 {
103 
105  for ( ; it != mEntries.constEnd(); ++it )
106  {
107  result.push_back( it.value() );
108  }
109  return result;
110 }
111 
113 {
114  if ( !provider || provider->bandCount() < 1 )
115  {
116  return nullptr;
117  }
118 
119 
120  QgsRasterRenderer* renderer = nullptr;
121  switch ( theDrawingStyle )
122  {
124  {
125  int grayBand = 1; //reasonable default
126  QList<QgsColorRampShader::ColorRampItem> colorEntries = provider->colorTable( grayBand );
127 
128  //go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
129  int colorArraySize = 0;
131  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
132  {
133  if ( colorIt->value > colorArraySize )
134  {
135  colorArraySize = ( int )( colorIt->value );
136  }
137  }
138 
139  colorArraySize += 1; //usually starts at 0
140  QColor* colorArray = new QColor[ colorArraySize ];
141  colorIt = colorEntries.constBegin();
142  QVector<QString> labels;
143  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
144  {
145  int idx = ( int )( colorIt->value );
146  colorArray[idx] = colorIt->color;
147  if ( !colorIt->label.isEmpty() )
148  {
149  if ( labels.size() <= idx ) labels.resize( idx + 1 );
150  labels[idx] = colorIt->label;
151  }
152  }
153 
154  renderer = new QgsPalettedRasterRenderer( provider,
155  grayBand,
156  colorArray,
157  colorArraySize,
158  labels );
159  }
160  break;
163  {
164  int grayBand = 1;
165  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
166 
168  provider->dataType( grayBand ) ) );
169 
170 // 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).
171  (( QgsSingleBandGrayRenderer* )renderer )->setContrastEnhancement( ce );
172  break;
173  }
175  {
176  int bandNo = 1;
177  double minValue = 0;
178  double maxValue = 0;
179  // TODO: avoid calculating statistics if not necessary (default style loaded)
180  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
181  QgsRasterShader* shader = new QgsRasterShader( minValue, maxValue );
182  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
183  break;
184  }
186  {
187  QSettings s;
188 
189  int redBand = s.value( "/Raster/defaultRedBand", 1 ).toInt();
190  if ( redBand < 0 || redBand > provider->bandCount() )
191  {
192  redBand = -1;
193  }
194  int greenBand = s.value( "/Raster/defaultGreenBand", 2 ).toInt();
195  if ( greenBand < 0 || greenBand > provider->bandCount() )
196  {
197  greenBand = -1;
198  }
199  int blueBand = s.value( "/Raster/defaultBlueBand", 3 ).toInt();
200  if ( blueBand < 0 || blueBand > provider->bandCount() )
201  {
202  blueBand = -1;
203  }
204 
205  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
206  break;
207  }
209  {
210  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
211  break;
212  }
213  default:
214  return nullptr;
215  }
216 
217  QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
218  int bandCount = renderer->usesBands().size();
219  if ( bandCount == 1 )
220  {
222  tr->setTransparentSingleValuePixelList( transparentSingleList );
223  }
224  else if ( bandCount == 3 )
225  {
227  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
228  }
229  renderer->setRasterTransparency( tr );
230  return renderer;
231 }
232 
233 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider* provider, double& minValue, double& maxValue ) const
234 {
235  if ( !provider )
236  {
237  return false;
238  }
239 
240  minValue = 0;
241  maxValue = 0;
242 
243  QSettings s;
244  if ( s.value( "/Raster/useStandardDeviation", false ).toBool() )
245  {
247 
248  double stdDevFactor = s.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble();
249  double diff = stdDevFactor * stats.stdDev;
250  minValue = stats.mean - diff;
251  maxValue = stats.mean + diff;
252  }
253  else
254  {
256  minValue = stats.minimumValue;
257  maxValue = stats.maximumValue;
258  }
259  return true;
260 }
virtual int bandCount() const =0
Get number of bands.
Interface for all raster shaders.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
virtual QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Renderer for paletted raster images.
bool rendererData(const QString &rendererName, QgsRasterRendererRegistryEntry &data) const
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)
QgsRasterRendererWidgetCreateFunc widgetCreateFunction
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
QgsRasterRenderer *(* QgsRasterRendererCreateFunc)(const QDomElement &, QgsRasterInterface *input)
QgsRasterRenderer * defaultRendererForDrawingStyle(QgsRaster::DrawingStyle theDrawingStyle, QgsRasterDataProvider *provider) const
Creates a default renderer for a raster drawing style (considering user options such as default contr...
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.
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.
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...
const_iterator constEnd() const
const_iterator constBegin() const
QList< QgsRasterRendererRegistryEntry > entries() const
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Factory method to create a new renderer.
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
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.