QGIS API Documentation  2.99.0-Master (b058df7)
qgshillshadefilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshillshadefilter.h - description
3  --------------------------------
4  begin : September 26th, 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 "qgshillshadefilter.h"
19 #include <cmath>
20 
21 QgsHillshadeFilter::QgsHillshadeFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat, double lightAzimuth,
22  double lightAngle )
23  : QgsDerivativeFilter( inputFile, outputFile, outputFormat )
24  , mLightAzimuth( lightAzimuth )
25  , mLightAngle( lightAngle )
26 {
27 }
28 
29 float QgsHillshadeFilter::processNineCellWindow( float *x11, float *x21, float *x31,
30  float *x12, float *x22, float *x32,
31  float *x13, float *x23, float *x33 )
32 {
33  float derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33 );
34  float derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33 );
35 
36  if ( derX == mOutputNodataValue || derY == mOutputNodataValue )
37  {
38  return mOutputNodataValue;
39  }
40 
41  float zenith_rad = mLightAngle * M_PI / 180.0;
42  float slope_rad = std::atan( std::sqrt( derX * derX + derY * derY ) );
43  float azimuth_rad = mLightAzimuth * M_PI / 180.0;
44  float aspect_rad = 0;
45  if ( derX == 0 && derY == 0 ) //aspect undefined, take a neutral value. Better solutions?
46  {
47  aspect_rad = azimuth_rad / 2.0;
48  }
49  else
50  {
51  aspect_rad = M_PI + std::atan2( derX, derY );
52  }
53  return std::max( 0.0, 255.0 * ( ( std::cos( zenith_rad ) * std::cos( slope_rad ) ) + ( std::sin( zenith_rad ) * std::sin( slope_rad ) * std::cos( azimuth_rad - aspect_rad ) ) ) );
54 }
float calcFirstDerY(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33)
Calculates the first order derivative in y-direction according to Horn (1981)
float calcFirstDerX(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33)
Calculates the first order derivative in x-direction according to Horn (1981)
QgsHillshadeFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat, double lightAzimuth=300, double lightAngle=40)
Adds the ability to calculate derivatives in x- and y-directions.
float mOutputNodataValue
The nodata value of the output layer.
float processNineCellWindow(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33) override
Calculates output value from nine input values.