QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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"
28 #include "qgshillshaderenderer.h"
29 #include "qgsapplication.h"
30 #include "qgssettings.h"
31 
32 #include <QIcon>
33 
34 QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry( const QString &name, const QString &visibleName,
35  QgsRasterRendererCreateFunc rendererFunction,
36  QgsRasterRendererWidgetCreateFunc widgetFunction )
37  : name( name )
38  , visibleName( visibleName )
39  , rendererCreateFunction( rendererFunction )
40  , widgetCreateFunction( widgetFunction )
41 {
42 }
43 
45 {
46  return QgsApplication::getThemeIcon( QString( "styleicons/%1.svg" ).arg( name ) );
47 }
48 
50 {
51  // insert items in a particular order, which is returned in renderersList()
52  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "multibandcolor" ), QObject::tr( "Multiband color" ),
54  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "paletted" ), QObject::tr( "Paletted/Unique values" ), QgsPalettedRasterRenderer::create, nullptr ) );
55  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandgray" ), QObject::tr( "Singleband gray" ),
57  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandpseudocolor" ), QObject::tr( "Singleband pseudocolor" ),
59  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "singlebandcolordata" ), QObject::tr( "Singleband color data" ),
61  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "hillshade" ), QObject::tr( "Hillshade" ),
62  QgsHillshadeRenderer::create, nullptr ) );
63  insert( QgsRasterRendererRegistryEntry( QStringLiteral( "contour" ), QObject::tr( "Contours" ),
65 }
66 
68 {
69  mEntries.insert( entry.name, entry );
70  mSortedEntries.append( entry.name );
71 }
72 
74 {
75  if ( !mEntries.contains( rendererName ) )
76  {
77  return;
78  }
79  mEntries[rendererName].widgetCreateFunction = func;
80 }
81 
82 bool QgsRasterRendererRegistry::rendererData( const QString &rendererName, QgsRasterRendererRegistryEntry &data ) const
83 {
84  QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.find( rendererName );
85  if ( it == mEntries.constEnd() )
86  {
87  return false;
88  }
89  data = it.value();
90  return true;
91 }
92 
94 {
95  return mSortedEntries;
96 }
97 
98 QList< QgsRasterRendererRegistryEntry > QgsRasterRendererRegistry::entries() const
99 {
100  QList< QgsRasterRendererRegistryEntry > result;
101 
102  QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constBegin();
103  for ( ; it != mEntries.constEnd(); ++it )
104  {
105  result.push_back( it.value() );
106  }
107  return result;
108 }
109 
111 {
112  if ( !provider || provider->bandCount() < 1 )
113  {
114  return nullptr;
115  }
116 
117 
118  QgsRasterRenderer *renderer = nullptr;
119  switch ( drawingStyle )
120  {
122  {
123  int grayBand = 1; //reasonable default
125  renderer = new QgsPalettedRasterRenderer( provider,
126  grayBand,
127  classes );
128  }
129  break;
132  {
133  int grayBand = 1;
134  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
135 
137  provider->dataType( grayBand ) ) );
138 
139 // 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).
140  ( ( QgsSingleBandGrayRenderer * )renderer )->setContrastEnhancement( ce );
141  break;
142  }
144  {
145  int bandNo = 1;
146  double minValue = 0;
147  double maxValue = 0;
148  // TODO: avoid calculating statistics if not necessary (default style loaded)
149  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
150  QgsRasterShader *shader = new QgsRasterShader( minValue, maxValue );
151  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
152  break;
153  }
155  {
156  QgsSettings s;
157 
158  int redBand = s.value( QStringLiteral( "/Raster/defaultRedBand" ), 1 ).toInt();
159  if ( redBand < 0 || redBand > provider->bandCount() )
160  {
161  redBand = -1;
162  }
163  int greenBand = s.value( QStringLiteral( "/Raster/defaultGreenBand" ), 2 ).toInt();
164  if ( greenBand < 0 || greenBand > provider->bandCount() )
165  {
166  greenBand = -1;
167  }
168  int blueBand = s.value( QStringLiteral( "/Raster/defaultBlueBand" ), 3 ).toInt();
169  if ( blueBand < 0 || blueBand > provider->bandCount() )
170  {
171  blueBand = -1;
172  }
173 
174  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
175  break;
176  }
178  {
179  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
180  break;
181  }
182  default:
183  return nullptr;
184  }
185 
186  QgsRasterTransparency *tr = new QgsRasterTransparency(); //renderer takes ownership
187  int bandCount = renderer->usesBands().size();
188  if ( bandCount == 1 )
189  {
190  QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
191  tr->setTransparentSingleValuePixelList( transparentSingleList );
192  }
193  else if ( bandCount == 3 )
194  {
195  QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
196  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
197  }
198  renderer->setRasterTransparency( tr );
199  return renderer;
200 }
201 
202 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider *provider, double &minValue, double &maxValue ) const
203 {
204  if ( !provider )
205  {
206  return false;
207  }
208 
209  minValue = 0;
210  maxValue = 0;
211 
212  QgsSettings s;
213  if ( s.value( QStringLiteral( "/Raster/useStandardDeviation" ), false ).toBool() )
214  {
216 
217  double stdDevFactor = s.value( QStringLiteral( "/Raster/defaultStandardDeviation" ), 2.0 ).toDouble();
218  double diff = stdDevFactor * stats.stdDev;
219  minValue = stats.mean - diff;
220  maxValue = stats.mean + diff;
221  }
222  else
223  {
225  minValue = stats.minimumValue;
226  maxValue = stats.maximumValue;
227  }
228  return true;
229 }
QgsPalettedRasterRenderer::ClassData
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of value to class properties.
Definition: qgspalettedrasterrenderer.h:59
QgsRasterRendererRegistry::defaultRendererForDrawingStyle
QgsRasterRenderer * defaultRendererForDrawingStyle(QgsRaster::DrawingStyle drawingStyle, QgsRasterDataProvider *provider) const
Creates a default renderer for a raster drawing style (considering user options such as default contr...
Definition: qgsrasterrendererregistry.cpp:110
QgsRasterTransparency::setTransparentThreeValuePixelList
void setTransparentThreeValuePixelList(const QList< QgsRasterTransparency::TransparentThreeValuePixel > &newList)
Sets the transparent three value pixel list, replacing the whole existing list.
Definition: qgsrastertransparency.cpp:69
Qgis::DataType
DataType
Raster data types.
Definition: qgis.h:114
QgsRasterRenderer::setRasterTransparency
void setRasterTransparency(QgsRasterTransparency *t)
Definition: qgsrasterrenderer.cpp:98
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:605
QgsRasterDataProvider::dataType
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsRasterInterface::bandStatistics
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics.
Definition: qgsrasterinterface.cpp:116
QgsRaster::PalettedColor
@ PalettedColor
Definition: qgsraster.h:107
QgsRaster::SingleBandColorDataStyle
@ SingleBandColorDataStyle
Definition: qgsraster.h:114
QgsRasterBandStats
Definition: qgsrasterbandstats.h:34
QgsRaster::SingleBandGray
@ SingleBandGray
Definition: qgsraster.h:105
QgsRasterRendererRegistryEntry::name
QString name
Definition: qgsrasterrendererregistry.h:53
QgsRasterRendererRegistryEntry
Definition: qgsrasterrendererregistry.h:44
QgsRasterBandStats::mean
double mean
The mean cell value for the band. NO_DATA values are excluded.
Definition: qgsrasterbandstats.h:107
QgsRaster::DrawingStyle
DrawingStyle
This enumerator describes the different kinds of drawing we can do.
Definition: qgsraster.h:102
QgsHillshadeRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Factory method to create a new renderer.
Definition: qgshillshaderenderer.cpp:58
QgsSingleBandGrayRenderer
Definition: qgssinglebandgrayrenderer.h:33
QgsRasterRendererCreateFunc
QgsRasterRenderer *(* QgsRasterRendererCreateFunc)(const QDomElement &, QgsRasterInterface *input)
Definition: qgsrasterrendererregistry.h:37
qgssinglebandcolordatarenderer.h
QgsSettings
Definition: qgssettings.h:61
qgsrasterrendererregistry.h
QgsRasterRendererRegistry::insertWidgetFunction
void insertWidgetFunction(const QString &rendererName, QgsRasterRendererWidgetCreateFunc func)
Definition: qgsrasterrendererregistry.cpp:73
QgsRasterContourRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Creates an instance of the renderer based on definition from XML (used by renderer registry)
Definition: qgsrastercontourrenderer.cpp:45
QgsPalettedRasterRenderer::colorTableToClassData
static QgsPalettedRasterRenderer::ClassData colorTableToClassData(const QList< QgsColorRampShader::ColorRampItem > &table)
Converts a raster color table to paletted renderer class data.
Definition: qgspalettedrasterrenderer.cpp:346
qgssinglebandpseudocolorrenderer.h
QgsRasterRenderer::usesBands
virtual QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
Definition: qgsrasterrenderer.h:132
qgsapplication.h
QgsSingleBandColorDataRenderer
Definition: qgssinglebandcolordatarenderer.h:31
QgsRasterBandStats::maximumValue
double maximumValue
The maximum cell value in the raster band.
Definition: qgsrasterbandstats.h:99
QgsPalettedRasterRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Definition: qgspalettedrasterrenderer.cpp:49
qgshillshaderenderer.h
qgspalettedrasterrenderer.h
QgsRasterRenderer
Definition: qgsrasterrenderer.h:38
QgsRasterRendererRegistryEntry::icon
QIcon icon()
Definition: qgsrasterrendererregistry.cpp:44
QgsRasterBandStats::StdDev
@ StdDev
Definition: qgsrasterbandstats.h:71
QgsRasterRendererRegistry::QgsRasterRendererRegistry
QgsRasterRendererRegistry()
Definition: qgsrasterrendererregistry.cpp:49
QgsRasterBandStats::Min
@ Min
Definition: qgsrasterbandstats.h:66
QgsRasterTransparency::setTransparentSingleValuePixelList
void setTransparentSingleValuePixelList(const QList< QgsRasterTransparency::TransparentSingleValuePixel > &newList)
Sets the transparent single value pixel list, replacing the whole existing list.
Definition: qgsrastertransparency.cpp:64
QgsRasterRendererRegistry::entries
QList< QgsRasterRendererRegistryEntry > entries() const
Definition: qgsrasterrendererregistry.cpp:98
QgsSingleBandGrayRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Definition: qgssinglebandgrayrenderer.cpp:48
QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry
QgsRasterRendererRegistryEntry()=default
Constructor for QgsRasterRendererRegistryEntry.
QgsRasterDataProvider::colorTable
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
Definition: qgsrasterdataprovider.h:259
QgsMultiBandColorRenderer
Definition: qgsmultibandcolorrenderer.h:32
QgsRasterRendererRegistry::insert
void insert(const QgsRasterRendererRegistryEntry &entry)
Definition: qgsrasterrendererregistry.cpp:67
QgsPalettedRasterRenderer
Definition: qgspalettedrasterrenderer.h:35
QgsRasterBandStats::Mean
@ Mean
Definition: qgsrasterbandstats.h:70
QgsRasterTransparency
Definition: qgsrastertransparency.h:32
qgsrastertransparency.h
QgsContrastEnhancement
Definition: qgscontrastenhancement.h:42
qgssinglebandgrayrenderer.h
qgsrastershader.h
QgsRasterRendererWidgetCreateFunc
QgsRasterRendererWidget *(* QgsRasterRendererWidgetCreateFunc)(QgsRasterLayer *, const QgsRectangle &extent)
Definition: qgsrasterrendererregistry.h:38
QgsRasterBandStats::minimumValue
double minimumValue
The minimum cell value in the raster band.
Definition: qgsrasterbandstats.h:104
QgsMultiBandColorRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Definition: qgsmultibandcolorrenderer.cpp:88
QgsRasterBandStats::Max
@ Max
Definition: qgsrasterbandstats.h:67
QgsRasterRendererRegistry::rendererData
bool rendererData(const QString &rendererName, QgsRasterRendererRegistryEntry &data) const
Definition: qgsrasterrendererregistry.cpp:82
QgsRaster::SingleBandPseudoColor
@ SingleBandPseudoColor
Definition: qgsraster.h:106
qgsmultibandcolorrenderer.h
qgssettings.h
qgsrastercontourrenderer.h
QgsRaster::MultiBandColor
@ MultiBandColor
Definition: qgsraster.h:113
QgsRasterBandStats::stdDev
double stdDev
The standard deviation of the cell values.
Definition: qgsrasterbandstats.h:113
QgsSingleBandColorDataRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Definition: qgssinglebandcolordatarenderer.cpp:39
QgsSingleBandPseudoColorRenderer
Definition: qgssinglebandpseudocolorrenderer.h:35
QgsRasterInterface::bandCount
virtual int bandCount() const =0
Gets number of bands.
QgsSingleBandPseudoColorRenderer::create
static QgsRasterRenderer * create(const QDomElement &elem, QgsRasterInterface *input)
Definition: qgssinglebandpseudocolorrenderer.cpp:132
QgsRasterShader
Definition: qgsrastershader.h:34
QgsRaster::MultiBandSingleBandGray
@ MultiBandSingleBandGray
Definition: qgsraster.h:111
QgsRasterDataProvider
Definition: qgsrasterdataprovider.h:88
QgsRasterRendererRegistry::renderersList
QStringList renderersList() const
Definition: qgsrasterrendererregistry.cpp:93
qgsrasterdataprovider.h