SAGA API v9.10
Loading...
Searching...
No Matches
api_core.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// api_core.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 Hamburg //
44// Germany //
45// //
46// e-mail: oconrad@saga-gis.org //
47// //
49
50//---------------------------------------------------------
51#include <wx/utils.h>
52#include <wx/app.h>
53#include <wx/dir.h>
54#include <wx/stdpaths.h>
55
56#include "api_core.h"
57#include "tool_library.h"
58
59
61// //
62// //
63// //
65
66//---------------------------------------------------------
67#ifdef _OPENMP
68
69#include <omp.h>
70
71//---------------------------------------------------------
72int g_SG_OMP_Max_Num_Threads = omp_get_num_procs();
73
74//---------------------------------------------------------
75void SG_OMP_Set_Max_Num_Threads (int iCores)
76{
77 if( iCores < 1 )
78 {
79 iCores = 1;
80 }
81 else if( iCores > omp_get_num_procs() )
82 {
83 iCores = omp_get_num_procs();
84 }
85
86 omp_set_num_threads(g_SG_OMP_Max_Num_Threads = iCores);
87}
88
89//---------------------------------------------------------
91{
92 return( g_SG_OMP_Max_Num_Threads );
93}
94
95//---------------------------------------------------------
97{
98 return( omp_get_num_procs() );
99}
100
101//---------------------------------------------------------
102int SG_OMP_Get_Thread_Num (void)
103{
104 return( omp_get_thread_num() );
105}
106
107//---------------------------------------------------------
108#else
109void SG_OMP_Set_Max_Num_Threads (int iCores) {}
110int SG_OMP_Get_Max_Num_Threads (void) { return( 1 ); }
111int SG_OMP_Get_Max_Num_Procs (void) { return( 1 ); }
112int SG_OMP_Get_Thread_Num (void) { return( 0 ); }
113#endif
114
115
117// //
118// //
119// //
121
122//---------------------------------------------------------
124{
125 switch( Type )
126 {
127 case SG_DATATYPE_Bit : return( _TL("bit" ) );
128 case SG_DATATYPE_Byte : return( bShort ? _TL("uint8" ) : _TL("unsigned 1 byte integer" ) );
129 case SG_DATATYPE_Char : return( bShort ? _TL("int8" ) : _TL("signed 1 byte integer" ) );
130 case SG_DATATYPE_Word : return( bShort ? _TL("uint16") : _TL("unsigned 2 byte integer" ) );
131 case SG_DATATYPE_Short : return( bShort ? _TL("int16" ) : _TL("signed 2 byte integer" ) );
132 case SG_DATATYPE_DWord : return( bShort ? _TL("uint32") : _TL("unsigned 4 byte integer" ) );
133 case SG_DATATYPE_Int : return( bShort ? _TL("int32" ) : _TL("signed 4 byte integer" ) );
134 case SG_DATATYPE_ULong : return( bShort ? _TL("uint64") : _TL("unsigned 8 byte integer" ) );
135 case SG_DATATYPE_Long : return( bShort ? _TL("int64" ) : _TL("signed 8 byte integer" ) );
136 case SG_DATATYPE_Float : return( bShort ? _TL("float" ) : _TL("4 byte floating point number") );
137 case SG_DATATYPE_Double: return( bShort ? _TL("double") : _TL("8 byte floating point number") );
138 case SG_DATATYPE_String: return( _TL("string") );
139 case SG_DATATYPE_Date : return( _TL("date" ) );
140 case SG_DATATYPE_Color : return( _TL("color" ) );
141 case SG_DATATYPE_Binary: return( _TL("binary") );
142 default : return( bShort ? _TL("none" ) : _TL("undefined" ) );
143 }
144};
145
146//---------------------------------------------------------
151
152//---------------------------------------------------------
154{
155 if( !Identifier.Cmp(gSG_Data_Type_Identifier[SG_DATATYPE_Bit ]) ) return( SG_DATATYPE_Bit );
156 if( !Identifier.Cmp(gSG_Data_Type_Identifier[SG_DATATYPE_Byte ]) ) return( SG_DATATYPE_Byte );
157 if( !Identifier.Cmp(gSG_Data_Type_Identifier[SG_DATATYPE_Char ]) ) return( SG_DATATYPE_Char );
158 if( !Identifier.Cmp(gSG_Data_Type_Identifier[SG_DATATYPE_Word ]) ) return( SG_DATATYPE_Word );
161 if( !Identifier.Cmp(gSG_Data_Type_Identifier[SG_DATATYPE_Int ]) ) return( SG_DATATYPE_Int );
163 if( !Identifier.Cmp(gSG_Data_Type_Identifier[SG_DATATYPE_Long ]) ) return( SG_DATATYPE_Long );
167 if( !Identifier.Cmp(gSG_Data_Type_Identifier[SG_DATATYPE_Date ]) ) return( SG_DATATYPE_Date );
169
170 return( SG_DATATYPE_Undefined );
171}
172
173//---------------------------------------------------------
175{
176 switch( Type )
177 {
178 case SG_DATATYPE_Bit : return( SG_DATATYPES_Bit );
179 case SG_DATATYPE_Byte : return( SG_DATATYPES_Byte );
180 case SG_DATATYPE_Char : return( SG_DATATYPES_Char );
181 case SG_DATATYPE_Word : return( SG_DATATYPES_Word );
182 case SG_DATATYPE_Short : return( SG_DATATYPES_Short );
183 case SG_DATATYPE_DWord : return( SG_DATATYPES_DWord );
184 case SG_DATATYPE_Int : return( SG_DATATYPES_Int );
185 case SG_DATATYPE_ULong : return( SG_DATATYPES_ULong );
186 case SG_DATATYPE_Long : return( SG_DATATYPES_Long );
187 case SG_DATATYPE_Float : return( SG_DATATYPES_Float );
190 case SG_DATATYPE_Date : return( SG_DATATYPES_Date );
191 case SG_DATATYPE_Color : return( SG_DATATYPES_Color );
193 default : return( SG_DATATYPES_Undefined );
194 }
195};
196
197//---------------------------------------------------------
199{
200 switch( Type )
201 {
202 case SG_DATATYPE_Bit :
203 case SG_DATATYPE_Byte :
204 case SG_DATATYPE_Char :
205 case SG_DATATYPE_Word :
206 case SG_DATATYPE_Short :
207 case SG_DATATYPE_DWord :
208 case SG_DATATYPE_Int :
209 case SG_DATATYPE_ULong :
210 case SG_DATATYPE_Long :
211 case SG_DATATYPE_Float :
213 return( true );
214
216 case SG_DATATYPE_Date :
217 case SG_DATATYPE_Color :
218 case SG_DATATYPE_Binary: default:
219 return( false );
220 }
221}
222
223//---------------------------------------------------------
225{
226 double min, max;
227
228 switch( Type )
229 {
230 default:
231 case SG_DATATYPE_Double: Value = (double)Value; return( true );
232 case SG_DATATYPE_Float : Value = (float )Value; return( true );
233
234 case SG_DATATYPE_Bit : min = 0.; max = 1.; break;
235 case SG_DATATYPE_Byte : min = 0.; max = 255.; break;
236 case SG_DATATYPE_Char : min = -128.; max = 127.; break;
237 case SG_DATATYPE_Word : min = 0.; max = 65535.; break;
238 case SG_DATATYPE_Short : min = -32768.; max = 32767.; break;
239 case SG_DATATYPE_DWord : min = 0.; max = 4294967295.; break;
240 case SG_DATATYPE_Int : min = -2147483648.; max = 2147483647.; break;
241 }
242
243 if( Value < min )
244 {
245 Value = min;
246 }
247 else if( Value > max )
248 {
249 Value = max;
250 }
251
252 return( true );
253}
254
255
257// //
258// Environment //
259// //
261
262//---------------------------------------------------------
273class CSG_App_Initialize
274{
275public:
276 CSG_App_Initialize(void)
277 {
278 m_Initialized = 0;
279 }
280
281 virtual ~CSG_App_Initialize(void)
282 {}
283
284 bool Initialize (void)
285 {
286 if( wxInitialize() )
287 {
288 m_Initialized++;
289
290 return( true );
291 }
292
293 return( false );
294 }
295
296 bool Uninitialize (void)
297 {
298 if( m_Initialized > 0 )
299 {
300 wxUninitialize();
301
302 m_Initialized--;
303
304 return( true );
305 }
306
307 return( false );
308 }
309
310 bool Uninitialize (bool bAll)
311 {
312 if( bAll )
313 {
314 while( m_Initialized > 0 )
315 {
316 Uninitialize();
317 }
318 }
319 else
320 {
321 Uninitialize();
322 }
323
324 return( true );
325 }
326
327
328private:
329
330 int m_Initialized;
331
332};
333
334//---------------------------------------------------------
335CSG_App_Initialize g_App_Initialize;
336
337
339// //
341
342//---------------------------------------------------------
344
346{
347 if( g_SAGA_API_Path.is_Empty() && SG_UI_Get_Application_Name().Find("saga") == 0 )
348 {
350 }
351
352 return( g_SAGA_API_Path );
353}
354
355//---------------------------------------------------------
357{
358 static CSG_String App_Path;
359
360 if( App_Path.is_Empty() )
361 {
362 App_Path = wxStandardPaths::Get().GetExecutablePath().wc_str();
363 }
364
365 CSG_String Path(App_Path);
366
367 if( bPathOnly )
368 {
369 Path = SG_File_Get_Path(App_Path);
370 }
371
372 return( SG_File_Get_Path_Absolute(Path) );
373}
374
375//---------------------------------------------------------
377
379{
380 if( g_App_Name.is_Empty() )
381 {
382 g_App_Name = wxApp::GetInstance()->GetAppName().wc_str();
383 }
384
385 return( g_App_Name );
386}
387
389{
390 CSG_String oldName(g_App_Name);
391
392 g_App_Name = Name;
393
394 return( oldName );
395}
396
397
399// //
401
402//---------------------------------------------------------
403bool SG_Add_Dll_Paths(const wxString &Directory, wxString &Paths)
404{
405 wxDir Dir(Directory);
406
407 if( Dir.IsOpened() )
408 {
409 wxString Path;
410
411 if( Dir.GetFirst(&Path, "*.dll", wxDIR_HIDDEN|wxDIR_FILES) )
412 {
413 if( !Path.IsEmpty() )
414 {
415 Paths += ";";
416 }
417
418 Paths += Directory;
419 }
420
421 if( Dir.GetFirst(&Path, "gdal_netCDF.dll", wxDIR_HIDDEN|wxDIR_FILES) )
422 {
423 wxSetEnv("GDAL_DRIVER_PATH", Directory);
424 }
425
426 if( Dir.GetFirst(&Path, "pdalcpp.dll", wxDIR_HIDDEN | wxDIR_FILES) )
427 {
428 wxSetEnv("PDAL_DRIVER_PATH", Directory);
429 }
430
431 if( Dir.GetFirst(&Path, wxEmptyString, wxDIR_HIDDEN|wxDIR_DIRS) )
432 {
433 do
434 {
435 wxString SubDir(Directory + "\\" + Path);
436
437 if( !Path.CmpNoCase("gdal-plugins") ) { wxSetEnv("GDAL_DRIVER_PATH", SubDir); } else
438 if( !Path.CmpNoCase("gdal-data" ) ) { wxSetEnv("GDAL_DATA" , SubDir); } else
439 if( !Path.CmpNoCase("proj-data" ) ) { wxSetEnv("PROJ_LIB" , SubDir); } else
440 {
441 SG_Add_Dll_Paths(SubDir, Paths);
442 }
443 }
444 while( Dir.GetNext(&Path) );
445 }
446 }
447
448 return( !Paths.IsEmpty() );
449}
450
451//---------------------------------------------------------
452bool SG_Initialize_Environment(bool bLibraries, bool bProjections, const SG_Char *SAGA_Path, bool bInitializeWX)
453{
454 #ifdef _DEBUG
455 SG_UI_Console_Print_StdOut("\n!!! Loading SAGA API => DEBUG !!!");
456 #endif
457
458 if( bInitializeWX )
459 {
460 g_App_Initialize.Initialize();
461 }
462
463 //-----------------------------------------------------
464 static bool bInitialized = false;
465
466 if( bInitialized == false )
467 {
468 bInitialized = true;
469
471
472 #if defined(_SAGA_MSW)
473 {
474 wxString App_Path, Dll_Paths, System_Paths;
475
476 if( SAGA_Path && SG_Dir_Exists(SAGA_Path) )
477 {
478 App_Path = SAGA_Path;
479 }
480 else
481 {
482 App_Path = SG_UI_Get_Application_Path(true).c_str();
483 }
484
485 g_SAGA_API_Path = &App_Path;
486
487 wxGetEnv("PATH", &System_Paths);
488
489 if( SG_Add_Dll_Paths(App_Path + "\\dll", Dll_Paths) )
490 {
491 Dll_Paths += ';' + App_Path + ';' + System_Paths;
492
493 wxSetEnv("PATH", Dll_Paths);
494 }
495 }
496 #elif defined(__WXMAC__)
497 {
499
500 if( SG_Dir_Exists(App_Path + "/proj-data") ) { wxSetEnv("PROJ_LIB" , wxString::Format("%s/proj-data", App_Path.c_str())); }
501 if( SG_Dir_Exists(App_Path + "/gdal-data") ) { wxSetEnv("GDAL_DATA", wxString::Format("%s/gdal-data", App_Path.c_str())); }
502 }
503 #else // #if defined(_SAGA_LINUX)
504 {
505 }
506 #endif
507
508 SG_Get_Projections().Create(bProjections);
509
511 }
512
513 //-----------------------------------------------------
514 if( bLibraries )
515 {
517
519
521 }
522
523 //-----------------------------------------------------
524 return( true );
525}
526
527//---------------------------------------------------------
529{
530 g_App_Initialize.Uninitialize(true);
531
532 return( true );
533}
534
535
537// //
538// //
539// //
541
542//---------------------------------------------------------
void SG_UI_Console_Print_StdOut(const char *Text, SG_Char End, bool bFlush)
void SG_UI_ProgressAndMsg_Lock(bool bOn)
bool SG_Uninitialize_Environment(void)
Definition api_core.cpp:528
CSG_String SG_UI_Set_Application_Name(const CSG_String &Name)
Definition api_core.cpp:388
TSG_Data_Type SG_Data_Type_Get_Type(const CSG_String &Identifier)
Definition api_core.cpp:153
CSG_String g_SAGA_API_Path
Definition api_core.cpp:343
int SG_Data_Type_Get_Flag(TSG_Data_Type Type)
Definition api_core.cpp:174
CSG_String SG_UI_Get_Application_Name(void)
Definition api_core.cpp:378
void SG_OMP_Set_Max_Num_Threads(int iCores)
Definition api_core.cpp:109
int SG_OMP_Get_Max_Num_Procs(void)
Definition api_core.cpp:111
bool SG_Add_Dll_Paths(const wxString &Directory, wxString &Paths)
Definition api_core.cpp:403
CSG_String SG_UI_Get_Application_Path(bool bPathOnly)
Definition api_core.cpp:356
CSG_String SG_UI_Get_API_Path(void)
Definition api_core.cpp:345
int SG_OMP_Get_Thread_Num(void)
Definition api_core.cpp:112
CSG_String SG_Data_Type_Get_Name(TSG_Data_Type Type, bool bShort)
Definition api_core.cpp:123
CSG_App_Initialize g_App_Initialize
Definition api_core.cpp:335
bool SG_Data_Type_is_Numeric(TSG_Data_Type Type)
Definition api_core.cpp:198
bool SG_Initialize_Environment(bool bLibraries, bool bProjections, const SG_Char *SAGA_Path, bool bInitializeWX)
Definition api_core.cpp:452
bool SG_Data_Type_Range_Check(TSG_Data_Type Type, double &Value)
Definition api_core.cpp:224
int SG_OMP_Get_Max_Num_Threads(void)
Definition api_core.cpp:110
CSG_String SG_Data_Type_Get_Identifier(TSG_Data_Type Type)
Definition api_core.cpp:147
CSG_String g_App_Name
Definition api_core.cpp:376
#define SG_DATATYPES_DWord
Definition api_core.h:1024
#define SG_DATATYPES_Byte
Definition api_core.h:1020
#define SG_DATATYPES_Word
Definition api_core.h:1022
#define SG_DATATYPES_Bit
Definition api_core.h:1019
#define SG_DATATYPES_Short
Definition api_core.h:1023
#define SG_DATATYPES_String
Definition api_core.h:1030
#define SG_DATATYPES_Double
Definition api_core.h:1029
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path_Absolute(const CSG_String &full_Path)
#define SG_DATATYPES_Date
Definition api_core.h:1031
#define SG_DATATYPES_Binary
Definition api_core.h:1033
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path(const CSG_String &full_Path)
#define SG_DATATYPES_Int
Definition api_core.h:1025
#define SG_DATATYPES_Char
Definition api_core.h:1021
const char gSG_Data_Type_Identifier[][32]
Definition api_core.h:1043
SAGA_API_DLL_EXPORT bool SG_Dir_Exists(const CSG_String &Directory)
Definition api_file.cpp:966
#define SG_DATATYPES_Undefined
Definition api_core.h:1018
#define SG_DATATYPES_Long
Definition api_core.h:1027
#define SG_DATATYPES_ULong
Definition api_core.h:1026
TSG_Data_Type
Definition api_core.h:997
@ SG_DATATYPE_Long
Definition api_core.h:1006
@ SG_DATATYPE_Byte
Definition api_core.h:999
@ SG_DATATYPE_Bit
Definition api_core.h:998
@ SG_DATATYPE_Short
Definition api_core.h:1002
@ SG_DATATYPE_Word
Definition api_core.h:1001
@ SG_DATATYPE_ULong
Definition api_core.h:1005
@ SG_DATATYPE_Float
Definition api_core.h:1007
@ SG_DATATYPE_Undefined
Definition api_core.h:1013
@ SG_DATATYPE_Binary
Definition api_core.h:1012
@ SG_DATATYPE_Double
Definition api_core.h:1008
@ SG_DATATYPE_Color
Definition api_core.h:1011
@ SG_DATATYPE_Int
Definition api_core.h:1004
@ SG_DATATYPE_Char
Definition api_core.h:1000
@ SG_DATATYPE_String
Definition api_core.h:1009
@ SG_DATATYPE_DWord
Definition api_core.h:1003
@ SG_DATATYPE_Date
Definition api_core.h:1010
#define SG_Char
Definition api_core.h:536
#define _TL(s)
Definition api_core.h:1568
#define SG_DATATYPES_Color
Definition api_core.h:1032
#define SG_DATATYPES_Float
Definition api_core.h:1028
bool Create(bool LoadCodeList=true)
int Cmp(const CSG_String &String) const
const SG_Char * c_str(void) const
bool is_Empty(void) const
bool Add_Default_Libraries(bool bVerbose=false)
SAGA_API_DLL_EXPORT CSG_Projections & SG_Get_Projections(void)
CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)