1 /**********************************************************
2 * Version $Id: Grid_FractalDimension.cpp 1921 2014-01-09 10:24:11Z oconrad $
3 *********************************************************/
4
5 ///////////////////////////////////////////////////////////
6 // //
7 // SAGA //
8 // //
9 // System for Automated Geoscientific Analyses //
10 // //
11 // Tool Library //
12 // Fractals //
13 // //
14 //-------------------------------------------------------//
15 // //
16 // Grid_FractalDimension.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_FractalDimension.h"
64
65
66 ///////////////////////////////////////////////////////////
67 // //
68 // //
69 // //
70 ///////////////////////////////////////////////////////////
71
72 //---------------------------------------------------------
CGrid_FractalDimension(void)73 CGrid_FractalDimension::CGrid_FractalDimension(void)
74 {
75 Set_Name (_TL("Fractal Dimension of Grid Surface"));
76
77 Set_Author (SG_T("O.Conrad (c) 2001"));
78
79 Set_Description (_TW(
80 "Calculates surface areas for increasing mesh sizes.")
81 );
82
83 Parameters.Add_Grid (NULL, "INPUT" , _TL("Input") , _TL(""), PARAMETER_INPUT);
84 Parameters.Add_Table(NULL, "RESULT", _TL("Result"), _TL(""), PARAMETER_OUTPUT);
85 Parameters.Add_Value(NULL, "DSIZE" , _TL("Scale" ), _TL(""), PARAMETER_TYPE_Double, 1.5, 1.0001, true);
86 }
87
88
89 ///////////////////////////////////////////////////////////
90 // //
91 // //
92 // //
93 ///////////////////////////////////////////////////////////
94
95 //---------------------------------------------------------
On_Execute(void)96 bool CGrid_FractalDimension::On_Execute(void)
97 {
98 double maxSize, dSize;
99 CSG_Grid *pGrid;
100 CSG_Table *pTable;
101
102 //-----------------------------------------------------
103 pGrid = Parameters("INPUT" )->asGrid();
104 pTable = Parameters("RESULT")->asTable();
105
106 pTable->Destroy();
107 pTable->Set_Name(_TL("Fractal Dimension"));
108
109 pTable->Add_Field(SG_T("CLASS" ), SG_DATATYPE_Int);
110 pTable->Add_Field(SG_T("SCALE" ), SG_DATATYPE_Double);
111 pTable->Add_Field(SG_T("BASAL" ), SG_DATATYPE_Double);
112 pTable->Add_Field(SG_T("SURFACE"), SG_DATATYPE_Double);
113 pTable->Add_Field(SG_T("RATIO" ), SG_DATATYPE_Double);
114 pTable->Add_Field(SG_T("CHANGE" ), SG_DATATYPE_Double);
115
116 //-----------------------------------------------------
117 Get_Area(pGrid, pTable);
118
119 maxSize = 0.5 * (pGrid->Get_XRange() > pGrid->Get_YRange() ? pGrid->Get_XRange() : pGrid->Get_YRange());
120 dSize = Parameters("DSIZE")->asDouble();
121
122 for(double Cellsize=dSize*pGrid->Get_Cellsize(); Cellsize<maxSize && Set_Progress(Cellsize, maxSize); Cellsize*=dSize)
123 {
124 Set_Show_Progress(false);
125
126 CSG_Grid g(CSG_Grid_System(Cellsize,
127 pGrid->Get_XMin(true), pGrid->Get_YMin(true),
128 pGrid->Get_XMax(true), pGrid->Get_YMax(true)
129 ));
130
131 g.Assign(pGrid, GRID_RESAMPLING_BSpline);
132
133 Get_Area(&g, pTable);
134
135 Set_Show_Progress(true);
136 }
137
138 //-----------------------------------------------------
139 return( true );
140 }
141
142
143 ///////////////////////////////////////////////////////////
144 // //
145 // //
146 // //
147 ///////////////////////////////////////////////////////////
148
149 //---------------------------------------------------------
Get_Area(CSG_Grid * pGrid,CSG_Table * pTable)150 void CGrid_FractalDimension::Get_Area(CSG_Grid *pGrid, CSG_Table *pTable)
151 {
152 double scale = pGrid->Get_Cellsize();
153 CSG_Grid g;
154
155 if( !Get_System().is_Equal(pGrid->Get_System()) )
156 {
157 g.Create(Get_System());
158 g.Assign(pGrid, GRID_RESAMPLING_BSpline);
159 pGrid = &g;
160 }
161
162 double basal = 0.0;
163 double surface = 0.0;
164
165 for(int y=0; y<pGrid->Get_NY() && Process_Get_Okay(); y++)
166 {
167 for(int x=0; x<pGrid->Get_NX(); x++)
168 {
169 double s, a;
170
171 if( pGrid->Get_Gradient(x, y, s, a) )
172 {
173 basal += pGrid->Get_Cellarea();
174 surface += pGrid->Get_Cellarea() / cos(s);
175 }
176 }
177 }
178
179 //-----------------------------------------------------
180 if( basal > 0.0 )
181 {
182 CSG_Table_Record *pRecord = pTable->Add_Record();
183
184 pRecord->Set_Value(0, pTable->Get_Count()); // CLASS
185 pRecord->Set_Value(1, scale); // SCALE
186 pRecord->Set_Value(2, basal); // BASAL
187 pRecord->Set_Value(3, surface); // SURFACE
188 pRecord->Set_Value(4, surface / basal); // RATIO
189
190 if( (pRecord = pTable->Get_Record(pTable->Get_Count() - 2)) != NULL )
191 {
192 pRecord->Set_Value(5, pRecord->asDouble(3) - surface); // CHANGE
193 }
194 }
195 }
196
197
198 ///////////////////////////////////////////////////////////
199 // //
200 // //
201 // //
202 ///////////////////////////////////////////////////////////
203
204 //---------------------------------------------------------
205