QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsderivativefilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsderivativefilter.cpp - description
3  -----------------------
4  begin : August 7th, 2009
5  copyright : (C) 2009 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz 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 "qgsderivativefilter.h"
19 
20 QgsDerivativeFilter::QgsDerivativeFilter( const QString& inputFile, const QString& outputFile, const QString& outputFormat )
21  : QgsNineCellFilter( inputFile, outputFile, outputFormat )
22 {
23 
24 }
25 
27 {
28 
29 }
30 
31 float QgsDerivativeFilter::calcFirstDerX( float* x11, float* x21, float* x31, float* x12, float* x22, float* x32, float* x13, float* x23, float* x33 )
32 {
33  //the basic formula would be simple, but we need to test for nodata values...
34  //return (( (*x31 - *x11) + 2 * (*x32 - *x12) + (*x33 - *x13) ) / (8 * mCellSizeX));
35 
36  int weight = 0;
37  double sum = 0;
38 
39  //first row
40  if ( *x31 != mInputNodataValue && *x11 != mInputNodataValue ) //the normal case
41  {
42  sum += ( *x31 - *x11 );
43  weight += 2;
44  }
45  else if ( *x31 == mInputNodataValue && *x11 != mInputNodataValue && *x21 != mInputNodataValue ) //probably 3x3 window is at the border
46  {
47  sum += ( *x21 - *x11 );
48  weight += 1;
49  }
50  else if ( *x11 == mInputNodataValue && *x31 != mInputNodataValue && *x21 != mInputNodataValue ) //probably 3x3 window is at the border
51  {
52  sum += ( *x31 - *x21 );
53  weight += 1;
54  }
55 
56  //second row
57  if ( *x32 != mInputNodataValue && *x12 != mInputNodataValue ) //the normal case
58  {
59  sum += 2 * ( *x32 - *x12 );
60  weight += 4;
61  }
62  else if ( *x32 == mInputNodataValue && *x12 != mInputNodataValue && *x22 != mInputNodataValue )
63  {
64  sum += 2 * ( *x22 - *x12 );
65  weight += 2;
66  }
67  else if ( *x12 == mInputNodataValue && *x32 != mInputNodataValue && *x22 != mInputNodataValue )
68  {
69  sum += 2 * ( *x32 - *x22 );
70  weight += 2;
71  }
72 
73  //third row
74  if ( *x33 != mInputNodataValue && *x13 != mInputNodataValue ) //the normal case
75  {
76  sum += ( *x33 - *x13 );
77  weight += 2;
78  }
79  else if ( *x33 == mInputNodataValue && *x13 != mInputNodataValue && *x23 != mInputNodataValue )
80  {
81  sum += ( *x23 - *x13 );
82  weight += 1;
83  }
84  else if ( *x13 == mInputNodataValue && *x33 != mInputNodataValue && *x23 != mInputNodataValue )
85  {
86  sum += ( *x33 - *x23 );
87  weight += 1;
88  }
89 
90  if ( weight == 0 )
91  {
92  return mOutputNodataValue;
93  }
94 
95  return sum / ( weight * mCellSizeX ) * mZFactor;
96 }
97 
98 float QgsDerivativeFilter::calcFirstDerY( float* x11, float* x21, float* x31, float* x12, float* x22, float* x32, float* x13, float* x23, float* x33 )
99 {
100  //the basic formula would be simple, but we need to test for nodata values...
101  //return (((*x11 - *x13) + 2 * (*x21 - *x23) + (*x31 - *x33)) / ( 8 * mCellSizeY));
102 
103  double sum = 0;
104  int weight = 0;
105 
106  //first row
107  if ( *x11 != mInputNodataValue && *x13 != mInputNodataValue ) //normal case
108  {
109  sum += ( *x11 - *x13 );
110  weight += 2;
111  }
112  else if ( *x11 == mInputNodataValue && *x13 != mInputNodataValue && *x12 != mInputNodataValue )
113  {
114  sum += ( *x12 - *x13 );
115  weight += 1;
116  }
117  else if ( *x31 == mInputNodataValue && *x11 != mInputNodataValue && *x12 != mInputNodataValue )
118  {
119  sum += ( *x11 - *x12 );
120  weight += 1;
121  }
122 
123  //second row
124  if ( *x21 != mInputNodataValue && *x23 != mInputNodataValue )
125  {
126  sum += 2 * ( *x21 - *x23 );
127  weight += 4;
128  }
129  else if ( *x21 == mInputNodataValue && *x23 != mInputNodataValue && *x22 != mInputNodataValue )
130  {
131  sum += 2 * ( *x22 - *x23 );
132  weight += 2;
133  }
134  else if ( *x23 == mInputNodataValue && *x21 != mInputNodataValue && *x22 != mInputNodataValue )
135  {
136  sum += 2 * ( *x21 - *x22 );
137  weight += 2;
138  }
139 
140  //third row
141  if ( *x31 != mInputNodataValue && *x33 != mInputNodataValue )
142  {
143  sum += ( *x31 - *x33 );
144  weight += 2;
145  }
146  else if ( *x31 == mInputNodataValue && *x33 != mInputNodataValue && *x32 != mInputNodataValue )
147  {
148  sum += ( *x32 - *x33 );
149  weight += 1;
150  }
151  else if ( *x33 == mInputNodataValue && *x31 != mInputNodataValue && *x32 != mInputNodataValue )
152  {
153  sum += ( *x31 - *x32 );
154  weight += 1;
155  }
156 
157  if ( weight == 0 )
158  {
159  return mOutputNodataValue;
160  }
161 
162  return sum / ( weight * mCellSizeY ) * mZFactor;
163 }
164 
165 
166 
167 
168 
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)
QgsDerivativeFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat)
float mOutputNodataValue
The nodata value of the output layer.
double mZFactor
Scale factor for z-value if x-/y- units are different to z-units (111120 for degree->meters and 37040...
float mInputNodataValue
The nodata value of the input layer.
Base class for raster analysis methods that work with a 3x3 cell filter and calculate the value of ea...