QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
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
20QgsDerivativeFilter::QgsDerivativeFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat )
21 : QgsNineCellFilter( inputFile, outputFile, outputFormat )
22{}
23
24float QgsDerivativeFilter::calcFirstDerX( float x11, float x21, float x31, float x12, float x22, float x32, float x13, float x23, float x33 ) const
25{
26 //the basic formula would be simple, but we need to test for nodata values...
27 //return (( (*x31 - *x11) + 2 * (*x32 - *x12) + (*x33 - *x13) ) / (8 * mCellSizeX));
28
29 int weight = 0;
30 double sum = 0;
31
32 //first row
33 if ( x31 != mInputNodataValue && x11 != mInputNodataValue ) //the normal case
34 {
35 sum += ( x31 - x11 );
36 weight += 2;
37 }
38 else if ( x31 == mInputNodataValue && x11 != mInputNodataValue && x21 != mInputNodataValue ) //probably 3x3 window is at the border
39 {
40 sum += ( x21 - x11 );
41 weight += 1;
42 }
43 else if ( x11 == mInputNodataValue && x31 != mInputNodataValue && x21 != mInputNodataValue ) //probably 3x3 window is at the border
44 {
45 sum += ( x31 - x21 );
46 weight += 1;
47 }
48
49 //second row
50 if ( x32 != mInputNodataValue && x12 != mInputNodataValue ) //the normal case
51 {
52 sum += 2 * ( x32 - x12 );
53 weight += 4;
54 }
55 else if ( x32 == mInputNodataValue && x12 != mInputNodataValue && x22 != mInputNodataValue )
56 {
57 sum += 2 * ( x22 - x12 );
58 weight += 2;
59 }
60 else if ( x12 == mInputNodataValue && x32 != mInputNodataValue && x22 != mInputNodataValue )
61 {
62 sum += 2 * ( x32 - x22 );
63 weight += 2;
64 }
65
66 //third row
67 if ( x33 != mInputNodataValue && x13 != mInputNodataValue ) //the normal case
68 {
69 sum += ( x33 - x13 );
70 weight += 2;
71 }
72 else if ( x33 == mInputNodataValue && x13 != mInputNodataValue && x23 != mInputNodataValue )
73 {
74 sum += ( x23 - x13 );
75 weight += 1;
76 }
77 else if ( x13 == mInputNodataValue && x33 != mInputNodataValue && x23 != mInputNodataValue )
78 {
79 sum += ( x33 - x23 );
80 weight += 1;
81 }
82
83 if ( weight == 0 )
84 {
85 return mOutputNodataValue;
86 }
87
88 return sum / ( weight * mCellSizeX ) * mZFactor;
89}
90
91float QgsDerivativeFilter::calcFirstDerY( float x11, float x21, float x31, float x12, float x22, float x32, float x13, float x23, float x33 ) const
92{
93 //the basic formula would be simple, but we need to test for nodata values...
94 //return (((*x11 - *x13) + 2 * (*x21 - *x23) + (*x31 - *x33)) / ( 8 * mCellSizeY));
95
96 double sum = 0;
97 int weight = 0;
98
99 //first row
100 if ( x11 != mInputNodataValue && x13 != mInputNodataValue ) //normal case
101 {
102 sum += ( x11 - x13 );
103 weight += 2;
104 }
105 else if ( x11 == mInputNodataValue && x13 != mInputNodataValue && x12 != mInputNodataValue )
106 {
107 sum += ( x12 - x13 );
108 weight += 1;
109 }
110 else if ( x31 == mInputNodataValue && x11 != mInputNodataValue && x12 != mInputNodataValue )
111 {
112 sum += ( x11 - x12 );
113 weight += 1;
114 }
115
116 //second row
117 if ( x21 != mInputNodataValue && x23 != mInputNodataValue )
118 {
119 sum += 2 * ( x21 - x23 );
120 weight += 4;
121 }
122 else if ( x21 == mInputNodataValue && x23 != mInputNodataValue && x22 != mInputNodataValue )
123 {
124 sum += 2 * ( x22 - x23 );
125 weight += 2;
126 }
127 else if ( x23 == mInputNodataValue && x21 != mInputNodataValue && x22 != mInputNodataValue )
128 {
129 sum += 2 * ( x21 - x22 );
130 weight += 2;
131 }
132
133 //third row
134 if ( x31 != mInputNodataValue && x33 != mInputNodataValue )
135 {
136 sum += ( x31 - x33 );
137 weight += 2;
138 }
139 else if ( x31 == mInputNodataValue && x33 != mInputNodataValue && x32 != mInputNodataValue )
140 {
141 sum += ( x32 - x33 );
142 weight += 1;
143 }
144 else if ( x33 == mInputNodataValue && x31 != mInputNodataValue && x32 != mInputNodataValue )
145 {
146 sum += ( x31 - x32 );
147 weight += 1;
148 }
149
150 if ( weight == 0 )
151 {
152 return mOutputNodataValue;
153 }
154
155 return sum / ( weight * mCellSizeY ) * mZFactor;
156}
QgsDerivativeFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat)
float calcFirstDerY(float x11, float x21, float x31, float x12, float x22, float x32, float x13, float x23, float x33) const
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) const
Calculates the first order derivative in x-direction according to Horn (1981).
QgsNineCellFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat)
Constructor that takes input file, output file and output format (GDAL string).
double mOutputNodataValue
The nodata value of the output layer.
double mInputNodataValue
The nodata value of the input layer.
double mZFactor
Scale factor for z-value if x-/y- units are different to z-units (111120 for degree->meters and 37040...