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