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