1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                     grid_spline                       //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                Gridding_Spline_Base.cpp               //
14 //                                                       //
15 //                 Copyright (C) 2006 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 "Gridding_Spline_Base.h"
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
CGridding_Spline_Base(bool bGridPoints)61 CGridding_Spline_Base::CGridding_Spline_Base(bool bGridPoints)
62 {
63 	if( bGridPoints )
64 	{
65 		Parameters.Add_Grid("",
66 			"GRID"	, _TL("Grid"),
67 			_TL(""),
68 			PARAMETER_INPUT
69 		);
70 	}
71 	else
72 	{
73 		Parameters.Add_Shapes("",
74 			"SHAPES", _TL("Points"),
75 			_TL(""),
76 			PARAMETER_INPUT
77 		);
78 
79 		Parameters.Add_Table_Field("SHAPES",
80 			"FIELD"	, _TL("Attribute"),
81 			_TL("")
82 		);
83 	}
84 
85 	m_Grid_Target.Create(&Parameters, true, "", "TARGET_");
86 }
87 
88 
89 ///////////////////////////////////////////////////////////
90 //														 //
91 ///////////////////////////////////////////////////////////
92 
93 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)94 int CGridding_Spline_Base::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
95 {
96 	if( pParameter->Cmp_Identifier("SHAPES") )
97 	{
98 		m_Grid_Target.Set_User_Defined(pParameters, pParameter->asShapes());
99 	}
100 
101 	if( pParameter->Cmp_Identifier("GRID") && pParameter->asGrid() )
102 	{
103 		m_Grid_Target.Set_User_Defined(pParameters, pParameter->asGrid()->Get_System());
104 	}
105 
106 	m_Grid_Target.On_Parameter_Changed(pParameters, pParameter);
107 
108 	return( CSG_Tool::On_Parameter_Changed(pParameters, pParameter) );
109 }
110 
111 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)112 int CGridding_Spline_Base::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
113 {
114 	m_Grid_Target.On_Parameters_Enable(pParameters, pParameter);
115 
116 	return( CSG_Tool::On_Parameters_Enable(pParameters, pParameter) );
117 }
118 
119 
120 ///////////////////////////////////////////////////////////
121 //														 //
122 ///////////////////////////////////////////////////////////
123 
124 //---------------------------------------------------------
Initialize(CSG_Points_Z & Points,bool bInGridOnly,bool bDetrend)125 bool CGridding_Spline_Base::Initialize(CSG_Points_Z &Points, bool bInGridOnly, bool bDetrend)
126 {
127 	return( _Get_Grid() && _Get_Points(Points, bInGridOnly, bDetrend) );
128 }
129 
130 //---------------------------------------------------------
Initialize(void)131 bool CGridding_Spline_Base::Initialize(void)
132 {
133 	return( _Get_Grid() );
134 }
135 
136 //---------------------------------------------------------
Finalize(bool bDetrend)137 bool CGridding_Spline_Base::Finalize(bool bDetrend)
138 {
139 	if( bDetrend )
140 	{
141 		double	Mean	= Parameters("GRID")
142 			? Parameters("GRID"  )->asGrid  ()->Get_Mean()
143 			: Parameters("SHAPES")->asShapes()->Get_Mean(Parameters("FIELD")->asInt());
144 
145 		if( Mean )
146 		{
147 			for(sLong i=0; i<m_pGrid->Get_NCells(); i++)
148 			{
149 				m_pGrid->Add_Value(i, Mean);
150 			}
151 		}
152 	}
153 
154 	return( true );
155 }
156 
157 
158 ///////////////////////////////////////////////////////////
159 //														 //
160 ///////////////////////////////////////////////////////////
161 
162 //---------------------------------------------------------
_Get_Grid(void)163 bool CGridding_Spline_Base::_Get_Grid(void)
164 {
165 	if( (m_pGrid = m_Grid_Target.Get_Grid()) == NULL )
166 	{
167 		return( false );
168 	}
169 
170 	m_pGrid->Assign_NoData();
171 
172 	//-----------------------------------------------------
173 	if( Parameters("GRID") )
174 	{
175 		CSG_Grid	*pPoints	= Parameters("GRID")->asGrid();
176 
177 		m_pGrid->Fmt_Name("%s [%s]"   , pPoints->Get_Name(), Get_Name().c_str());
178 	}
179 	else
180 	{
181 		CSG_Shapes	*pPoints	= Parameters("SHAPES")->asShapes();
182 
183 		m_pGrid->Fmt_Name("%s.%s [%s]", pPoints->Get_Name(), Parameters("FIELD")->asString(), Get_Name().c_str());
184 	}
185 
186 	//-----------------------------------------------------
187 	return( true );
188 }
189 
190 
191 ///////////////////////////////////////////////////////////
192 //														 //
193 ///////////////////////////////////////////////////////////
194 
195 //---------------------------------------------------------
_Get_Points(CSG_Points_Z & Points,bool bInGridOnly,bool bDetrend)196 bool CGridding_Spline_Base::_Get_Points(CSG_Points_Z &Points, bool bInGridOnly, bool bDetrend)
197 {
198 	Points.Clear();
199 
200 	//-----------------------------------------------------
201 	if( Parameters("GRID") )
202 	{
203 		CSG_Grid	*pPoints	= Parameters("GRID")->asGrid();
204 
205 		double	Mean	= bDetrend ? pPoints->Get_Mean() : 0.;
206 
207 		TSG_Point	p; p.y	= pPoints->Get_YMin();
208 
209 		for(int y=0; y<pPoints->Get_NY() && Set_Progress(y, pPoints->Get_NY()); y++, p.y+=pPoints->Get_Cellsize())
210 		{
211 			p.x	= pPoints->Get_XMin();
212 
213 			for(int x=0; x<pPoints->Get_NX(); x++, p.x+=pPoints->Get_Cellsize())
214 			{
215 				if( !pPoints->is_NoData(x, y) && (!bInGridOnly || m_pGrid->is_InGrid_byPos(p, false)) )
216 				{
217 					Points.Add(p.x, p.y, pPoints->asDouble(x, y) - Mean);
218 				}
219 			}
220 		}
221 	}
222 
223 	//-----------------------------------------------------
224 	else
225 	{
226 		CSG_Shapes	*pPoints	= Parameters("SHAPES")->asShapes();
227 
228 		int	Field	= Parameters("FIELD")->asInt();
229 
230 		double	Mean	= bDetrend ? pPoints->Get_Mean(Field) : 0.;
231 
232 		for(int i=0; i<pPoints->Get_Count() && Set_Progress(i, pPoints->Get_Count()); i++)
233 		{
234 			CSG_Shape	*pShape	= pPoints->Get_Shape(i);
235 
236 			if( !pShape->is_NoData(Field) )
237 			{
238 				double	z	= pShape->asDouble(Field) - Mean;
239 
240 				for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
241 				{
242 					for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
243 					{
244 						TSG_Point	p	= pShape->Get_Point(iPoint, iPart);
245 
246 						if( !bInGridOnly || m_pGrid->is_InGrid_byPos(p, false) )
247 						{
248 							Points.Add(p.x, p.y, z);
249 						}
250 					}
251 				}
252 			}
253 		}
254 	}
255 
256 	//-----------------------------------------------------
257 	return( Points.Get_Count() >= 3 );
258 }
259 
260 
261 ///////////////////////////////////////////////////////////
262 //														 //
263 //														 //
264 //														 //
265 ///////////////////////////////////////////////////////////
266 
267 //---------------------------------------------------------
268