1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                  Grid_Visualisation                   //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //                 Grid_Terrain_Map.cpp                  //
17 //                                                       //
18 //                 Copyright (C) 2014 by                 //
19 //                    Volker Wichmann                    //
20 //                                                       //
21 //-------------------------------------------------------//
22 //                                                       //
23 // This file is part of 'SAGA - System for Automated     //
24 // Geoscientific Analyses'. SAGA is free software; you   //
25 // can redistribute it and/or modify it under the terms  //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the     //
28 // License, or (at your option) any later version.       //
29 //                                                       //
30 // SAGA is distributed in the hope that it will be       //
31 // useful, but WITHOUT ANY WARRANTY; without even the    //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
33 // PARTICULAR PURPOSE. See the GNU General Public        //
34 // License for more details.                             //
35 //                                                       //
36 // You should have received a copy of the GNU General    //
37 // Public License along with this program; if not, see   //
38 // <http://www.gnu.org/licenses/>.                       //
39 //                                                       //
40 //-------------------------------------------------------//
41 //                                                       //
42 //    e-mail:     wichmann@alps-gmbh.com                 //
43 //                                                       //
44 //    contact:    alpS GmbH                              //
45 //                Center for Climate Change Adaptation   //
46 //                Grabenweg 68                           //
47 //                6020 Innsbruck                         //
48 //                Austria                                //
49 //                www.alps-gmbh.com                      //
50 //                                                       //
51 ///////////////////////////////////////////////////////////
52 
53 //---------------------------------------------------------
54 
55 
56 ///////////////////////////////////////////////////////////
57 //														 //
58 //														 //
59 //														 //
60 ///////////////////////////////////////////////////////////
61 
62 //---------------------------------------------------------
63 #include "Grid_Terrain_Map.h"
64 
65 
66 ///////////////////////////////////////////////////////////
67 //														 //
68 //														 //
69 //														 //
70 ///////////////////////////////////////////////////////////
71 
72 //---------------------------------------------------------
73 #define RUN_TOOL(LIBRARY, TOOL, CONDITION)	{\
74 	bool	bResult;\
75 	SG_RUN_TOOL(bResult, LIBRARY, TOOL, CONDITION)\
76 	if( !bResult ) return( false );\
77 }
78 
79 #define SET_PARAMETER(IDENTIFIER, VALUE)	pTool->Get_Parameters()->Set_Parameter(SG_T(IDENTIFIER), VALUE)
80 
81 
82 //---------------------------------------------------------
CGrid_Terrain_Map(void)83 CGrid_Terrain_Map::CGrid_Terrain_Map(void)
84 {
85 	//-----------------------------------------------------
86 	Set_Name(_TL("Terrain Map View"));
87 
88 	Set_Author(_TL("Copyrights (c) 2014 by Volker Wichmann"));
89 
90 	Set_Description	(_TW(
91 		"This tool allows one to create different terrain visualisations from an elevation dataset:\n\n"
92 		"* Topography: a simple map with an analytical hillshading of the terrain\n\n"
93 		"* Morphology: a map which visualizes the terrain by combining positive and "
94 		"negative openness (Yokoyama et al. 2002) with terrain slope in a single map. "
95 		"In contrast to conventional shading methods this has the advantage of being "
96 		"independent from the direction of the light source.\n\n"
97 		"References:\n"
98 		"Yokoyama, R. / Shirasawa, M. / Pike, R.J. (2002): "
99 		"Visualizing topography by openness: A new application of image processing to digital elevation models. "
100 		"Photogrammetric Engineering and Remote Sensing, Vol.68, pp.251-266. "
101 		"<a target=\"_blank\" href=\"http://info.asprs.org/publications/pers/2002journal/march/2002_mar_257-265.pdf\">online at ASPRS</a>.\n\n")
102 	);
103 
104 
105 	//-----------------------------------------------------
106 	Parameters.Add_Grid(
107 		NULL	, "DEM"		,_TL("DEM"),
108 		_TL("Digital elevation model."),
109 		PARAMETER_INPUT
110 	);
111 
112 	Parameters.Add_Grid(
113 		NULL	, "SHADE"	, _TL("Shade"),
114 		_TL("The shaded DTM."),
115 		PARAMETER_OUTPUT_OPTIONAL
116 	);
117 
118 	Parameters.Add_Grid(
119 		NULL	, "OPENNESS"	, _TL("Openness"),
120 		_TL("The difference of positive and negative openness."),
121 		PARAMETER_OUTPUT_OPTIONAL
122 	);
123 
124 	Parameters.Add_Grid(
125 		NULL	, "SLOPE"	, _TL("Slope"),
126 		_TL("The calculated terrain slope [radians]."),
127 		PARAMETER_OUTPUT_OPTIONAL
128 	);
129 
130 	Parameters.Add_Shapes(
131 		NULL	, "CONTOURS"	, _TL("Contours"),
132 		_TL("The generated contour lines."),
133 		PARAMETER_OUTPUT_OPTIONAL
134 	);
135 
136 	Parameters.Add_Choice(
137 		NULL	, "METHOD"	,	_TL("Method"),
138 		_TL("Choose the map type to generate."),
139 		CSG_String::Format(SG_T("%s|%s"),
140 			_TL("Topography"),
141 			_TL("Morphology")
142 		), 0
143 	);
144 
145 	Parameters.Add_Value(
146 		NULL	, "RADIUS"		, _TL("Radial Limit"),
147 		_TL("Radial search limit for openness calculation."),
148 		PARAMETER_TYPE_Double	, 1000.0, 0.0, true
149 	);
150 
151 	Parameters.Add_Value(
152 		NULL	, "CONTOUR_LINES"	, _TL("Contour Lines"),
153 		_TL("Derive contour lines."),
154 		PARAMETER_TYPE_Bool		, true
155 	);
156 
157 	Parameters.Add_Value(
158 		Parameters("CONTOUR_LINES")	, "EQUIDISTANCE"	, _TL("Equidistance"),
159 		_TL("Contour lines equidistance [map units]."),
160 		PARAMETER_TYPE_Double	, 50.0, 0.0, true
161 	);
162 }
163 
164 
165 //---------------------------------------------------------
~CGrid_Terrain_Map(void)166 CGrid_Terrain_Map::~CGrid_Terrain_Map(void)
167 {}
168 
169 
170 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)171 int CGrid_Terrain_Map::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
172 {
173 	//-----------------------------------------------------
174 	if(	pParameter->Cmp_Identifier(SG_T("METHOD")) )
175 	{
176 		pParameters->Get_Parameter("SHADE")			->Set_Enabled(pParameter->asInt() == 0);
177 
178 		pParameters->Get_Parameter("OPENNESS")		->Set_Enabled(pParameter->asInt() == 1);
179 		pParameters->Get_Parameter("SLOPE")			->Set_Enabled(pParameter->asInt() == 1);
180 		pParameters->Get_Parameter("RADIUS")		->Set_Enabled(pParameter->asInt() == 1);
181 	}
182 
183 	//-----------------------------------------------------
184 	if(	pParameter->Cmp_Identifier(SG_T("CONTOUR_LINES")) )
185 	{
186 		pParameters->Get_Parameter("CONTOURS")		->Set_Enabled(pParameter->asBool());
187 		pParameters->Get_Parameter("EQUIDISTANCE")	->Set_Enabled(pParameter->asBool());
188 	}
189 
190 	//-----------------------------------------------------
191 	return( 0 );
192 }
193 
194 
195 ///////////////////////////////////////////////////////////
196 //														 //
197 //														 //
198 //														 //
199 ///////////////////////////////////////////////////////////
200 
201 //---------------------------------------------------------
On_Execute(void)202 bool CGrid_Terrain_Map::On_Execute(void)
203 {
204 	bool	bOkay = false;
205 
206 	//-----------------------------------------------------
207 	switch( Parameters("METHOD")->asInt() )
208 	{
209 	default:
210 	case 0:		bOkay = Generate_Topography();		break;
211 	case 1:		bOkay = Generate_Morphology();		break;
212 	}
213 
214 	if( !bOkay )
215 	{
216 		return( false );
217 	}
218 
219 	//-----------------------------------------------------
220 	if( Parameters("CONTOUR_LINES")->asBool() )
221 	{
222 		return( Generate_Contours() );
223 	}
224 
225 	//-----------------------------------------------------
226 	return( true );
227 
228 }
229 
230 
231 //---------------------------------------------------------
Generate_Topography()232 bool CGrid_Terrain_Map::Generate_Topography()
233 {
234 	CSG_Grid	*pShade;
235 
236 	pShade		= Parameters("SHADE")->asGrid();
237 
238 
239 	//-----------------------------------------------------
240 	if( pShade == NULL )
241 	{
242 		pShade = SG_Create_Grid(Get_System(), SG_DATATYPE_Float);
243 		Parameters("SHADE")->Set_Value(pShade);
244 		DataObject_Add(pShade);
245 	}
246 
247 
248 	//-----------------------------------------------------
249 	RUN_TOOL("ta_lighting"			, 0,
250 			SET_PARAMETER("ELEVATION"	, Parameters("DEM"))
251 		&&	SET_PARAMETER("SHADE"		, pShade)
252 		&&	SET_PARAMETER("METHOD"		, 0)
253 	)
254 
255 
256 	//-----------------------------------------------------
257 	DataObject_Set_Colors(Parameters("DEM")->asGrid()	, 11, SG_COLORS_TOPOGRAPHY , false);
258 	DataObject_Set_Colors(pShade						, 11, SG_COLORS_BLACK_WHITE, true);
259 
260 
261 	CSG_Parameters	Parms;
262 
263 	if( DataObject_Get_Parameters(pShade, Parms) && Parms("DISPLAY_TRANSPARENCY") )
264 	{
265 		Parms("DISPLAY_TRANSPARENCY")->Set_Value(40);
266 
267 		DataObject_Set_Parameters(pShade, Parms);
268 	}
269 
270 
271 	pShade->Fmt_Name("%s (%s)", _TL("Shading"), Parameters("DEM")->asGrid()->Get_Name());
272 	DataObject_Update(Parameters("DEM")->asGrid()	, SG_UI_DATAOBJECT_SHOW_NEW_MAP);
273 	DataObject_Update(pShade						, SG_UI_DATAOBJECT_SHOW_LAST_MAP);
274 
275 
276 	//-----------------------------------------------------
277 	return( true );
278 }
279 
280 
281 //---------------------------------------------------------
Generate_Morphology()282 bool CGrid_Terrain_Map::Generate_Morphology()
283 {
284 
285 	CSG_Grid	*pOpenness, *pSlope;
286 	double		dRadius;
287 
288 	pOpenness	= Parameters("OPENNESS")->asGrid();
289 	pSlope		= Parameters("SLOPE")->asGrid();
290 	dRadius		= Parameters("RADIUS")->asDouble();
291 
292 
293 	//-----------------------------------------------------
294 	if( pOpenness == NULL )
295 	{
296 		pOpenness = new CSG_Grid(Get_System(), SG_DATATYPE_Float);
297 		Parameters("OPENNESS")->Set_Value(pOpenness);
298 		DataObject_Add(pOpenness);
299 	}
300 
301 	if( pSlope == NULL )
302 	{
303 		pSlope = SG_Create_Grid(Get_System(), SG_DATATYPE_Float);
304 		Parameters("SLOPE")->Set_Value(pSlope);
305 		DataObject_Add(pSlope);
306 	}
307 
308 	CSG_Grid	TMP1(Get_System(), SG_DATATYPE_Float);
309 
310 
311 	//-----------------------------------------------------
312 	RUN_TOOL("ta_lighting"			, 5,
313 			SET_PARAMETER("DEM"			, Parameters("DEM"))
314 		&&	SET_PARAMETER("POS"			, pOpenness)
315 		&&	SET_PARAMETER("NEG"			, &TMP1)
316 		&&	SET_PARAMETER("RADIUS"		, dRadius)
317 		&&	SET_PARAMETER("METHOD"		, 1)
318 		&&	SET_PARAMETER("NDIRS"		, 8)
319 	)
320 
321 	pOpenness->Subtract(TMP1);
322 
323 
324 	//-----------------------------------------------------
325 	RUN_TOOL("ta_morphometry"			, 0,
326 			SET_PARAMETER("ELEVATION"	, Parameters("DEM"))
327 		&&	SET_PARAMETER("SLOPE"		, pSlope)
328 		&&	SET_PARAMETER("ASPECT"		, &TMP1)
329 	)
330 
331 
332 	//-----------------------------------------------------
333 	DataObject_Set_Colors(pOpenness, 11, SG_COLORS_BLACK_WHITE, false);
334 	DataObject_Set_Colors(pSlope   , 11, SG_COLORS_WHITE_RED  , false);
335 
336 	CSG_Parameters	Parms;
337 
338 	if( DataObject_Get_Parameters(pSlope, Parms) && Parms("DISPLAY_TRANSPARENCY") )
339 	{
340 		Parms("DISPLAY_TRANSPARENCY")->Set_Value(60);
341 
342 		DataObject_Set_Parameters(pSlope, Parms);
343 	}
344 
345 
346 	pOpenness->Fmt_Name("%s (%s)", _TL("Openness"), Parameters("DEM")->asGrid()->Get_Name());
347 	pSlope->Fmt_Name("%s (%s)", _TL("Slope"), Parameters("DEM")->asGrid()->Get_Name());
348 	DataObject_Update(pOpenness	, SG_UI_DATAOBJECT_SHOW_NEW_MAP);
349 	DataObject_Update(pSlope	, SG_UI_DATAOBJECT_SHOW_LAST_MAP);
350 
351 
352 	//-----------------------------------------------------
353 	return( true );
354 
355 }
356 
357 
358 //---------------------------------------------------------
Generate_Contours()359 bool CGrid_Terrain_Map::Generate_Contours()
360 {
361 	CSG_Shapes	*pContours;
362 
363 	pContours	= Parameters("CONTOURS")->asShapes();
364 
365 
366 	//-----------------------------------------------------
367 	if( pContours == NULL )
368 	{
369 		pContours = SG_Create_Shapes(SHAPE_TYPE_Line);
370 		Parameters("CONTOURS")->Set_Value(pContours);
371 		DataObject_Add(pContours);
372 	}
373 
374 
375 	//-----------------------------------------------------
376 	RUN_TOOL("shapes_grid"			, 5,
377 			SET_PARAMETER("GRID"		, Parameters("DEM"))
378 		&&	SET_PARAMETER("CONTOUR"		, pContours)
379 		&&	SET_PARAMETER("ZSTEP"		, Parameters("EQUIDISTANCE"))
380 	)
381 
382 
383 	//-----------------------------------------------------
384 	CSG_Parameters	Parms;
385 
386 	if( DataObject_Get_Parameters(pContours, Parms) && Parms("UNISYMBOL_COLOR") && Parms("DISPLAY_TRANSPARENCY") )
387 	{
388 		Parms("UNISYMBOL_COLOR")->Set_Value(0);
389 		Parms("DISPLAY_TRANSPARENCY")->Set_Value(70);
390 
391 		DataObject_Set_Parameters(pContours, Parms);
392 	}
393 
394 	pContours->Fmt_Name("%s (%s)", _TL("Contours"), Parameters("DEM")->asGrid()->Get_Name());
395 	DataObject_Update(pContours, SG_UI_DATAOBJECT_SHOW_LAST_MAP);
396 
397 
398 	//-----------------------------------------------------
399 	return( true );
400 }
401 
402 
403 ///////////////////////////////////////////////////////////
404 //														 //
405 //														 //
406 //														 //
407 ///////////////////////////////////////////////////////////
408 
409 //---------------------------------------------------------
410