A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.
1 /********************************************************** 2 * Version $Id: A1WiTh.cpp 911 2011-02-14 16:38:15Z reklov_w $ 3 *********************************************************/ 4 5 /////////////////////////////////////////////////////////// 6 // // 7 // Tool: // 8 // A1WiTh // 9 // // 10 // for SAGA // 11 // System for Automated Geoscientific Analyses // 12 // // 13 //-------------------------------------------------------// 14 // // 15 // A1WiTh.cpp // 16 // // 17 // // 18 //-------------------------------------------------------// 19 // // 20 // // 21 // by Alessandro Perego (Italy) // 22 // // 23 // e-mail: alper78@alice.it // 24 // // 25 // // 26 /////////////////////////////////////////////////////////// 27 28 //--------------------------------------------------------- 29 #include "A1WiTh.h" 30 31 //--------------------------------------------------------- 32 CA1WiTh::CA1WiTh(void) 33 { 34 // 1. Info... 35 Set_Name(_TL("Average With Thereshold 1")); 36 Set_Author(_TL("Alessandro Perego")); 37 Set_Description(_TL("Average With Thereshold for Grids calculates average in X and Y distances unsing only the values that differ form central pixel less than a specified threshold. It's useful to remove noise whit a known maximum reducing the loss of informations")); 38 39 // 2. Parameters... 40 Parameters.Add_Grid(NULL, "INPUT", _TL("Input"), _TL("This must be your input data of type grid."), PARAMETER_INPUT); 41 Parameters.Add_Grid(NULL, "RESULT", _TL("AWT Grid"), _TL("New grid filtered with the A1WiTh tool"), PARAMETER_OUTPUT); 42 Parameters.Add_Value(NULL, "RX", _TL("Radius X"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true); 43 Parameters.Add_Value(NULL, "RY", _TL("Radius Y"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true); 44 Parameters.Add_Value(NULL, "THRESH", _TL("Threshold"), _TL("The values in the specified radius is used in the average calculation only if its difference with the central value is lesser or equal to this threshold."), PARAMETER_TYPE_Double, 2.0); 45 } 46 47 //--------------------------------------------------------- 48 CA1WiTh::~CA1WiTh(void) 49 {} 50 51 //--------------------------------------------------------- 52 bool CA1WiTh::On_Execute(void) 53 { 54 int x, y, Rx, Ry, ax, ay, bx, by, ix, iy, n; 55 double Thresh, Diff, Sum, c, d; 56 CSG_Grid *pInput, *pResult; 57 58 pInput = Parameters("INPUT")->asGrid(); 59 pResult = Parameters("RESULT")->asGrid(); 60 Rx = Parameters("RX")->asInt(); 61 Ry = Parameters("RY")->asInt(); 62 Thresh = Parameters("THRESH")->asDouble(); 63 64 //----------------------------------------------------- 65 for(y=0; y<Get_NY() && Set_Progress_Rows(y); y++) 66 { 67 for(x=0; x<Get_NX(); x++) 68 { 69 Sum = 0.0; 70 n = 0; 71 //---------------------------------------------------- 72 ax = x - Rx; 73 bx = x + Ry; 74 75 ay = y - Ry; 76 by = y + Ry; 77 78 if( ax < 0 ) { ax = 0; } 79 if( bx >= Get_NX() ){ bx = Get_NX() - 1; } 80 if( ay < 0 ) { ay = 0; } 81 if( by >= Get_NY() ){ by = Get_NY() - 1; } 82 83 //----------------------------------------------------- 84 for(iy=ay; iy<=by; iy++) 85 { 86 for(ix=ax; ix<=bx; ix++) 87 { 88 c = pInput->asDouble(ix, iy); 89 d = pInput->asDouble(x, y); 90 Diff = c - d; 91 if( Diff < 0) 92 { 93 Diff = 0 - Diff; 94 } 95 if( Diff <= Thresh ) 96 { 97 Sum += pInput->asDouble(ix, iy); 98 n++; 99 } 100 } 101 } 102 103 //------------------------------------------------------ 104 if( n > 0 ) 105 { 106 pResult->Set_Value(x, y, Sum / n); 107 } 108 109 } 110 } 111 112 return( true ); 113 } 114 115