1
2 ///////////////////////////////////////////////////////////
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Tool Library //
9 // grid_spline //
10 // //
11 //-------------------------------------------------------//
12 // //
13 // MBASpline_for_Categories.cpp //
14 // //
15 // Copyright (C) 2015 by //
16 // Olaf Conrad //
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 Hamburg //
44 // Germany //
45 // //
46 ///////////////////////////////////////////////////////////
47
48 //---------------------------------------------------------
49 #include "MBASpline_for_Categories.h"
50
51
52 ///////////////////////////////////////////////////////////
53 // //
54 // //
55 // //
56 ///////////////////////////////////////////////////////////
57
58 //---------------------------------------------------------
CMBASpline_for_Categories(void)59 CMBASpline_for_Categories::CMBASpline_for_Categories(void)
60 {
61 Set_Name (_TL("Multilevel B-Spline for Categories"));
62
63 Set_Author ("O.Conrad (c) 2015");
64
65 Set_Description (_TW(
66 "The 'Multilevel B-Spline for Categories' tool is comparable to "
67 "indicator Kriging except that uses the Multilevel B-spline "
68 "algorithm for interpolation. "
69 ));
70
71 Add_Reference(
72 "Lee, S., Wolberg, G., Shin, S.Y.", "1997",
73 "Scattered Data Interpolation with Multilevel B-Splines",
74 "IEEE Transactions On Visualisation And Computer Graphics, Vol.3, No.3., p.228-244.",
75 SG_T("https://www.researchgate.net/profile/George_Wolberg/publication/3410822_Scattered_Data_Interpolation_with_Multilevel_B-Splines/links/00b49518719ac9f08a000000/Scattered-Data-Interpolation-with-Multilevel-B-Splines.pdf"),
76 SG_T("ResearchGate")
77 );
78
79 //-----------------------------------------------------
80 Parameters.Add_Shapes("",
81 "POINTS", _TL("Points"),
82 _TL(""),
83 PARAMETER_INPUT
84 );
85
86 Parameters.Add_Table_Field("POINTS",
87 "FIELD" , _TL("Attribute"),
88 _TL("")
89 );
90
91 //-----------------------------------------------------
92 m_Grid_Target.Create(&Parameters, false, "", "TARGET_");
93
94 m_Grid_Target.Add_Grid("CATEGORIES" , _TL("Categories" ), false);
95 m_Grid_Target.Add_Grid("PROPABILITY", _TL("Propability"), false);
96 }
97
98
99 ///////////////////////////////////////////////////////////
100 // //
101 ///////////////////////////////////////////////////////////
102
103 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)104 int CMBASpline_for_Categories::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
105 {
106 if( pParameter->Cmp_Identifier("POINTS") )
107 {
108 m_Grid_Target.Set_User_Defined(pParameters, pParameter->asShapes());
109 }
110
111 m_Grid_Target.On_Parameter_Changed(pParameters, pParameter);
112
113 return( CSG_Tool::On_Parameter_Changed(pParameters, pParameter) );
114 }
115
116 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)117 int CMBASpline_for_Categories::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
118 {
119 m_Grid_Target.On_Parameters_Enable(pParameters, pParameter);
120
121 return( CSG_Tool::On_Parameters_Enable(pParameters, pParameter) );
122 }
123
124
125 ///////////////////////////////////////////////////////////
126 // //
127 ///////////////////////////////////////////////////////////
128
129 //---------------------------------------------------------
On_Execute(void)130 bool CMBASpline_for_Categories::On_Execute(void)
131 {
132 CSG_Shapes Points;
133
134 //-----------------------------------------------------
135 SG_RUN_TOOL_ExitOnError("table_tools", 20, // Add Indicator Fields for Categories
136 SG_TOOL_PARAMETER_SET("TABLE" , Parameters("POINTS")->asShapes())
137 && SG_TOOL_PARAMETER_SET("FIELD" , Parameters("FIELD"))
138 && SG_TOOL_PARAMETER_SET("OUT_SHAPES", &Points) // >> Indicators
139 )
140
141 int nCategories = Points.Get_Field_Count() - 1;
142
143 if( nCategories < 2 )
144 {
145 Error_Set(_TL("found less than two categories, nothing to do"));
146
147 return( false );
148 }
149
150 //-----------------------------------------------------
151 CSG_Grid *pGrid, *pProp, Prop;
152
153 if( !(pGrid = m_Grid_Target.Get_Grid("CATEGORIES", nCategories < 128 ? SG_DATATYPE_Char : SG_DATATYPE_Int))
154 || !(pProp = m_Grid_Target.Get_Grid("PROPABILITY")) )
155 {
156 return( false );
157 }
158
159 //-----------------------------------------------------
160 CSG_Parameter *pLUT = DataObject_Get_Parameter(pGrid, "LUT");
161
162 if( pLUT && pLUT->asTable() )
163 {
164 CSG_Colors Colors(nCategories); Colors.Random();
165
166 CSG_Table *pClasses = pLUT->asTable();
167
168 pClasses->Set_Record_Count(nCategories);
169
170 for(int iClass=0; iClass<nCategories; iClass++)
171 {
172 CSG_Table_Record *pClass = pClasses->Get_Record(iClass);
173
174 pClass->Set_Value(0, Colors[iClass]);
175 pClass->Set_Value(1, Points.Get_Field_Name(1 + iClass));
176 pClass->Set_Value(2, "");
177 pClass->Set_Value(3, iClass);
178 pClass->Set_Value(4, iClass);
179 }
180
181 DataObject_Set_Parameter(pGrid, pLUT); // Lookup Table
182 DataObject_Set_Parameter(pGrid, "COLORS_TYPE", 1); // Color Classification Type: Lookup Table
183 }
184
185 //-----------------------------------------------------
186 pGrid->Fmt_Name("%s" , Points.Get_Field_Name(0));
187 pProp->Fmt_Name("%s [%s]", Points.Get_Field_Name(0), _TL("Propability"));
188
189 pProp->Assign(0.0);
190 pProp->Set_NoData_Value(0.0);
191
192 Prop.Create(pGrid->Get_System());
193
194 //-----------------------------------------------------
195 for(int i=0; i<nCategories; i++)
196 {
197 Process_Set_Text("%s: %s", _TL("processing"), Points.Get_Field_Name(1 + i));
198
199 SG_UI_Progress_Lock(true);
200
201 SG_RUN_TOOL_ExitOnError("grid_spline", 4, // Multilevel B-Spline Interpolation
202 SG_TOOL_PARAMETER_SET("SHAPES" , &Points)
203 && SG_TOOL_PARAMETER_SET("FIELD" , 1 + i) // indicator field
204 && SG_TOOL_PARAMETER_SET("TARGET_DEFINITION", 1) // grid or grid system
205 && SG_TOOL_PARAMETER_SET("TARGET_OUT_GRID" , &Prop) // target grid
206 )
207
208 SG_UI_Progress_Lock(false);
209
210 #pragma omp parallel for
211 for(int y=0; y<pGrid->Get_NY(); y++)
212 {
213 for(int x=0; x<pGrid->Get_NX(); x++)
214 {
215 if( pProp->asDouble(x, y) < Prop.asDouble(x, y) )
216 {
217 pProp->Set_Value(x, y, Prop.asDouble(x, y));
218 pGrid->Set_Value(x, y, i);
219 }
220 }
221 }
222 }
223
224 //-----------------------------------------------------
225 return( true );
226 }
227
228
229 ///////////////////////////////////////////////////////////
230 // //
231 // //
232 // //
233 ///////////////////////////////////////////////////////////
234
235 //---------------------------------------------------------
236