1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                       Lectures                        //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                   Exercise_08.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 "Exercise_08.h"
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
CExercise_08(void)61 CExercise_08::CExercise_08(void)
62 {
63 	Set_Name		(_TL("08: Extended neighbourhoods - catchment areas (parallel)"));
64 
65 	Set_Author		("O.Conrad (c) 2006");
66 
67 	Set_Description	(_TW(
68 		"Extended Neighbourhoods - Catchment areas."
69 	));
70 
71 	Add_Reference("Conrad, O.", "2007",
72 		"SAGA - Entwurf, Funktionsumfang und Anwendung eines Systems f�r Automatisierte Geowissenschaftliche Analysen",
73 		"ediss.uni-goettingen.de.", SG_T("http://hdl.handle.net/11858/00-1735-0000-0006-B26C-6"), SG_T("Online")
74 	);
75 
76 
77 	//-----------------------------------------------------
78 	Parameters.Add_Grid(
79 		"", "ELEVATION"	, _TL("Elevation grid"),
80 		_TL("This must be your input data of type grid."),
81 		PARAMETER_INPUT
82 	);
83 
84 	Parameters.Add_Grid(
85 		"", "AREA"		, _TL("Catchment area"),
86 		_TL("This will contain your output data of type grid."),
87 		PARAMETER_OUTPUT
88 	);
89 
90 	Parameters.Add_Choice(
91 		"", "METHOD"		, _TL("Method"),
92 		_TL("Choose a method"),
93 		CSG_String::Format("%s|%s",
94 			_TL("D8"),
95 			_TL("MFD")
96 		)
97 	);
98 }
99 
100 
101 ///////////////////////////////////////////////////////////
102 //														 //
103 ///////////////////////////////////////////////////////////
104 
105 //---------------------------------------------------------
On_Execute(void)106 bool CExercise_08::On_Execute(void)
107 {
108 	//-----------------------------------------------------
109 	// Get parameter settings...
110 
111 	m_pDTM	= Parameters("ELEVATION")->asGrid();
112 
113 	m_pArea	= Parameters("AREA")->asGrid();
114 
115 	m_pArea->Assign(0.);
116 	m_pArea->Set_Unit("m^2");
117 
118 	DataObject_Set_Colors(m_pArea, 100, SG_COLORS_WHITE_BLUE);
119 
120 
121 	//-----------------------------------------------------
122 	// Execute calculation...
123 
124 	switch( Parameters("METHOD")->asInt() )
125 	{
126 	default: return( Method_01() );
127 	case  1: return( Method_02() );
128 	}
129 }
130 
131 
132 ///////////////////////////////////////////////////////////
133 //														 //
134 ///////////////////////////////////////////////////////////
135 
136 //---------------------------------------------------------
Method_01(void)137 bool CExercise_08::Method_01(void)
138 {
139 	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
140 	{
141 		int	x, y; m_pDTM->Get_Sorted(n, x, y, true, false);
142 
143 		if( m_pDTM->is_NoData(x, y) )
144 		{
145 			m_pArea->Set_NoData(x, y);
146 		}
147 		else
148 		{
149 			m_pArea->Add_Value(x, y, Get_Cellarea());
150 
151 			double z = m_pDTM->asDouble(x, y), dzMax = 0.; int iMax = -1;
152 
153 			for(int i=0; i<8; i++)
154 			{
155 				int	ix	= Get_xTo(i, x);
156 				int	iy	= Get_yTo(i, y);
157 
158 				if( is_InGrid(ix, iy) && !m_pDTM->is_NoData(ix, iy) )
159 				{
160 					double	dz	= (z - m_pDTM->asDouble(ix, iy)) / Get_Length(i);
161 
162 					if( dz > 0. && (iMax < 0 || (iMax >= 0 && dzMax < dz)) )
163 					{
164 						iMax	= i;
165 						dzMax	= dz;
166 					}
167 				}
168 			}
169 
170 			if( iMax >= 0 )
171 			{
172 				int ix	= Get_xTo(iMax, x);
173 				int	iy	= Get_yTo(iMax, y);
174 
175 				m_pArea->Add_Value(ix, iy, m_pArea->asDouble(x, y));
176 			}
177 		}
178 	}
179 
180 	//-----------------------------------------------------
181 	return( true );
182 }
183 
184 //---------------------------------------------------------
Method_02(void)185 bool CExercise_08::Method_02(void)
186 {
187 	const double	MFD_Converge	= 1.1;
188 
189 	for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++)
190 	{
191 		int x, y; m_pDTM->Get_Sorted(n, x, y, true, false);
192 
193 		if( m_pDTM->is_NoData(x, y) )
194 		{
195 			m_pArea->Set_NoData(x, y);
196 		}
197 		else
198 		{
199 			m_pArea->Add_Value(x, y, Get_Cellarea());
200 
201 			double z = m_pDTM->asDouble(x, y), d, dz[8], dzSum = 0.;
202 
203 			for(int i=0; i<8; i++)
204 			{
205 				int	ix	= Get_xTo(i, x);
206 				int	iy	= Get_yTo(i, y);
207 
208 				if( is_InGrid(ix, iy) && !m_pDTM->is_NoData(ix, iy) && (d = z - m_pDTM->asDouble(ix, iy)) > 0. )
209 				{
210 					dz[i]	= pow(d / Get_Length(i), MFD_Converge);
211 					dzSum	+= dz[i];
212 				}
213 				else
214 				{
215 					dz[i]	= 0.;
216 				}
217 			}
218 
219 			if( dzSum > 0. )
220 			{
221 				d	= m_pArea->asDouble(x, y) / dzSum;
222 
223 				for(int i=0; i<8; i++)
224 				{
225 					if( dz[i] > 0. )
226 					{
227 						int	ix	= Get_xTo(i, x);
228 						int	iy	= Get_yTo(i, y);
229 
230 						m_pArea->Add_Value(ix, iy, dz[i] * d);
231 					}
232 				}
233 			}
234 		}
235 	}
236 
237 	//-----------------------------------------------------
238 	return( true );
239 }
240 
241 
242 ///////////////////////////////////////////////////////////
243 //														 //
244 //														 //
245 //														 //
246 ///////////////////////////////////////////////////////////
247 
248 //---------------------------------------------------------
249