QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrelief.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelief.cpp - description
3  ---------------------------
4  begin : November 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler 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 
18 #include "qgsrelief.h"
19 #include "qgsaspectfilter.h"
20 #include "qgshillshadefilter.h"
21 #include "qgsslopefilter.h"
22 #include "qgis.h"
23 #include "cpl_string.h"
24 #include <QProgressDialog>
25 #include <cfloat>
26 
27 #include <QFile>
28 #include <QTextStream>
29 
30 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
31 #define TO8F(x) (x).toUtf8().constData()
32 #else
33 #define TO8F(x) QFile::encodeName( x ).constData()
34 #endif
35 
36 QgsRelief::QgsRelief( const QString& inputFile, const QString& outputFile, const QString& outputFormat )
37  : mInputFile( inputFile )
38  , mOutputFile( outputFile )
39  , mOutputFormat( outputFormat )
40  , mCellSizeX( 0.0 )
41  , mCellSizeY( 0.0 )
42  , mInputNodataValue( -1 )
43  , mOutputNodataValue( -1 )
44  , mZFactor( 1.0 )
45 {
46  mSlopeFilter = new QgsSlopeFilter( inputFile, outputFile, outputFormat );
47  mAspectFilter = new QgsAspectFilter( inputFile, outputFile, outputFormat );
48  mHillshadeFilter285 = new QgsHillshadeFilter( inputFile, outputFile, outputFormat, 285, 30 );
49  mHillshadeFilter300 = new QgsHillshadeFilter( inputFile, outputFile, outputFormat, 300, 30 );
50  mHillshadeFilter315 = new QgsHillshadeFilter( inputFile, outputFile, outputFormat, 315, 30 );
51 
52  /*mReliefColors = calculateOptimizedReliefClasses();
53  setDefaultReliefColors();*/
54 }
55 
57 {
58  delete mSlopeFilter;
59  delete mAspectFilter;
60  delete mHillshadeFilter285;
61  delete mHillshadeFilter300;
62  delete mHillshadeFilter315;
63 }
64 
66 {
67  mReliefColors.clear();
68 }
69 
71 {
72  mReliefColors.push_back( color );
73 }
74 
75 void QgsRelief::setDefaultReliefColors()
76 {
78  addReliefColorClass( ReliefColor( QColor( 9, 176, 76 ), 0, 200 ) );
79  addReliefColorClass( ReliefColor( QColor( 20, 228, 128 ), 200, 500 ) );
80  addReliefColorClass( ReliefColor( QColor( 167, 239, 153 ), 500, 1000 ) );
81  addReliefColorClass( ReliefColor( QColor( 218, 188, 143 ), 1000, 2000 ) );
82  addReliefColorClass( ReliefColor( QColor( 233, 158, 91 ), 2000, 4000 ) );
83  addReliefColorClass( ReliefColor( QColor( 255, 255, 255 ), 4000, 9000 ) );
84 }
85 
86 int QgsRelief::processRaster( QProgressDialog* p )
87 {
88  //open input file
89  int xSize, ySize;
90  GDALDatasetH inputDataset = openInputFile( xSize, ySize );
91  if ( inputDataset == NULL )
92  {
93  return 1; //opening of input file failed
94  }
95 
96  //output driver
97  GDALDriverH outputDriver = openOutputDriver();
98  if ( outputDriver == 0 )
99  {
100  return 2;
101  }
102 
103  GDALDatasetH outputDataset = openOutputFile( inputDataset, outputDriver );
104  if ( outputDataset == NULL )
105  {
106  return 3; //create operation on output file failed
107  }
108 
109  //initialize dependency filters with cell sizes
110  mHillshadeFilter285->setCellSizeX( mCellSizeX );
111  mHillshadeFilter285->setCellSizeY( mCellSizeY );
112  mHillshadeFilter285->setZFactor( mZFactor );
113  mHillshadeFilter300->setCellSizeX( mCellSizeX );
114  mHillshadeFilter300->setCellSizeY( mCellSizeY );
115  mHillshadeFilter300->setZFactor( mZFactor );
116  mHillshadeFilter315->setCellSizeX( mCellSizeX );
117  mHillshadeFilter315->setCellSizeY( mCellSizeY );
118  mHillshadeFilter315->setZFactor( mZFactor );
119  mSlopeFilter->setCellSizeX( mCellSizeX );
120  mSlopeFilter->setCellSizeY( mCellSizeY );
121  mSlopeFilter->setZFactor( mZFactor );
122  mAspectFilter->setCellSizeX( mCellSizeX );
123  mAspectFilter->setCellSizeY( mCellSizeY );
124  mAspectFilter->setZFactor( mZFactor );
125 
126  //open first raster band for reading (operation is only for single band raster)
127  GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset, 1 );
128  if ( rasterBand == NULL )
129  {
130  GDALClose( inputDataset );
131  GDALClose( outputDataset );
132  return 4;
133  }
134  mInputNodataValue = GDALGetRasterNoDataValue( rasterBand, NULL );
135  mSlopeFilter->setInputNodataValue( mInputNodataValue );
136  mAspectFilter->setInputNodataValue( mInputNodataValue );
137  mHillshadeFilter285->setInputNodataValue( mInputNodataValue );
138  mHillshadeFilter300->setInputNodataValue( mInputNodataValue );
139  mHillshadeFilter315->setInputNodataValue( mInputNodataValue );
140 
141  GDALRasterBandH outputRedBand = GDALGetRasterBand( outputDataset, 1 );
142  GDALRasterBandH outputGreenBand = GDALGetRasterBand( outputDataset, 2 );
143  GDALRasterBandH outputBlueBand = GDALGetRasterBand( outputDataset, 3 );
144 
145  if ( outputRedBand == NULL || outputGreenBand == NULL || outputBlueBand == NULL )
146  {
147  GDALClose( inputDataset );
148  GDALClose( outputDataset );
149  return 5;
150  }
151  //try to set -9999 as nodata value
152  GDALSetRasterNoDataValue( outputRedBand, -9999 );
153  GDALSetRasterNoDataValue( outputGreenBand, -9999 );
154  GDALSetRasterNoDataValue( outputBlueBand, -9999 );
155  mOutputNodataValue = GDALGetRasterNoDataValue( outputRedBand, NULL );
156  mSlopeFilter->setOutputNodataValue( mOutputNodataValue );
157  mAspectFilter->setOutputNodataValue( mOutputNodataValue );
158  mHillshadeFilter285->setOutputNodataValue( mOutputNodataValue );
159  mHillshadeFilter300->setOutputNodataValue( mOutputNodataValue );
160  mHillshadeFilter315->setOutputNodataValue( mOutputNodataValue );
161 
162  if ( ySize < 3 ) //we require at least three rows (should be true for most datasets)
163  {
164  GDALClose( inputDataset );
165  GDALClose( outputDataset );
166  return 6;
167  }
168 
169  //keep only three scanlines in memory at a time
170  float* scanLine1 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
171  float* scanLine2 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
172  float* scanLine3 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
173 
174  unsigned char* resultRedLine = ( unsigned char * ) CPLMalloc( sizeof( unsigned char ) * xSize );
175  unsigned char* resultGreenLine = ( unsigned char * ) CPLMalloc( sizeof( unsigned char ) * xSize );
176  unsigned char* resultBlueLine = ( unsigned char * ) CPLMalloc( sizeof( unsigned char ) * xSize );
177 
178  if ( p )
179  {
180  p->setMaximum( ySize );
181  }
182 
183  bool resultOk;
184 
185  //values outside the layer extent (if the 3x3 window is on the border) are sent to the processing method as (input) nodata values
186  for ( int i = 0; i < ySize; ++i )
187  {
188  if ( p )
189  {
190  p->setValue( i );
191  }
192 
193  if ( p && p->wasCanceled() )
194  {
195  break;
196  }
197 
198  if ( i == 0 )
199  {
200  //fill scanline 1 with (input) nodata for the values above the first row and feed scanline2 with the first row
201  for ( int a = 0; a < xSize; ++a )
202  {
203  scanLine1[a] = mInputNodataValue;
204  }
205  GDALRasterIO( rasterBand, GF_Read, 0, 0, xSize, 1, scanLine2, xSize, 1, GDT_Float32, 0, 0 );
206  }
207  else
208  {
209  //normally fetch only scanLine3 and release scanline 1 if we move forward one row
210  CPLFree( scanLine1 );
211  scanLine1 = scanLine2;
212  scanLine2 = scanLine3;
213  scanLine3 = ( float * ) CPLMalloc( sizeof( float ) * xSize );
214  }
215 
216  if ( i == ySize - 1 ) //fill the row below the bottom with nodata values
217  {
218  for ( int a = 0; a < xSize; ++a )
219  {
220  scanLine3[a] = mInputNodataValue;
221  }
222  }
223  else
224  {
225  GDALRasterIO( rasterBand, GF_Read, 0, i + 1, xSize, 1, scanLine3, xSize, 1, GDT_Float32, 0, 0 );
226  }
227 
228  for ( int j = 0; j < xSize; ++j )
229  {
230  if ( j == 0 )
231  {
232  resultOk = processNineCellWindow( &mInputNodataValue, &scanLine1[j], &scanLine1[j+1], &mInputNodataValue, &scanLine2[j], \
233  &scanLine2[j+1], &mInputNodataValue, &scanLine3[j], &scanLine3[j+1], \
234  &resultRedLine[j], &resultGreenLine[j], &resultBlueLine[j] );
235  }
236  else if ( j == xSize - 1 )
237  {
238  resultOk = processNineCellWindow( &scanLine1[j-1], &scanLine1[j], &mInputNodataValue, &scanLine2[j-1], &scanLine2[j], \
239  &mInputNodataValue, &scanLine3[j-1], &scanLine3[j], &mInputNodataValue, \
240  &resultRedLine[j], &resultGreenLine[j], &resultBlueLine[j] );
241  }
242  else
243  {
244  resultOk = processNineCellWindow( &scanLine1[j-1], &scanLine1[j], &scanLine1[j+1], &scanLine2[j-1], &scanLine2[j], \
245  &scanLine2[j+1], &scanLine3[j-1], &scanLine3[j], &scanLine3[j+1], \
246  &resultRedLine[j], &resultGreenLine[j], &resultBlueLine[j] );
247  }
248 
249  if ( !resultOk )
250  {
251  resultRedLine[j] = mOutputNodataValue;
252  resultGreenLine[j] = mOutputNodataValue;
253  resultBlueLine[j] = mOutputNodataValue;
254  }
255  }
256 
257  GDALRasterIO( outputRedBand, GF_Write, 0, i, xSize, 1, resultRedLine, xSize, 1, GDT_Byte, 0, 0 );
258  GDALRasterIO( outputGreenBand, GF_Write, 0, i, xSize, 1, resultGreenLine, xSize, 1, GDT_Byte, 0, 0 );
259  GDALRasterIO( outputBlueBand, GF_Write, 0, i, xSize, 1, resultBlueLine, xSize, 1, GDT_Byte, 0, 0 );
260  }
261 
262  if ( p )
263  {
264  p->setValue( ySize );
265  }
266 
267  CPLFree( resultRedLine );
268  CPLFree( resultBlueLine );
269  CPLFree( resultGreenLine );
270  CPLFree( scanLine1 );
271  CPLFree( scanLine2 );
272  CPLFree( scanLine3 );
273 
274  GDALClose( inputDataset );
275 
276  if ( p && p->wasCanceled() )
277  {
278  //delete the dataset without closing (because it is faster)
279  GDALDeleteDataset( outputDriver, TO8F( mOutputFile ) );
280  return 7;
281  }
282  GDALClose( outputDataset );
283 
284  return 0;
285 }
286 
287 bool QgsRelief::processNineCellWindow( float* x1, float* x2, float* x3, float* x4, float* x5, float* x6, float* x7, float* x8, float* x9,
288  unsigned char* red, unsigned char* green, unsigned char* blue )
289 {
290  //1. component: color and hillshade from 300 degrees
291  int r = 0;
292  int g = 0;
293  int b = 0;
294 
295  float hillShadeValue300 = mHillshadeFilter300->processNineCellWindow( x1, x2, x3, x4, x5, x6, x7, x8, x9 );
296  if ( hillShadeValue300 != mOutputNodataValue )
297  {
298  if ( !setElevationColor( *x5, &r, &g, &b ) )
299  {
300  r = hillShadeValue300;
301  g = hillShadeValue300;
302  b = hillShadeValue300;
303  }
304  else
305  {
306  r = r / 2.0 + hillShadeValue300 / 2.0;
307  g = g / 2.0 + hillShadeValue300 / 2.0;
308  b = b / 2.0 + hillShadeValue300 / 2.0;
309  }
310  }
311 
312  //2. component: hillshade and slope
313  float hillShadeValue315 = mHillshadeFilter315->processNineCellWindow( x1, x2, x3, x4, x5, x6, x7, x8, x9 );
314  float slope = mSlopeFilter->processNineCellWindow( x1, x2, x3, x4, x5, x6, x7, x8, x9 );
315  if ( hillShadeValue315 != mOutputNodataValue && slope != mOutputNodataValue )
316  {
317  int r2, g2, b2;
318  if ( slope > 15 )
319  {
320  r2 = 0 / 2.0 + hillShadeValue315 / 2.0;
321  g2 = 0 / 2.0 + hillShadeValue315 / 2.0;
322  b2 = 0 / 2.0 + hillShadeValue315 / 2.0;
323  }
324  else if ( slope >= 1 )
325  {
326  int slopeValue = 255 - ( slope / 15.0 * 255.0 );
327  r2 = slopeValue / 2.0 + hillShadeValue315 / 2.0;
328  g2 = slopeValue / 2.0 + hillShadeValue315 / 2.0;
329  b2 = slopeValue / 2.0 + hillShadeValue315 / 2.0;
330  }
331  else
332  {
333  r2 = hillShadeValue315; g2 = hillShadeValue315; b2 = hillShadeValue315;
334  }
335 
336  //combine with r,g,b with 70 percentage coverage
337  r = r * 0.7 + r2 * 0.3;
338  g = g * 0.7 + g2 * 0.3;
339  b = b * 0.7 + b2 * 0.3;
340  }
341 
342  //3. combine yellow aspect with 10% transparency, illumination from 285 degrees
343  float hillShadeValue285 = mHillshadeFilter285->processNineCellWindow( x1, x2, x3, x4, x5, x6, x7, x8, x9 );
344  float aspect = mAspectFilter->processNineCellWindow( x1, x2, x3, x4, x5, x6, x7, x8, x9 );
345  if ( hillShadeValue285 != mOutputNodataValue && aspect != mOutputNodataValue )
346  {
347  double angle_diff = qAbs( 285 - aspect );
348  if ( angle_diff > 180 )
349  {
350  angle_diff -= 180;
351  }
352 
353  int r3, g3, b3;
354  if ( angle_diff < 90 )
355  {
356  int aspectVal = ( 1 - cos( angle_diff * M_PI / 180 ) ) * 255;
357  r3 = 0.5 * 255 + hillShadeValue315 * 0.5;
358  g3 = 0.5 * 255 + hillShadeValue315 * 0.5;
359  b3 = 0.5 * aspectVal + hillShadeValue315 * 0.5;
360  }
361  else //white
362  {
363  r3 = 0.5 * 255 + hillShadeValue315 * 0.5;
364  g3 = 0.5 * 255 + hillShadeValue315 * 0.5;
365  b3 = 0.5 * 255 + hillShadeValue315 * 0.5;
366  }
367 
368  r = r3 * 0.1 + r * 0.9;
369  g = g3 * 0.1 + g * 0.9;
370  b = b3 * 0.1 + b * 0.9;
371  }
372 
373  *red = ( unsigned char )r;
374  *green = ( unsigned char )g;
375  *blue = ( unsigned char )b;
376  return true;
377 }
378 
379 bool QgsRelief::setElevationColor( double elevation, int* red, int* green, int* blue )
380 {
381  QList< ReliefColor >::const_iterator reliefColorIt = mReliefColors.constBegin();
382  for ( ; reliefColorIt != mReliefColors.constEnd(); ++reliefColorIt )
383  {
384  if ( elevation >= reliefColorIt->minElevation && elevation <= reliefColorIt->maxElevation )
385  {
386  const QColor& c = reliefColorIt->color;
387  *red = c.red();
388  *green = c.green();
389  *blue = c.blue();
390 
391  return true;
392  }
393  }
394  return false;
395 }
396 
397 //duplicated from QgsNineCellFilter. Todo: make common base class
398 GDALDatasetH QgsRelief::openInputFile( int& nCellsX, int& nCellsY )
399 {
400  GDALDatasetH inputDataset = GDALOpen( TO8F( mInputFile ), GA_ReadOnly );
401  if ( inputDataset != NULL )
402  {
403  nCellsX = GDALGetRasterXSize( inputDataset );
404  nCellsY = GDALGetRasterYSize( inputDataset );
405 
406  //we need at least one band
407  if ( GDALGetRasterCount( inputDataset ) < 1 )
408  {
409  GDALClose( inputDataset );
410  return NULL;
411  }
412  }
413  return inputDataset;
414 }
415 
416 GDALDriverH QgsRelief::openOutputDriver()
417 {
418  char **driverMetadata;
419 
420  //open driver
421  GDALDriverH outputDriver = GDALGetDriverByName( mOutputFormat.toLocal8Bit().data() );
422 
423  if ( outputDriver == NULL )
424  {
425  return outputDriver; //return NULL, driver does not exist
426  }
427 
428  driverMetadata = GDALGetMetadata( outputDriver, NULL );
429  if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) )
430  {
431  return NULL; //driver exist, but it does not support the create operation
432  }
433 
434  return outputDriver;
435 }
436 
437 GDALDatasetH QgsRelief::openOutputFile( GDALDatasetH inputDataset, GDALDriverH outputDriver )
438 {
439  if ( inputDataset == NULL )
440  {
441  return NULL;
442  }
443 
444  int xSize = GDALGetRasterXSize( inputDataset );
445  int ySize = GDALGetRasterYSize( inputDataset );;
446 
447  //open output file
448  char **papszOptions = NULL;
449 
450  //use PACKBITS compression for tiffs by default
451  papszOptions = CSLSetNameValue( papszOptions, "COMPRESS", "PACKBITS" );
452 
453  //create three band raster (reg, green, blue)
454  GDALDatasetH outputDataset = GDALCreate( outputDriver, TO8F( mOutputFile ), xSize, ySize, 3, GDT_Byte, papszOptions );
455  if ( outputDataset == NULL )
456  {
457  return outputDataset;
458  }
459 
460  //get geotransform from inputDataset
461  double geotransform[6];
462  if ( GDALGetGeoTransform( inputDataset, geotransform ) != CE_None )
463  {
464  GDALClose( outputDataset );
465  return NULL;
466  }
467  GDALSetGeoTransform( outputDataset, geotransform );
468 
469  //make sure mCellSizeX and mCellSizeY are always > 0
470  mCellSizeX = geotransform[1];
471  if ( mCellSizeX < 0 )
472  {
473  mCellSizeX = -mCellSizeX;
474  }
475  mCellSizeY = geotransform[5];
476  if ( mCellSizeY < 0 )
477  {
478  mCellSizeY = -mCellSizeY;
479  }
480 
481  const char* projection = GDALGetProjectionRef( inputDataset );
482  GDALSetProjection( outputDataset, projection );
483 
484  return outputDataset;
485 }
486 
487 //this function is mainly there for debugging
489 {
490  int nCellsX, nCellsY;
491  GDALDatasetH inputDataset = openInputFile( nCellsX, nCellsY );
492  if ( inputDataset == NULL )
493  {
494  return false;
495  }
496 
497  //open first raster band for reading (elevation raster is always single band)
498  GDALRasterBandH elevationBand = GDALGetRasterBand( inputDataset, 1 );
499  if ( elevationBand == NULL )
500  {
501  GDALClose( inputDataset );
502  return false;
503  }
504 
505  //1. get minimum and maximum of elevation raster -> 252 elevation classes
506  int minOk, maxOk;
507  double minMax[2];
508  minMax[0] = GDALGetRasterMinimum( elevationBand, &minOk );
509  minMax[1] = GDALGetRasterMaximum( elevationBand, &maxOk );
510 
511  if ( !minOk || !maxOk )
512  {
513  GDALComputeRasterMinMax( elevationBand, TRUE, minMax );
514  }
515 
516  //2. go through raster cells and get frequency of classes
517 
518  //store elevation frequency in 256 elevation classes
519  double frequency[252];
520  double frequencyClassRange = ( minMax[1] - minMax[0] ) / 252.0;
521  //initialize to zero
522  for ( int i = 0; i < 252; ++i )
523  {
524  frequency[i] = 0;
525  }
526 
527  float* scanLine = ( float * ) CPLMalloc( sizeof( float ) * nCellsX );
528  int elevationClass = -1;
529 
530  for ( int i = 0; i < nCellsY; ++i )
531  {
532  GDALRasterIO( elevationBand, GF_Read, 0, i, nCellsX, 1,
533  scanLine, nCellsX, 1, GDT_Float32,
534  0, 0 );
535  for ( int j = 0; j < nCellsX; ++j )
536  {
537  elevationClass = frequencyClassForElevation( scanLine[j], minMax[0], frequencyClassRange );
538  if ( elevationClass >= 0 )
539  {
540  frequency[elevationClass] += 1.0;
541  }
542  }
543  }
544 
545  CPLFree( scanLine );
546 
547  //log10 transformation for all frequency values
548  for ( int i = 0; i < 252; ++i )
549  {
550  frequency[i] = log10( frequency[i] );
551  }
552 
553  //write out frequency values to csv file for debugging
554  QFile outFile( file );
555  if ( !outFile.open( QIODevice::WriteOnly ) )
556  {
557  return false;
558  }
559 
560  QTextStream outstream( &outFile );
561  for ( int i = 0; i < 252; ++i )
562  {
563  outstream << QString::number( i ) + "," + QString::number( frequency[i] ) << endl;
564  }
565  outFile.close();
566  return true;
567 }
568 
569 QList< QgsRelief::ReliefColor > QgsRelief::calculateOptimizedReliefClasses()
570 {
571  QList< QgsRelief::ReliefColor > resultList;
572 
573  int nCellsX, nCellsY;
574  GDALDatasetH inputDataset = openInputFile( nCellsX, nCellsY );
575  if ( inputDataset == NULL )
576  {
577  return resultList;
578  }
579 
580  //open first raster band for reading (elevation raster is always single band)
581  GDALRasterBandH elevationBand = GDALGetRasterBand( inputDataset, 1 );
582  if ( elevationBand == NULL )
583  {
584  GDALClose( inputDataset );
585  return resultList;
586  }
587 
588  //1. get minimum and maximum of elevation raster -> 252 elevation classes
589  int minOk, maxOk;
590  double minMax[2];
591  minMax[0] = GDALGetRasterMinimum( elevationBand, &minOk );
592  minMax[1] = GDALGetRasterMaximum( elevationBand, &maxOk );
593 
594  if ( !minOk || !maxOk )
595  {
596  GDALComputeRasterMinMax( elevationBand, TRUE, minMax );
597  }
598 
599  //2. go through raster cells and get frequency of classes
600 
601  //store elevation frequency in 256 elevation classes
602  double frequency[252];
603  double frequencyClassRange = ( minMax[1] - minMax[0] ) / 252.0;
604  //initialize to zero
605  for ( int i = 0; i < 252; ++i )
606  {
607  frequency[i] = 0;
608  }
609 
610  float* scanLine = ( float * ) CPLMalloc( sizeof( float ) * nCellsX );
611  int elevationClass = -1;
612 
613  for ( int i = 0; i < nCellsY; ++i )
614  {
615  GDALRasterIO( elevationBand, GF_Read, 0, i, nCellsX, 1,
616  scanLine, nCellsX, 1, GDT_Float32,
617  0, 0 );
618  for ( int j = 0; j < nCellsX; ++j )
619  {
620  elevationClass = frequencyClassForElevation( scanLine[j], minMax[0], frequencyClassRange );
621  if ( elevationClass < 0 )
622  {
623  elevationClass = 0;
624  }
625  else if ( elevationClass >= 252 )
626  {
627  elevationClass = 251;
628  }
629  frequency[elevationClass] += 1.0;
630  }
631  }
632 
633  CPLFree( scanLine );
634 
635  //log10 transformation for all frequency values
636  for ( int i = 0; i < 252; ++i )
637  {
638  frequency[i] = log10( frequency[i] );
639  }
640 
641  //start with 9 uniformly distributed classes
642  QList<int> classBreaks;
643  classBreaks.append( 0 );
644  classBreaks.append( 28 );
645  classBreaks.append( 56 );
646  classBreaks.append( 84 );
647  classBreaks.append( 112 );
648  classBreaks.append( 140 );
649  classBreaks.append( 168 );
650  classBreaks.append( 196 );
651  classBreaks.append( 224 );
652  classBreaks.append( 252 );
653 
654  for ( int i = 0; i < 10; ++i )
655  {
656  optimiseClassBreaks( classBreaks, frequency );
657  }
658 
659  //debug, print out all the classbreaks
660  for ( int i = 0; i < classBreaks.size(); ++i )
661  {
662  qWarning( "%d", classBreaks[i] );
663  }
664 
665  //set colors according to optimised class breaks
666  QList<QColor> colorList;
667  colorList.push_back( QColor( 7, 165, 144 ) );
668  colorList.push_back( QColor( 12, 221, 162 ) );
669  colorList.push_back( QColor( 33, 252, 183 ) );
670  colorList.push_back( QColor( 247, 252, 152 ) );
671  colorList.push_back( QColor( 252, 196, 8 ) );
672  colorList.push_back( QColor( 252, 166, 15 ) );
673  colorList.push_back( QColor( 175, 101, 15 ) );
674  colorList.push_back( QColor( 255, 133, 92 ) );
675  colorList.push_back( QColor( 204, 204, 204 ) );
676 
677  for ( int i = 1; i < classBreaks.size(); ++i )
678  {
679  double minElevation = minMax[0] + classBreaks[i - 1] * frequencyClassRange;
680  double maxElevation = minMax[0] + classBreaks[i] * frequencyClassRange;
681  resultList.push_back( QgsRelief::ReliefColor( colorList.at( i - 1 ), minElevation, maxElevation ) );
682  }
683 
684  return resultList;
685 }
686 
687 void QgsRelief::optimiseClassBreaks( QList<int>& breaks, double* frequencies )
688 {
689  int nClasses = breaks.size() - 1;
690  double* a = new double[nClasses]; //slopes
691  double* b = new double[nClasses]; //y-offsets
692 
693  for ( int i = 0; i < nClasses; ++i )
694  {
695  //get all the values between the class breaks into input
696  QList< QPair < int, double > > regressionInput;
697  for ( int j = breaks.at( i ); j < breaks.at( i + 1 ); ++j )
698  {
699  regressionInput.push_back( qMakePair( j, frequencies[j] ) );
700  }
701 
702  double aParam, bParam;
703  if ( regressionInput.size() > 0 && calculateRegression( regressionInput, aParam, bParam ) )
704  {
705  a[i] = aParam;
706  b[i] = bParam;
707  }
708  else
709  {
710  a[i] = 0;
711  b[i] = 0; //better default value
712  }
713  }
714 
715  QList<int> classesToRemove;
716 
717  //shift class boundaries or eliminate classes which fall together
718  for ( int i = 1; i < nClasses ; ++i )
719  {
720  if ( breaks[i] == breaks[ i - 1 ] )
721  {
722  continue;
723  }
724 
725  if ( qgsDoubleNear( a[i - 1 ], a[i] ) )
726  {
727  continue;
728  }
729  else
730  {
731  int newX = ( b[i - 1] - b[ i ] ) / ( a[ i ] - a[ i - 1 ] );
732 
733  if ( newX <= breaks[i - 1] )
734  {
735  newX = breaks[i - 1];
736  // classesToRemove.push_back( i );//remove this class later as it falls together with the preceding one
737  }
738  else if ( i < nClasses - 1 && newX >= breaks[i + 1] )
739  {
740  newX = breaks[i + 1];
741  // classesToRemove.push_back( i );//remove this class later as it falls together with the next one
742  }
743 
744  breaks[i] = newX;
745  }
746  }
747 
748  for ( int i = classesToRemove.size() - 1; i >= 0; --i )
749  {
750  breaks.removeAt( classesToRemove.at( i ) );
751  }
752 
753  delete[] a;
754  delete[] b;
755 }
756 
757 int QgsRelief::frequencyClassForElevation( double elevation, double minElevation, double elevationClassRange )
758 {
759  return ( elevation - minElevation ) / elevationClassRange;
760 }
761 
762 bool QgsRelief::calculateRegression( const QList< QPair < int, double > >& input, double& a, double& b )
763 {
764  double xMean, yMean;
765  double xSum = 0;
766  double ySum = 0;
767  QList< QPair < int, double > >::const_iterator inputIt = input.constBegin();
768  for ( ; inputIt != input.constEnd(); ++inputIt )
769  {
770  xSum += inputIt->first;
771  ySum += inputIt->second;
772  }
773  xMean = xSum / input.size();
774  yMean = ySum / input.size();
775 
776  double sumCounter = 0;
777  double sumDenominator = 0;
778  inputIt = input.constBegin();
779  for ( ; inputIt != input.constEnd(); ++inputIt )
780  {
781  sumCounter += (( inputIt->first - xMean ) * ( inputIt->second - yMean ) );
782  sumDenominator += (( inputIt->first - xMean ) * ( inputIt->first - xMean ) );
783  }
784 
785  a = sumCounter / sumDenominator;
786  b = yMean - a * xMean;
787 
788  return true;
789 }
790