1
2 ///////////////////////////////////////////////////////////
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Tool Library //
9 // grid_tools //
10 // //
11 //-------------------------------------------------------//
12 // //
13 // Grid_Aggregate.cpp //
14 // //
15 // Copyright (C) 2005 by //
16 // Victor Olaya //
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 "Grid_Aggregate.h"
52
53
54 ///////////////////////////////////////////////////////////
55 // //
56 // //
57 // //
58 ///////////////////////////////////////////////////////////
59
60 //---------------------------------------------------------
CGrid_Aggregate(void)61 CGrid_Aggregate::CGrid_Aggregate(void)
62 {
63 Set_Name (_TL("Aggregate"));
64
65 Set_Author ("V.Olaya (c) 2005");
66
67 Set_Description (_TW(
68 "Resamples a raster layer to a lower resolution, aggregating"
69 "the values of a group of cells. This should be used in any case in which a normal"
70 "resampling will result in wrong values in the resulting layer, such as, for instance,"
71 "the number of elements of a given class in each cell."
72 ));
73
74 //-----------------------------------------------------
75 Parameters.Add_Grid("",
76 "INPUT" , _TL("Grid"),
77 _TL(""),
78 PARAMETER_INPUT
79 );
80
81 Parameters.Add_Grid_Output("",
82 "OUTPUT" , _TL("Aggregated Grid"),
83 _TL("")
84 );
85
86 Parameters.Add_Int("",
87 "SIZE" , _TL("Aggregation Size"),
88 _TL(""),
89 2, 2, true
90 );
91
92 Parameters.Add_Choice("",
93 "METHOD" , _TL("Method"),
94 _TL(""),
95 CSG_String::Format("%s|%s|%s|%s|%s|%s",
96 _TL("Sum"),
97 _TL("Minimum"),
98 _TL("Maximum"),
99 _TL("Median"),
100 _TL("Mean"),
101 _TL("Mode")
102 ), 4
103 );
104 }
105
106
107 ///////////////////////////////////////////////////////////
108 // //
109 ///////////////////////////////////////////////////////////
110
111 //---------------------------------------------------------
On_Execute(void)112 bool CGrid_Aggregate::On_Execute(void)
113 {
114 int Size = Parameters("SIZE")->asInt();
115
116 CSG_Grid_System System(Get_Cellsize() * Size, Get_XMin(), Get_YMin(), Get_NX() / Size, Get_NY() / Size);
117
118 CSG_Grid *pOutput, *pGrid = Parameters("INPUT")->asGrid();
119
120 Parameters("OUTPUT")->Set_Value(pOutput = SG_Create_Grid(System, pGrid->Get_Type()));
121
122 pOutput->Set_Name(pGrid->Get_Name());
123
124 int Method = Parameters("METHOD")->asInt();
125
126 //-----------------------------------------------------
127 #pragma omp parallel for
128 for(int y=0; y<System.Get_NY(); y++)
129 {
130 int yy = -Size/2 + y * Size;
131
132 for(int x=0, xx=-Size/2; x<System.Get_NX(); xx+=Size, x++)
133 {
134 if( Method == 5 ) // Mode
135 {
136 CSG_Unique_Number_Statistics s;
137
138 for(int iy=yy; iy<yy+Size; iy++)
139 {
140 for(int ix=xx; ix<xx+Size; ix++)
141 {
142 if( pGrid->is_InGrid(ix, iy) )
143 {
144 s += pGrid->asDouble(ix, iy);
145 }
146 }
147 }
148
149 double Value;
150
151 if( s.Get_Majority(Value) == false )
152 {
153 pOutput->Set_NoData(x, y);
154 }
155 else
156 {
157 pOutput->Set_Value(x, y, Value);
158 }
159 }
160
161 //---------------------------------------------
162 else
163 {
164 CSG_Simple_Statistics s(Method == 3);
165
166 for(int iy=yy; iy<yy+Size; iy++)
167 {
168 for(int ix=xx; ix<xx+Size; ix++)
169 {
170 if( pGrid->is_InGrid(ix, iy) )
171 {
172 s += pGrid->asDouble(ix, iy);
173 }
174 }
175 }
176
177 if( s.Get_Count() == 0 )
178 {
179 pOutput->Set_NoData(x, y);
180 }
181 else switch( Method )
182 {
183 default: pOutput->Set_Value(x, y, s.Get_Sum ()); break;
184 case 1: pOutput->Set_Value(x, y, s.Get_Minimum()); break;
185 case 2: pOutput->Set_Value(x, y, s.Get_Maximum()); break;
186 case 3: pOutput->Set_Value(x, y, s.Get_Median ()); break;
187 case 4: pOutput->Set_Value(x, y, s.Get_Mean ()); break;
188 }
189 }
190 }
191 }
192
193 //-----------------------------------------------------
194 return( true );
195 }
196
197
198 ///////////////////////////////////////////////////////////
199 // //
200 // //
201 // //
202 ///////////////////////////////////////////////////////////
203
204 //---------------------------------------------------------
205