SAGA API Version 9.14
Loading...
Searching...
No Matches
table_selection.cpp
Go to the documentation of this file.
1
3// //
4// SAGA //
5// //
6// System for Automated Geoscientific Analyses //
7// //
8// Application Programming Interface //
9// //
10// Library: SAGA_API //
11// //
12//-------------------------------------------------------//
13// //
14// table_selection.cpp //
15// //
16// Copyright (C) 2005 by Olaf Conrad //
17// //
18//-------------------------------------------------------//
19// //
20// This file is part of 'SAGA - System for Automated //
21// Geoscientific Analyses'. //
22// //
23// This library is free software; you can redistribute //
24// it and/or modify it under the terms of the GNU Lesser //
25// General Public License as published by the Free //
26// Software Foundation, either version 2.1 of the //
27// License, or (at your option) any later version. //
28// //
29// This library is distributed in the hope that it will //
30// be useful, but WITHOUT ANY WARRANTY; without even the //
31// implied warranty of MERCHANTABILITY or FITNESS FOR A //
32// PARTICULAR PURPOSE. See the GNU Lesser General Public //
33// License for more details. //
34// //
35// You should have received a copy of the GNU Lesser //
36// General Public License along with this program; if //
37// not, see <http://www.gnu.org/licenses/>. //
38// //
39//-------------------------------------------------------//
40// //
41// contact: Olaf Conrad //
42// Institute of Geography //
43// University of Goettingen //
44// Goldschmidtstr. 5 //
45// 37077 Goettingen //
46// Germany //
47// //
48// e-mail: oconrad@saga-gis.org //
49// //
51
52//---------------------------------------------------------
53#include "table.h"
54
55#include "tool_library.h"
56
57
59// //
60// //
61// //
63
64//---------------------------------------------------------
66{
67 if( m_Selection.Inc_Array() )
68 {
69 return( _Set_Selection(iRecord, m_Selection.Get_Size() - 1) );
70 }
71
72 return( false );
73}
74
75//---------------------------------------------------------
77{
78 if( Selected < m_Selection.Get_Size() )
79 {
80 *((sLong *)m_Selection.Get_Entry(Selected)) = Index;
81
82 return( true );
83 }
84
85 return( false );
86}
87
88//---------------------------------------------------------
90{
91 for(sLong i=0; i<m_Selection.Get_Size(); i++)
92 {
93 if( Index == Get_Selection_Index(i) )
94 {
95 sLong *Selection = (sLong *)m_Selection.Get_Array();
96
97 for(sLong j=i+1; j<m_Selection.Get_Size(); i++, j++)
98 {
99 Selection[i] = Selection[j];
100 }
101
102 m_Selection.Dec_Array();
103
104 return( true );
105 }
106 }
107
108 return( false );
109}
110
111
113// //
115
116//---------------------------------------------------------
117bool CSG_Table::_Destroy_Selection(void)
118{
119 if( Get_Selection_Count() > 0 )
120 {
121 for(size_t iRecord=0; iRecord<(size_t)m_nRecords; iRecord++)
122 {
123 m_Records[iRecord]->Set_Selected(false);
124 }
125
126 m_Selection.Set_Array(0);
127 }
128
129 return( true );
130}
131
132
134// //
136
137//---------------------------------------------------------
138bool CSG_Table::Select(sLong iRecord, bool bInvert)
139{
140 if( !bInvert )
141 {
142 _Destroy_Selection();
143 }
144
145 CSG_Table_Record *pRecord = Get_Record(iRecord);
146
147 if( pRecord == NULL )
148 {
149 return( false );
150 }
151
152 if( pRecord->is_Selected() == false )
153 {
154 pRecord->Set_Selected(true);
155
156 _Add_Selection(iRecord);
157 }
158 else // if( pRecord->is_Selected() == true )
159 {
160 pRecord->Set_Selected(false);
161
162 _Del_Selection(iRecord);
163 }
164
165 return( true );
166}
167
168//---------------------------------------------------------
169bool CSG_Table::Select(CSG_Table_Record *pRecord, bool bInvert)
170{
171 return( Select(pRecord ? pRecord->Get_Index() : -1, bInvert) );
172}
173
174//---------------------------------------------------------
175/* Selects records for which the expression evaluates to non-zero. The expression syntax is the same as the one for the table calculator.
176 * Method is [0] new selection, [1] add to current selection, [2] select from current selection, or [3] remove from current selection.
177*/
178bool CSG_Table::Select_Numeric(const SG_Char *Expression, int Method, bool bNoData)
179{
180 if( Expression && *Expression )
181 {
182 CSG_Tool *pTool = SG_Get_Tool_Library_Manager().Create_Tool("table_tools", 18); // Select by Numerical Expression
183
184 if( pTool )
185 {
186 pTool->Set_Parameter("TABLE" , this);
187 pTool->Set_Parameter("EXPRESSION", Expression);
188 pTool->Set_Parameter("METHOD" , Method);
189 pTool->Set_Parameter("USE_NODATA", bNoData);
190
191 bool bResult = pTool->Execute();
192
194
195 return( bResult );
196 }
197 }
198
199 return( false );
200}
201
202//---------------------------------------------------------
203/* Searches for a character string expression in the attributes table and selects records where the expression is found.
204 * Method is [0] new selection, [1] add to current selection, [2] select from current selection, or [3] remove from current selection.
205 * Compare is [0] attribute is identical with search expression, [1] attribute contains search expression, [2] attribute is contained in search expression.
206*/
207bool CSG_Table::Select_String(const SG_Char *Expression, int Method, int Compare, bool bCaseSensitive)
208{
209 if( Expression && *Expression )
210 {
211 CSG_Tool *pTool = SG_Get_Tool_Library_Manager().Create_Tool("table_tools", 19); // Select by String Expression
212
213 if( pTool )
214 {
215 pTool->Set_Parameter("TABLE" , this);
216 pTool->Set_Parameter("EXPRESSION", Expression);
217 pTool->Set_Parameter("METHOD" , Method);
218 pTool->Set_Parameter("USE_NODATA", true);
219 pTool->Set_Parameter("COMPARE" , Compare);
220 pTool->Set_Parameter("CASE" , bCaseSensitive);
221
222 bool bResult = pTool->Execute();
223
225
226 return( bResult );
227 }
228 }
229
230 return( false );
231}
232
233
235// //
237
238//---------------------------------------------------------
240{
241 sLong n = 0;
242
243 if( Get_Selection_Count() > 0 )
244 {
245 for(sLong i=m_nRecords-1; i>=0; i--)
246 {
247 if( m_Records[i]->is_Selected() )
248 {
249 m_Records[i]->Set_Selected(false);
250
251 Del_Record(i);
252
253 n++;
254 }
255 }
256
257 m_Selection.Set_Array(0);
258 }
259
260 return( n );
261}
262
263//---------------------------------------------------------
265{
266 if( m_nRecords > 0 )
267 {
268 m_Selection.Set_Array(m_nRecords - m_Selection.Get_Size());
269
270 for(sLong i=0, n=0; i<Get_Count(); i++)
271 {
272 CSG_Table_Record *pRecord = Get_Record(i);
273
274 if( pRecord->is_Selected() == false )//&& n < m_Selection.Get_Size() )
275 {
276 pRecord->Set_Selected(true);
277
278 *((sLong *)m_Selection.Get_Entry(n++)) = i;
279 }
280 else
281 {
282 pRecord->Set_Selected(false);
283 }
284 }
285 }
286
287 return( Get_Selection_Count() );
288}
289
290
292// //
293// //
294// //
296
297//---------------------------------------------------------
signed long long sLong
Definition api_core.h:158
#define SG_Char
Definition api_core.h:536
void Set_Selected(bool bOn=true)
sLong Get_Index(void) const
Definition table.h:136
bool is_Selected(void) const
Definition table.h:254
virtual bool Select_Numeric(const SG_Char *Expression, int Method=0, bool bNoData=false)
sLong Get_Count(void) const
Definition table.h:410
bool _Set_Selection(sLong Index, sLong Selected)
sLong m_nRecords
Definition table.h:513
virtual bool Select(sLong Index, bool bInvert=false)
friend class CSG_Table_Record
Definition table.h:296
CSG_Array m_Selection
Definition table.h:515
virtual sLong Inv_Selection(void)
virtual bool Select_String(const SG_Char *Expression, int Method=0, int Compare=1, bool bCaseSensitive=true)
virtual CSG_Table_Record * Get_Record(sLong Index) const
Definition table.h:412
virtual bool Del_Record(sLong Index)
Definition table.cpp:897
bool _Add_Selection(sLong Index)
virtual bool is_Selected(sLong Index) const
Definition table.h:453
sLong Get_Selection_Index(sLong Index=0) const
Definition table.h:450
sLong Get_Selection_Count(void) const
Definition table.h:449
bool _Del_Selection(sLong Index)
virtual sLong Del_Selection(void)
bool Delete_Tool(CSG_Tool *pTool) const
CSG_Tool * Create_Tool(const CSG_String &Library, int Index, bool bWithGUI=false, bool bWithCMD=true) const
bool Set_Parameter(const CSG_String &ID, CSG_Parameter *pValue)
Definition tool.cpp:1659
bool Execute(bool bAddHistory=false)
Definition tool.cpp:560
CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)