1 /**********************************************************
2 * Version $Id: grid_metric_conversion.cpp 911 2011-02-14 16:38:15Z reklov_w $
3 *********************************************************/
4
5 ///////////////////////////////////////////////////////////
6 // //
7 // SAGA //
8 // //
9 // System for Automated Geoscientific Analyses //
10 // //
11 // Tool Library //
12 // Grid_Calculus //
13 // //
14 //-------------------------------------------------------//
15 // //
16 // grid_metric_conversion.cpp //
17 // //
18 // Copyright (C) 2011 by //
19 // Olaf Conrad //
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: oconrad@saga-gis.org //
43 // //
44 // contact: Olaf Conrad //
45 // Institute of Geography //
46 // University of Hamburg //
47 // Germany //
48 // //
49 ///////////////////////////////////////////////////////////
50
51 //---------------------------------------------------------
52
53
54 ///////////////////////////////////////////////////////////
55 // //
56 // //
57 // //
58 ///////////////////////////////////////////////////////////
59
60 //---------------------------------------------------------
61 #include "grid_metric_conversion.h"
62
63
64 ///////////////////////////////////////////////////////////
65 // //
66 // //
67 // //
68 ///////////////////////////////////////////////////////////
69
70 //---------------------------------------------------------
CGrid_Metric_Conversion(void)71 CGrid_Metric_Conversion::CGrid_Metric_Conversion(void)
72 {
73 Set_Name (_TL("Metric Conversions"));
74
75 Set_Author (SG_T("O. Conrad (c) 2011"));
76
77 Set_Description (_TW(
78 ""
79 ));
80
81 Parameters.Add_Grid(
82 NULL , "GRID" , _TL("Grid"),
83 _TL(""),
84 PARAMETER_INPUT
85 );
86
87 Parameters.Add_Grid(
88 NULL , "CONV" , _TL("Converted Grid"),
89 _TL(""),
90 PARAMETER_OUTPUT
91 );
92
93 Parameters.Add_Choice(
94 NULL , "CONVERSION" , _TL("Conversion"),
95 _TL(""),
96 CSG_String::Format(SG_T("%s|%s|%s|%s|"),
97 _TL("radians to degree"),
98 _TL("degree to radians"),
99 _TL("Celsius to Fahrenheit"),
100 _TL("Fahrenheit to Celsius")
101 )
102 );
103 }
104
105
106 ///////////////////////////////////////////////////////////
107 // //
108 ///////////////////////////////////////////////////////////
109
110 //---------------------------------------------------------
On_Execute(void)111 bool CGrid_Metric_Conversion::On_Execute(void)
112 {
113 int Conversion;
114 CSG_Grid *pGrid, *pConv;
115
116 //-----------------------------------------------------
117 pGrid = Parameters("GRID") ->asGrid();
118 pConv = Parameters("CONV") ->asGrid();
119 Conversion = Parameters("CONVERSION") ->asInt();
120
121 switch( Conversion )
122 {
123 case 0: pConv->Set_Unit(SG_T("\xbo")); break; // radians to degree
124 case 1: pConv->Set_Unit(SG_T("\xbo")); break; // degree to radians
125 case 2: pConv->Set_Unit(SG_T("\xboF")); break; // Celsius to Fahrenheit
126 case 3: pConv->Set_Unit(SG_T("\xboC")); break; // Fahrenheit to Celsius
127 }
128
129 //-----------------------------------------------------
130 for(int y=0; y<Get_NY() && Set_Progress(y); y++)
131 {
132 for(int x=0; x<Get_NX(); x++)
133 {
134 if( pGrid->is_NoData(x, y) )
135 {
136 pConv->Set_NoData(x, y);
137 }
138 else
139 {
140 double z = pGrid->asDouble(x, y);
141
142 switch( Conversion )
143 {
144 case 0: z = z * M_RAD_TO_DEG; break; // radians to degree
145 case 1: z = z * M_DEG_TO_RAD; break; // degree to radians
146 case 2: z = z * 1.8 + 32.0; break; // Celsius to Fahrenheit
147 case 3: z = (z - 32.0) / 1.8; break; // Fahrenheit to Celsius
148 }
149
150 pConv->Set_Value(x, y, z);
151 }
152 }
153 }
154
155 //-----------------------------------------------------
156 return( true );
157 }
158
159
160 ///////////////////////////////////////////////////////////
161 // //
162 // //
163 // //
164 ///////////////////////////////////////////////////////////
165
166 //---------------------------------------------------------
167