QGIS API Documentation  2.15.0-Master (94d88e6)
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 "qgsapplication.h"
25 
26 #include <QSettings>
27 #include <QIcon>
28 
30  QgsRasterRendererCreateFunc rendererFunction,
31  QgsRasterRendererWidgetCreateFunc widgetFunction ):
32  name( theName ), visibleName( theVisibleName ), rendererCreateFunction( rendererFunction ),
33  widgetCreateFunction( widgetFunction )
34 {
35 }
36 
38 {
39 }
40 
42 {
43  return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
44 }
45 
47 {
48  static QgsRasterRendererRegistry mInstance;
49  return &mInstance;
50 }
51 
53 {
54  // insert items in a particular order, which is returned in renderersList()
55  insert( QgsRasterRendererRegistryEntry( "multibandcolor", QObject::tr( "Multiband color" ),
57  insert( QgsRasterRendererRegistryEntry( "paletted", QObject::tr( "Paletted" ), QgsPalettedRasterRenderer::create, nullptr ) );
58  insert( QgsRasterRendererRegistryEntry( "singlebandgray", QObject::tr( "Singleband gray" ),
60  insert( QgsRasterRendererRegistryEntry( "singlebandpseudocolor", QObject::tr( "Singleband pseudocolor" ),
62  insert( QgsRasterRendererRegistryEntry( "singlebandcolordata", QObject::tr( "Singleband color data" ),
64 }
65 
67 {
68  mEntries.insert( entry.name, entry );
69  mSortedEntries.append( entry.name );
70 }
71 
73 {
74  if ( !mEntries.contains( rendererName ) )
75  {
76  return;
77  }
78  mEntries[rendererName].widgetCreateFunction = func;
79 }
80 
82 {
84  if ( it == mEntries.constEnd() )
85  {
86  return false;
87  }
88  data = it.value();
89  return true;
90 }
91 
93 {
94  return mSortedEntries;
95 }
96 
98 {
100 
102  for ( ; it != mEntries.constEnd(); ++it )
103  {
104  result.push_back( it.value() );
105  }
106  return result;
107 }
108 
110 {
111  if ( !provider || provider->bandCount() < 1 )
112  {
113  return nullptr;
114  }
115 
116 
117  QgsRasterRenderer* renderer = nullptr;
118  switch ( theDrawingStyle )
119  {
121  {
122  int grayBand = 1; //reasonable default
123  QList<QgsColorRampShader::ColorRampItem> colorEntries = provider->colorTable( grayBand );
124 
125  //go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
126  int colorArraySize = 0;
128  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
129  {
130  if ( colorIt->value > colorArraySize )
131  {
132  colorArraySize = ( int )( colorIt->value );
133  }
134  }
135 
136  colorArraySize += 1; //usually starts at 0
137  QColor* colorArray = new QColor[ colorArraySize ];
138  colorIt = colorEntries.constBegin();
139  QVector<QString> labels;
140  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
141  {
142  int idx = ( int )( colorIt->value );
143  colorArray[idx] = colorIt->color;
144  if ( !colorIt->label.isEmpty() )
145  {
146  if ( labels.size() <= idx ) labels.resize( idx + 1 );
147  labels[idx] = colorIt->label;
148  }
149  }
150 
151  renderer = new QgsPalettedRasterRenderer( provider,
152  grayBand,
153  colorArray,
154  colorArraySize,
155  labels );
156  }
157  break;
160  {
161  int grayBand = 1;
162  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
163 
165  provider->dataType( grayBand ) ) );
166 
167 // 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).
168  (( QgsSingleBandGrayRenderer* )renderer )->setContrastEnhancement( ce );
169  break;
170  }
172  {
173  int bandNo = 1;
174  double minValue = 0;
175  double maxValue = 0;
176  // TODO: avoid calculating statistics if not necessary (default style loaded)
177  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
178  QgsRasterShader* shader = new QgsRasterShader( minValue, maxValue );
179  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
180  break;
181  }
183  {
184  QSettings s;
185 
186  int redBand = s.value( "/Raster/defaultRedBand", 1 ).toInt();
187  if ( redBand < 0 || redBand > provider->bandCount() )
188  {
189  redBand = -1;
190  }
191  int greenBand = s.value( "/Raster/defaultGreenBand", 2 ).toInt();
192  if ( greenBand < 0 || greenBand > provider->bandCount() )
193  {
194  greenBand = -1;
195  }
196  int blueBand = s.value( "/Raster/defaultBlueBand", 3 ).toInt();
197  if ( blueBand < 0 || blueBand > provider->bandCount() )
198  {
199  blueBand = -1;
200  }
201 
202  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
203  break;
204  }
206  {
207  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
208  break;
209  }
210  default:
211  return nullptr;
212  }
213 
214  QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
215  int bandCount = renderer->usesBands().size();
216  if ( bandCount == 1 )
217  {
219  tr->setTransparentSingleValuePixelList( transparentSingleList );
220  }
221  else if ( bandCount == 3 )
222  {
224  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
225  }
226  renderer->setRasterTransparency( tr );
227  return renderer;
228 }
229 
230 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider* provider, double& minValue, double& maxValue ) const
231 {
232  if ( !provider )
233  {
234  return false;
235  }
236 
237  minValue = 0;
238  maxValue = 0;
239 
240  QSettings s;
241  if ( s.value( "/Raster/useStandardDeviation", false ).toBool() )
242  {
244 
245  double stdDevFactor = s.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble();
246  double diff = stdDevFactor * stats.stdDev;
247  minValue = stats.mean - diff;
248  maxValue = stats.mean + diff;
249  }
250  else
251  {
253  minValue = stats.minimumValue;
254  maxValue = stats.maximumValue;
255  }
256  return true;
257 }
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
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.
#define tr(sourceText)