QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
26 QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry( const QString& theName, const QString& theVisibleName,
27  QgsRasterRendererCreateFunc rendererFunction,
28  QgsRasterRendererWidgetCreateFunc widgetFunction ):
29  name( theName ), visibleName( theVisibleName ), rendererCreateFunction( rendererFunction ),
30  widgetCreateFunction( widgetFunction )
31 {
32 }
33 
34 QgsRasterRendererRegistryEntry::QgsRasterRendererRegistryEntry(): rendererCreateFunction( 0 ), widgetCreateFunction( 0 )
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" ),
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 }
61 
63 {
64  mEntries.insert( entry.name, entry );
65  mSortedEntries.append( entry.name );
66 }
67 
69 {
70  if ( !mEntries.contains( rendererName ) )
71  {
72  return;
73  }
74  mEntries[rendererName].widgetCreateFunction = func;
75 }
76 
77 bool QgsRasterRendererRegistry::rendererData( const QString& rendererName, QgsRasterRendererRegistryEntry& data ) const
78 {
79  QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.find( rendererName );
80  if ( it == mEntries.constEnd() )
81  {
82  return false;
83  }
84  data = it.value();
85  return true;
86 }
87 
89 {
90  return mSortedEntries;
91 }
92 
93 QList< QgsRasterRendererRegistryEntry > QgsRasterRendererRegistry::entries() const
94 {
95  QList< QgsRasterRendererRegistryEntry > result;
96 
97  QHash< QString, QgsRasterRendererRegistryEntry >::const_iterator it = mEntries.constBegin();
98  for ( ; it != mEntries.constEnd(); ++it )
99  {
100  result.push_back( it.value() );
101  }
102  return result;
103 }
104 
106 {
107  if ( !provider || provider->bandCount() < 1 )
108  {
109  return 0;
110  }
111 
112 
113  QgsRasterRenderer* renderer = 0;
114  switch ( theDrawingStyle )
115  {
117  {
118  int grayBand = 1; //reasonable default
119  QList<QgsColorRampShader::ColorRampItem> colorEntries = provider->colorTable( grayBand );
120 
121  //go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
122  int colorArraySize = 0;
123  QList<QgsColorRampShader::ColorRampItem>::const_iterator colorIt = colorEntries.constBegin();
124  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
125  {
126  if ( colorIt->value > colorArraySize )
127  {
128  colorArraySize = ( int )( colorIt->value );
129  }
130  }
131 
132  colorArraySize += 1; //usually starts at 0
133  QColor* colorArray = new QColor[ colorArraySize ];
134  colorIt = colorEntries.constBegin();
135  QVector<QString> labels;
136  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
137  {
138  int idx = ( int )( colorIt->value );
139  colorArray[idx] = colorIt->color;
140  if ( !colorIt->label.isEmpty() )
141  {
142  if ( labels.size() <= idx ) labels.resize( idx + 1 );
143  labels[idx] = colorIt->label;
144  }
145  }
146 
147  renderer = new QgsPalettedRasterRenderer( provider,
148  grayBand,
149  colorArray,
150  colorArraySize,
151  labels );
152  }
153  break;
156  {
157  int grayBand = 1;
158  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
159 
161  provider->dataType( grayBand ) ) );
162 
163 // 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).
164  (( QgsSingleBandGrayRenderer* )renderer )->setContrastEnhancement( ce );
165  break;
166  }
168  {
169  int bandNo = 1;
170  double minValue = 0;
171  double maxValue = 0;
172  // TODO: avoid calculating statistics if not necessary (default style loaded)
173  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
174  QgsRasterShader* shader = new QgsRasterShader( minValue, maxValue );
175  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
176  break;
177  }
179  {
180  QSettings s;
181 
182  int redBand = s.value( "/Raster/defaultRedBand", 1 ).toInt();
183  if ( redBand < 0 || redBand > provider->bandCount() )
184  {
185  redBand = -1;
186  }
187  int greenBand = s.value( "/Raster/defaultGreenBand", 2 ).toInt();
188  if ( greenBand < 0 || greenBand > provider->bandCount() )
189  {
190  greenBand = -1;
191  }
192  int blueBand = s.value( "/Raster/defaultBlueBand", 3 ).toInt();
193  if ( blueBand < 0 || blueBand > provider->bandCount() )
194  {
195  blueBand = -1;
196  }
197 
198  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
199  break;
200  }
202  {
203  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
204  break;
205  }
206  default:
207  return 0;
208  }
209 
210  QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
211  int bandCount = renderer->usesBands().size();
212  if ( bandCount == 1 )
213  {
214  QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
215  tr->setTransparentSingleValuePixelList( transparentSingleList );
216  }
217  else if ( bandCount == 3 )
218  {
219  QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
220  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
221  }
222  renderer->setRasterTransparency( tr );
223  return renderer;
224 }
225 
226 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider* provider, double& minValue, double& maxValue ) const
227 {
228  if ( !provider )
229  {
230  return false;
231  }
232 
233  minValue = 0;
234  maxValue = 0;
235 
236  QSettings s;
237  if ( s.value( "/Raster/useStandardDeviation", false ).toBool() )
238  {
240 
241  double stdDevFactor = s.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble();
242  double diff = stdDevFactor * stats.stdDev;
243  minValue = stats.mean - diff;
244  maxValue = stats.mean + diff;
245  }
246  else
247  {
249  minValue = stats.minimumValue;
250  maxValue = stats.maximumValue;
251  }
252  return true;
253 }