1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                      Grid_Filter                      //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                      Filter.cpp                       //
14 //                                                       //
15 //                 Copyright (C) 2003 by                 //
16 //                      Olaf Conrad                      //
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:     oconrad@saga-gis.org                   //
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Goettingen               //
44 //                Goldschmidtstr. 5                      //
45 //                37077 Goettingen                       //
46 //                Germany                                //
47 //                                                       //
48 ///////////////////////////////////////////////////////////
49 
50 //---------------------------------------------------------
51 #include "Filter.h"
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
CFilter(void)61 CFilter::CFilter(void)
62 {
63 	//-----------------------------------------------------
64 	Set_Name		(_TL("Simple Filter"));
65 
66 	Set_Author		("O.Conrad (c) 2003");
67 
68 	Set_Description	(_TW(
69 		"Simple standard filters for grids."
70 	));
71 
72 	//-----------------------------------------------------
73 	Parameters.Add_Grid("",
74 		"INPUT"		, _TL("Grid"),
75 		_TL(""),
76 		PARAMETER_INPUT
77 	);
78 
79 	Parameters.Add_Grid("",
80 		"RESULT"	, _TL("Filtered Grid"),
81 		_TL(""),
82 		PARAMETER_OUTPUT_OPTIONAL
83 	);
84 
85 	Parameters.Add_Choice("",
86 		"METHOD"	, _TL("Filter"),
87 		_TL("Choose the filter method."),
88 		CSG_String::Format("%s|%s|%s",
89 			_TL("Smooth"),
90 			_TL("Sharpen"),
91 			_TL("Edge")
92 		), 0
93 	);
94 
95 	CSG_Grid_Cell_Addressor::Add_Parameters(Parameters);
96 }
97 
98 
99 ///////////////////////////////////////////////////////////
100 //														 //
101 ///////////////////////////////////////////////////////////
102 
103 //---------------------------------------------------------
On_Execute(void)104 bool CFilter::On_Execute(void)
105 {
106 	//-----------------------------------------------------
107 	int	Method	= Parameters("METHOD")->asInt();
108 
109 	if( !m_Kernel.Set_Parameters(Parameters) )
110 	{
111 		Error_Set(_TL("could not initialize kernel"));
112 
113 		return( false );
114 	}
115 
116 	//-----------------------------------------------------
117 	m_pInput	= Parameters("INPUT")->asGrid();
118 
119 	CSG_Grid	Input, *pResult	= Parameters("RESULT")->asGrid();
120 
121 	if( !pResult || pResult == m_pInput )
122 	{
123 		Input.Create(*m_pInput);
124 
125 		pResult		= m_pInput;
126 		m_pInput	= &Input;
127 	}
128 	else
129 	{
130 		pResult->Fmt_Name("%s [%s]", m_pInput->Get_Name(), _TL("Filter"));
131 
132 		if( Method != 2 )	// Edge...
133 		{
134 			pResult->Set_NoData_Value(m_pInput->Get_NoData_Value());
135 
136 			DataObject_Set_Parameters(pResult, m_pInput);
137 		}
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 			double	Mean;
147 
148 			if( Get_Mean(x, y, Mean) )
149 			{
150 				switch( Method )
151 				{
152 				default:	// Smooth...
153 					pResult->Set_Value(x, y, Mean);
154 					break;
155 
156 				case  1:	// Sharpen...
157 					pResult->Set_Value(x, y, m_pInput->asDouble(x, y) + (m_pInput->asDouble(x, y) - Mean));
158 					break;
159 
160 				case  2:	// Edge...
161 					pResult->Set_Value(x, y, m_pInput->asDouble(x, y) - Mean);
162 					break;
163 				}
164 			}
165 			else
166 			{
167 				pResult->Set_NoData(x, y);
168 			}
169 		}
170 	}
171 
172 	//-------------------------------------------------
173 	if( pResult == Parameters("INPUT")->asGrid() )
174 	{
175 		DataObject_Update(pResult);
176 	}
177 
178 	m_Kernel.Destroy();
179 
180 	//-----------------------------------------------------
181 	return( true );
182 }
183 
184 
185 ///////////////////////////////////////////////////////////
186 //														 //
187 ///////////////////////////////////////////////////////////
188 
189 //---------------------------------------------------------
Get_Mean(int x,int y,double & Value)190 bool CFilter::Get_Mean(int x, int y, double &Value)
191 {
192 	CSG_Simple_Statistics	s;
193 
194 	if( m_pInput->is_InGrid(x, y) )
195 	{
196 		for(int i=0; i<m_Kernel.Get_Count(); i++)
197 		{
198 			int	ix	= m_Kernel.Get_X(i, x);
199 			int	iy	= m_Kernel.Get_Y(i, y);
200 
201 			if( m_pInput->is_InGrid(ix, iy) )
202 			{
203 				s	+= m_pInput->asDouble(ix, iy);
204 			}
205 		}
206 	}
207 
208 	if( s.Get_Count() > 0 )
209 	{
210 		Value	= s.Get_Mean();
211 
212 		return( true );
213 	}
214 
215 	return( false );
216 }
217 
218 
219 ///////////////////////////////////////////////////////////
220 //														 //
221 //														 //
222 //														 //
223 ///////////////////////////////////////////////////////////
224 
225 //---------------------------------------------------------
226