1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                      Grid_Filter                      //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                    Filter_Gauss.cpp                   //
14 //                                                       //
15 //                 Copyright (C) 2003 by                 //
16 //                    Andre Ringeler                     //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    e-mail:     aringel@gwdg.de                        //
40 //                                                       //
41 //    contact:    Andre Ringeler                         //
42 //                Institute of Geography                 //
43 //                University of Goettingen               //
44 //                Goldschmidtstr. 5                      //
45 //                37077 Goettingen                       //
46 //                Germany                                //
47 //                                                       //
48 ///////////////////////////////////////////////////////////
49 
50 //---------------------------------------------------------
51 #include "Filter_Gauss.h"
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
CFilter_Gauss(void)61 CFilter_Gauss::CFilter_Gauss(void)
62 {
63 	//-----------------------------------------------------
64 	Set_Name		(_TL("Gaussian Filter"));
65 
66 	Set_Author		("A.Ringeler (c) 2003");
67 
68 	Set_Description	(_TW(
69 		"The Gaussian filter is a smoothing operator that is used to 'blur' or 'soften' data "
70 		"and to remove detail and noise. "
71 		"The degree of smoothing is determined by the standard deviation. "
72 		"For higher standard deviations you need to use a larger search radius."
73 	));
74 
75 	//-----------------------------------------------------
76 	Parameters.Add_Grid("",
77 		"INPUT"		, _TL("Grid"),
78 		_TL(""),
79 		PARAMETER_INPUT
80 	);
81 
82 	Parameters.Add_Grid("",
83 		"RESULT"	, _TL("Filtered Grid"),
84 		_TL(""),
85 		PARAMETER_OUTPUT_OPTIONAL
86 	);
87 
88 	Parameters.Add_Double("",
89 		"SIGMA"		, _TL("Standard Deviation"),
90 		_TL("The standard deviation as percentage of the kernel radius, determines the degree of smoothing."),
91 		50.0, 0.0001, true
92 	);
93 
94 	CSG_Grid_Cell_Addressor::Add_Parameters(Parameters);
95 }
96 
97 
98 ///////////////////////////////////////////////////////////
99 //														 //
100 ///////////////////////////////////////////////////////////
101 
102 //---------------------------------------------------------
On_Execute(void)103 bool CFilter_Gauss::On_Execute(void)
104 {
105 	//-----------------------------------------------------
106 	double	Sigma	= Parameters("SIGMA")->asDouble();
107 
108 	CSG_Grid_Cell_Addressor	Kernel;
109 
110 	Kernel.Get_Weighting().Set_Weighting(SG_DISTWGHT_GAUSS);
111 	Kernel.Get_Weighting().Set_BandWidth(Sigma * Parameters("KERNEL_RADIUS")->asDouble() / 100.0);
112 
113 	if( !Kernel.Set_Parameters(Parameters) )
114 	{
115 		Error_Set(_TL("could not initialize kernel"));
116 
117 		return( false );
118 	}
119 
120 	//-----------------------------------------------------
121 	CSG_Grid	*pInput 	= Parameters("INPUT" )->asGrid(), Input;
122 	CSG_Grid	*pResult	= Parameters("RESULT")->asGrid();
123 
124 	if( !pResult || pResult == pInput )
125 	{
126 		Input.Create(*pInput);
127 
128 		pResult	= pInput;
129 		pInput	= &Input;
130 	}
131 	else
132 	{
133 		pResult->Fmt_Name("%s [%s]", pInput->Get_Name(), _TL("Gaussian Filter"));
134 
135 		pResult->Set_NoData_Value(pInput->Get_NoData_Value());
136 
137 		DataObject_Set_Parameters(pResult, pInput);
138 	}
139 
140 	//-----------------------------------------------------
141 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
142 	{
143 		#pragma omp parallel for
144 		for(int x=0; x<Get_NX(); x++)
145 		{
146 			CSG_Simple_Statistics	s;
147 
148 			if( !pInput->is_NoData(x, y) )
149 			{
150 				for(int i=0; i<Kernel.Get_Count(); i++)
151 				{
152 					int	ix	= Kernel.Get_X(i, x);
153 					int	iy	= Kernel.Get_Y(i, y);
154 
155 					if( pInput->is_InGrid(ix, iy) )
156 					{
157 						s.Add_Value(pInput->asDouble(ix, iy), Kernel.Get_Weight(i));
158 					}
159 				}
160 			}
161 
162 			if( s.Get_Weights() > 0.0 )
163 			{
164 				pResult->Set_Value(x, y, s.Get_Mean());
165 			}
166 			else
167 			{
168 				pResult->Set_NoData(x, y);
169 			}
170 		}
171 	}
172 
173 	//-----------------------------------------------------
174 	if( pResult == Parameters("INPUT")->asGrid() )
175 	{
176 		DataObject_Update(pResult);
177 	}
178 
179 	return( true );
180 }
181 
182 
183 ///////////////////////////////////////////////////////////
184 //														 //
185 //														 //
186 //														 //
187 ///////////////////////////////////////////////////////////
188 
189 //---------------------------------------------------------
190