QGIS API Documentation  2.0.1-Dufour
 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 
41 {
42  if ( !mInstance )
43  {
45  }
46  return mInstance;
47 }
48 
50 {
51  // insert items in a particular order, which is returned in renderersList()
52  insert( QgsRasterRendererRegistryEntry( "multibandcolor", QObject::tr( "Multiband color" ),
55  insert( QgsRasterRendererRegistryEntry( "singlebandgray", QObject::tr( "Singleband gray" ),
57  insert( QgsRasterRendererRegistryEntry( "singlebandpseudocolor", QObject::tr( "Singleband pseudocolor" ),
59  insert( QgsRasterRendererRegistryEntry( "singlebandcolordata", QObject::tr( "Singleband color data" ),
61 }
62 
64 {
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 0;
115  }
116 
117 
118  QgsRasterRenderer* renderer = 0;
119  switch ( theDrawingStyle )
120  {
122  {
123  int grayBand = 1; //reasonable default
124  QList<QgsColorRampShader::ColorRampItem> colorEntries = provider->colorTable( grayBand );
125 
126  //go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
127  int colorArraySize = 0;
128  QList<QgsColorRampShader::ColorRampItem>::const_iterator colorIt = colorEntries.constBegin();
129  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
130  {
131  if ( colorIt->value > colorArraySize )
132  {
133  colorArraySize = ( int )( colorIt->value );
134  }
135  }
136 
137  colorArraySize += 1; //usually starts at 0
138  QColor* colorArray = new QColor[ colorArraySize ];
139  colorIt = colorEntries.constBegin();
140  for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
141  {
142  colorArray[( int )( colorIt->value )] = colorIt->color;
143  }
144 
145  renderer = new QgsPalettedRasterRenderer( provider,
146  grayBand,
147  colorArray,
148  colorArraySize );
149  }
150  break;
153  {
154  int grayBand = 1;
155  renderer = new QgsSingleBandGrayRenderer( provider, grayBand );
156 
158  provider->dataType( grayBand ) ) );
159 
160 // 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).
161  (( QgsSingleBandGrayRenderer* )renderer )->setContrastEnhancement( ce );
162  break;
163  }
165  {
166  int bandNo = 1;
167  double minValue = 0;
168  double maxValue = 0;
169  // TODO: avoid calculating statistics if not necessary (default style loaded)
170  minMaxValuesForBand( bandNo, provider, minValue, maxValue );
171  QgsRasterShader* shader = new QgsRasterShader( minValue, maxValue );
172  renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
173  break;
174  }
176  {
177  QSettings s;
178 
179  int redBand = s.value( "/Raster/defaultRedBand", 1 ).toInt();
180  if ( redBand < 0 || redBand > provider->bandCount() )
181  {
182  redBand = -1;
183  }
184  int greenBand = s.value( "/Raster/defaultGreenBand", 2 ).toInt();
185  if ( greenBand < 0 || greenBand > provider->bandCount() )
186  {
187  greenBand = -1;
188  }
189  int blueBand = s.value( "/Raster/defaultBlueBand", 3 ).toInt();
190  if ( blueBand < 0 || blueBand > provider->bandCount() )
191  {
192  blueBand = -1;
193  }
194 
195  renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
196  break;
197  }
199  {
200  renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
201  break;
202  }
203  default:
204  break;
205  }
206 
207  QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
208  int bandCount = renderer->usesBands().size();
209  if ( bandCount == 1 )
210  {
211  QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
212  tr->setTransparentSingleValuePixelList( transparentSingleList );
213  }
214  else if ( bandCount == 3 )
215  {
216  QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
217  tr->setTransparentThreeValuePixelList( transparentThreeValueList );
218  }
219  renderer->setRasterTransparency( tr );
220  return renderer;
221 }
222 
223 bool QgsRasterRendererRegistry::minMaxValuesForBand( int band, QgsRasterDataProvider* provider, double& minValue, double& maxValue ) const
224 {
225  if ( !provider )
226  {
227  return false;
228  }
229 
230  minValue = 0;
231  maxValue = 0;
232 
233  QSettings s;
234  if ( s.value( "/Raster/useStandardDeviation", false ).toBool() )
235  {
237 
238  double stdDevFactor = s.value( "/Raster/defaultStandardDeviation", 2.0 ).toDouble();
239  double diff = stdDevFactor * stats.stdDev;
240  minValue = stats.mean - diff;
241  maxValue = stats.mean + diff;
242  }
243  else
244  {
246  minValue = stats.minimumValue;
247  maxValue = stats.maximumValue;
248  }
249  return true;
250 }