QGIS API Documentation  3.0.2-Girona (307d082)
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"
27 #include "qgshillshaderenderer.h"
28 #include "qgsapplication.h"
29 #include "qgssettings.h"
30 
31 #include <QIcon>
32 
33 QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry( const QString &name, const QString &visibleName,
34  QgsRasterRendererCreateFunc rendererFunction,
35  QgsRasterRendererWidgetCreateFunc widgetFunction )
36  : name( name )
37  , visibleName( visibleName )
38  , rendererCreateFunction( rendererFunction )
39  , widgetCreateFunction( widgetFunction )
40 {
41 }
42 
44 {
45  return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
46 }
47 
49 {
50  // insert items in a particular order, which is returned in renderersList()
51  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "multibandcolor" ), QObject::tr( "Multiband color" ),
53  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "paletted" ), QObject::tr( "Paletted/Unique values" ), QgsPalettedRasterRenderer::create, nullptr ) );
54  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandgray" ), QObject::tr( "Singleband gray" ),
56  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandpseudocolor" ), QObject::tr( "Singleband pseudocolor" ),
58  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandcolordata" ), QObject::tr( "Singleband color data" ),
60  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "hillshade" ), QObject::tr( "Hillshade" ),
61  QgsHillshadeRenderer::create, nullptr ) );
62 }
63 
65 {
66  mEntries.insert( entry.name, entry );
67  mSortedEntries.append( entry.name );
68 }
69 
71 {
72  if ( !mEntries.contains( rendererName ) )
73  {
74  return;
75  }
76  mEntries[rendererName].widgetCreateFunction = func;
77 }
78 
79 bool QgsRasterRendererRegistry::rendererData( const QString &rendererName, QgsRasterRendererRegistryEntry &data ) const
80 {
81  QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.find( rendererName );
82  if ( it == mEntries.constEnd() )
83  {
84  return false;
85  }
86  data = it.value();
87  return true;
88 }
89 
91 {
92  return mSortedEntries;
93 }
94 
95 QList< QgsRasterRendererRegistryEntry > QgsRasterRendererRegistry::entries() const
96 {
97  QList< QgsRasterRendererRegistryEntry > result;
98 
99  QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constBegin();
100  for ( ; it != mEntries.constEnd(); ++it )
101  {
102  result.push_back( it.value() );
103  }
104  return result;
105 }
106 
108 {
109  if ( !provider || provider->bandCount() < 1 )
110  {
111  return nullptr;
112  }
113 
114 
115  QgsRasterRenderer *renderer = nullptr;
116  switch ( drawingStyle )
117  {
119  {
120  int grayBand = 1; //reasonable default
122  renderer = new QgsPalettedRasterRenderer( provider,
123  grayBand,
124  classes );
125  }
126  break;
129  {
130  int grayBand = 1;
131  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
132 
134  provider->dataType( grayBand ) ) );
135 
136 // 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).
137  ( ( QgsSingleBandGrayRenderer * )renderer )->setContrastEnhancement( ce );
138  break;
139  }
141  {
142  int bandNo = 1;
143  double minValue = 0;
144  double maxValue = 0;
145  // TODO: avoid calculating statistics if not necessary (default style loaded)
146  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
147  QgsRasterShader *shader = new QgsRasterShader( minValue, maxValue );
148  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
149  break;
150  }
152  {
153  QgsSettings s;
154 
155  int redBand = s.value( QStringLiteral( "/Raster/defaultRedBand" ), 1 ).toInt();
156  if ( redBand < 0 || redBand > provider->bandCount() )
157  {
158  redBand = -1;
159  }
160  int greenBand = s.value( QStringLiteral( "/Raster/defaultGreenBand" ), 2 ).toInt();
161  if ( greenBand < 0 || greenBand > provider->bandCount() )
162  {
163  greenBand = -1;
164  }
165  int blueBand = s.value( QStringLiteral( "/Raster/defaultBlueBand" ), 3 ).toInt();
166  if ( blueBand < 0 || blueBand > provider->bandCount() )
167  {
168  blueBand = -1;
169  }
170 
171  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
172  break;
173  }
175  {
176  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
177  break;
178  }
179  default:
180  return nullptr;
181  }
182 
183  QgsRasterTransparency *tr = new QgsRasterTransparency(); //renderer takes ownership
184  int bandCount = renderer->usesBands().size();
185  if ( bandCount == 1 )
186  {
187  QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
188  tr->setTransparentSingleValuePixelList( transparentSingleList );
189  }
190  else if ( bandCount == 3 )
191  {
192  QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
193  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
194  }
195  renderer->setRasterTransparency( tr );
196  return renderer;
197 }
198 
199 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider *provider, double &minValue, double &maxValue ) const
200 {
201  if ( !provider )
202  {
203  return false;
204  }
205 
206  minValue = 0;
207  maxValue = 0;
208 
209  QgsSettings s;
210  if ( s.value( QStringLiteral( "/Raster/useStandardDeviation" ), false ).toBool() )
211  {
213 
214  double stdDevFactor = s.value( QStringLiteral( "/Raster/defaultStandardDeviation" ), 2.0 ).toDouble();
215  double diff = stdDevFactor * stats.stdDev;
216  minValue = stats.mean - diff;
217  maxValue = stats.mean + diff;
218  }
219  else
220  {
222  minValue = stats.minimumValue;
223  maxValue = stats.maximumValue;
224  }
225  return true;
226 }
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)
void setTransparentThreeValuePixelList(const QList< QgsRasterTransparency::TransparentThreeValuePixel > &newList)
Mutator for transparentThreeValuePixelList.
Renderer for paletted raster images.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
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.
QgsRasterRenderer *(* QgsRasterRendererCreateFunc)(const QDomElement &, QgsRasterInterface *input)
DataType
Raster data types.
Definition: qgis.h:91
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)
Mutator for transparentSingleValuePixelList.
Registry for raster renderer entries.
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Get band statistics.
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
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.