1 /**********************************************************
2 * Version $Id$
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_Volume.cpp //
17 // //
18 // Copyright (C) 2003 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 Goettingen //
47 // Goldschmidtstr. 5 //
48 // 37077 Goettingen //
49 // Germany //
50 // //
51 ///////////////////////////////////////////////////////////
52
53 //---------------------------------------------------------
54
55
56 ///////////////////////////////////////////////////////////
57 // //
58 // //
59 // //
60 ///////////////////////////////////////////////////////////
61
62 //---------------------------------------------------------
63 #include "Grid_Volume.h"
64
65
66 ///////////////////////////////////////////////////////////
67 // //
68 // //
69 // //
70 ///////////////////////////////////////////////////////////
71
72 //---------------------------------------------------------
CGrid_Volume(void)73 CGrid_Volume::CGrid_Volume(void)
74 {
75 Set_Name(_TL("Grid Volume"));
76
77 Set_Author (SG_T("(c) 2005 by O.Conrad"));
78
79 Set_Description(
80 _TL("Calculate the volume under the grid's surface. This is mainly useful for Digital Elevation Models (DEM).")
81 );
82
83 Parameters.Add_Grid(
84 NULL , "GRID" , _TL("Grid"),
85 _TL(""),
86 PARAMETER_INPUT
87 );
88
89 Parameters.Add_Choice(
90 NULL , "METHOD" , _TL("Method"),
91 _TL(""),
92 CSG_String::Format(SG_T("%s|%s|%s|%s|"),
93 _TL("Count Only Above Base Level"),
94 _TL("Count Only Below Base Level"),
95 _TL("Subtract Volumes Below Base Level"),
96 _TL("Add Volumes Below Base Level")
97 )
98 );
99
100 Parameters.Add_Value(
101 NULL , "LEVEL" , _TL("Base Level"),
102 _TL(""),
103 PARAMETER_TYPE_Double, 0.0
104 );
105 }
106
107 //---------------------------------------------------------
~CGrid_Volume(void)108 CGrid_Volume::~CGrid_Volume(void)
109 {}
110
111
112 ///////////////////////////////////////////////////////////
113 // //
114 // //
115 // //
116 ///////////////////////////////////////////////////////////
117
118 //---------------------------------------------------------
On_Execute(void)119 bool CGrid_Volume::On_Execute(void)
120 {
121 int x, y, Method;
122 double Level, Volume, z;
123 CSG_Grid *pGrid;
124 CSG_String s;
125
126 //-----------------------------------------------------
127 pGrid = Parameters("GRID") ->asGrid();
128 Level = Parameters("LEVEL") ->asDouble();
129 Method = Parameters("METHOD") ->asInt();
130
131 //-----------------------------------------------------
132 for(y=0, Volume=0.0; y<Get_NY() && Set_Progress(y); y++)
133 {
134 for(x=0; x<Get_NX(); x++)
135 {
136 if( !pGrid->is_NoData(x, y) )
137 {
138 z = pGrid->asDouble(x, y) - Level;
139
140 switch( Method )
141 {
142 case 0: // Count Only Above Base Level
143 if( z > 0.0 )
144 {
145 Volume += z;
146 }
147 break;
148
149 case 1: // Count Only Below Base Level
150 if( z < 0.0 )
151 {
152 Volume -= z;
153 }
154 break;
155
156 case 2: // Subtract Volumes Below Base Level
157 Volume += z;
158 break;
159
160 case 3: // Add Volumes Below Base Level
161 Volume += fabs(z);
162 break;
163 }
164 }
165 }
166 }
167
168 //-----------------------------------------------------
169 Volume *= pGrid->Get_Cellarea();
170
171 s.Printf(_TL("Volume: %f"), Volume);
172
173 Message_Add(s);
174 Message_Dlg(s, _TL("Grid Volume"));
175
176 //-----------------------------------------------------
177 return( true );
178 }
179
180
181 ///////////////////////////////////////////////////////////
182 // //
183 // //
184 // //
185 ///////////////////////////////////////////////////////////
186
187 //---------------------------------------------------------
188