 |
SAGA API
v9.2
|
Go to the documentation of this file.
56 #include <wx/string.h>
71 m_bError_Ignore =
false;
88 if( m_Settings_Stack.
Get_Size() > 0 )
90 for(
size_t i=0; i<m_Settings_Stack.
Get_uSize(); i++)
98 for(
int i=0; i<m_npParameters; i++)
100 delete(m_pParameters[i]);
117 m_bError_Ignore =
false;
136 return( m_File_Name );
209 if( Menu.
Length() > 1 && Menu[1] ==
':' )
211 if( Menu[0] ==
'A' || Menu[0] ==
'a' )
226 return( m_Library_Menu );
229 return( m_Library_Menu +
"|" + Menu );
248 #define ADD_MESSAGE_EXECUTION(Text, Style) { SG_UI_Msg_Add(Text, true, Style); if( has_GUI() ) { SG_UI_Msg_Add_Execution(Text, true, Style); } }
258 m_bError_Ignore =
false;
260 bool bResult =
false;
262 m_Execution_Info.
Clear();
274 if(
Parameters.DataObjects_Create() ==
false )
276 _Synchronize_DataObjects();
287 #define _TOOL_EXCEPTION
296 #ifdef _TOOL_EXCEPTION
298 catch(
const std::exception &Exception)
312 _Synchronize_DataObjects();
321 if( bResult && bAddHistory )
323 _Set_Output_History();
353 _TL(
"execution time"), Time.
c_str()),
365 bResult ?
_TL(
"Execution succeeded") :
_TL(
"Execution failed"), Time.
c_str()),
391 bool CSG_Tool::_Synchronize_DataObjects(
void)
395 for(
int i=0; i<m_npParameters; i++)
397 m_pParameters[i]->DataObjects_Synchronize();
404 Parameters.DataObjects_Set_Projection(Projection);
406 for(
int j=0; j<m_npParameters; j++)
408 m_pParameters[j]->DataObjects_Set_Projection(Projection);
422 Parameters.DataObjects_Get_Projection(Projection);
424 for(
int i=0; i<m_npParameters && !Projection.
is_Okay(); i++)
426 m_pParameters[i]->DataObjects_Get_Projection(Projection);
429 return( Projection.
is_Okay() );
440 int CSG_Tool::_On_Parameter_Changed(
CSG_Parameter *pParameter,
int Flags)
488 pParameters = m_pParameters[m_npParameters++] =
new CSG_Parameters();
490 pParameters->
Create(
this, Name, Description, Identifier);
492 pParameters->m_pTool =
this;
494 return( pParameters );
500 for(
int i=0; i<m_npParameters; i++)
502 if( m_pParameters[i]->Cmp_Identifier(Identifier) )
504 return( m_pParameters[i] );
542 for(
int i=0; i<m_npParameters; i++)
553 for(
int i=0; i<m_npParameters; i++)
607 size_t n = m_Settings_Stack.
Get_Size();
615 for(
int i=0; i<m_npParameters; i++)
631 if( pP && (
int)m_Settings_Stack.
Get_Size() >= 1 + m_npParameters )
633 size_t n = m_Settings_Stack.
Get_Size() - 1;
635 for(
int i=m_npParameters-1; i>=0; i--, n--)
660 m_bShow_Progress = bOn;
666 return(
Set_Progress((
double)Position / (
double)Range) );
672 return(
Set_Progress((
double)Position / (
double)Range) );
726 m_Execution_Info +=
"\n____\n" + Text;
737 m_bError_Ignore =
true;
755 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
756 va_start(argptr, _Format);
757 _s.PrintfV(_Format, argptr);
759 va_start(argptr, Format);
760 _s.PrintfV(Format, argptr);
780 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
781 va_start(argptr, _Format);
782 _s.PrintfV(_Format, argptr);
784 va_start(argptr, Format);
785 _s.PrintfV(Format, argptr);
829 else if(
Parameters(i)->is_DataObject_List() )
831 for(
int j=0; j<
Parameters(i)->asList()->Get_Item_Count(); j++)
869 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
870 va_start(argptr, _Format);
871 _s.PrintfV(_Format, argptr);
873 va_start(argptr, Format);
874 _s.PrintfV(Format, argptr);
894 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
895 va_start(argptr, _Format);
896 _s.PrintfV(_Format, argptr);
898 va_start(argptr, Format);
899 _s.PrintfV(Format, argptr);
916 m_Execution_Info +=
"\n";
919 m_Execution_Info += Text;
932 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
933 va_start(argptr, _Format);
934 _s.PrintfV(_Format, argptr);
936 va_start(argptr, Format);
937 _s.PrintfV(Format, argptr);
957 wxString _Format(Format); _Format.Replace(
"%s",
"%ls");
958 va_start(argptr, _Format);
959 _s.PrintfV(_Format, argptr);
961 va_start(argptr, Format);
962 _s.PrintfV(Format, argptr);
1031 if( pDataObject == pCopy )
1224 for(
int i=0; i<m_npParameters; i++)
1249 for(
int i=0; i<m_npParameters; i++)
1298 void CSG_Tool::_Update_Parameter_States(
CSG_Parameters *pParameters)
1302 for(
int i=0; i<pParameters->
Get_Count(); i++)
1358 Script +=
"@ECHO OFF\n\n";
1359 Script +=
"PUSHD %~dp0\n\n";
1360 Script +=
"REM SET SAGA_TLB=C:\\MyTools\n\n";
1362 Script +=
"REM Tool: " +
Get_Name() +
"\n\n";
1363 Script +=
"%SAGA_CMD%";
1367 Script +=
"#!/bin/bash\n\n";
1368 Script +=
"# export SAGA_TLB=/home/myhome/mytools\n\n";
1369 Script +=
"# tool: " +
Get_Name() +
"\n\n";
1370 Script +=
"saga_cmd";
1376 Script +=
"saga_cmd";
1385 ?
" \"" +
Get_ID () +
"\""
1392 _Get_Script_CMD(Script,
Get_Parameters(i), bAllParameters, Type);
1398 Script +=
"\n\nPAUSE\n";
1407 #define GET_ID1(p) (p->Get_Parameters()->Get_Identifier().Length() > 0 \
1408 ? CSG_String::Format("%s_%s", p->Get_Parameters()->Get_Identifier().c_str(), p->Get_Identifier()) \
1409 : CSG_String::Format(p->Get_Identifier())).c_str()
1411 #define GET_ID2(p, s) CSG_String::Format("%s_%s", GET_ID1(p), s).c_str()
1418 default : Prefix =
" \\\n -";
break;
1422 for(
int iParameter=0; iParameter<pParameters->
Get_Count(); iParameter++)
1518 default : ext =
"dat" ;
break;
1537 Script +=
"file(s)";
1544 for(
int iObject=1; iObject<p->
asList()->Get_Item_Count(); iObject++)
1567 CSG_String CSG_Tool::_Get_Script_Python(
bool bHeader,
bool bAllParameters)
1571 Name.Replace(
" ",
"_");
1572 Name.Replace(
"(",
"");
1573 Name.Replace(
")",
"");
1574 Name.Replace(
"[",
"");
1575 Name.Replace(
"]",
"");
1576 Name.Replace(
".",
"");
1577 Name.Replace(
",",
"");
1578 Name.Replace(
"/",
"");
1579 Name.Replace(
"-",
"");
1584 Script +=
"#! /usr/bin/env python\n";
1586 Script +=
"#_________________________________________\n";
1587 Script +=
"##########################################\n";
1591 Script +=
"# Initialize the environment...\n";
1593 Script +=
"# Windows: Let the 'SAGA_PATH' environment variable point to\n";
1594 Script +=
"# the SAGA installation folder before importing 'saga_api'!\n";
1595 Script +=
"# This can be defined globally in the Windows system or\n";
1596 Script +=
"# user environment variable settings, in the 'PySAGA/__init__.py'\n";
1597 Script +=
"# file, or in the individual Python script itself. To do the latter\n";
1598 Script +=
"# just uncomment the following line and adjust the path accordingly:\n";
1599 Script +=
"###import os; os.environ['SAGA_PATH'] = '" + AppPath +
"'\n";
1601 Script +=
"# Windows: The most convenient way to make PySAGA available to all your\n";
1602 Script +=
"# Python scripts is to copy the PySAGA folder to the 'Lib/site-packages/'\n";
1603 Script +=
"# folder of your Python installation. If don't want to do this or if you\n";
1604 Script +=
"# don't have the rights to do so, you can also copy it to the folder with\n";
1605 Script +=
"# the Python scripts in which you want to use PySAGA, or alternatively\n";
1606 Script +=
"# you can add the path containing the PySAGA folder (e.g. the path to your\n";
1607 Script +=
"# SAGA installation) to the PYTHONPATH environment variable. To do this\n";
1608 Script +=
"# from within your script you can also take the following command (just\n";
1609 Script +=
"# uncomment the following line and adjust the path accordingly):\n";
1610 Script +=
"###import sys; sys.path.insert(1, '" + AppPath +
"')\n";
1613 Script +=
"# Import saga_api from PySAGA:\n";
1614 Script +=
"from PySAGA import saga_api\n";
1617 Script +=
"#_________________________________________\n";
1618 Script +=
"##########################################\n";
1619 Script +=
"def Run_" + Name +
"(Results):\n";
1623 if( bHeader ) Script +=
" # Get the tool:\n";
1624 Script +=
" Tool = saga_api.SG_Get_Tool_Library_Manager().Get_Tool('" +
Get_Library() +
"', '" +
Get_ID() +
"')\n";
1625 Script +=
" if not Tool:\n";
1626 Script +=
" saga_api.SG_UI_Msg_Add_Error('Failed to request tool: " +
Get_Name() +
"')\n";
1627 Script +=
" return False\n";
1629 if( bHeader ) Script +=
" # Set the parameter interface:\n";
1630 Script +=
" Tool.Reset()\n";
1642 if( bHeader ) Script +=
" # Execute the tool:\n";
1643 Script +=
" if not Tool.Execute():\n";
1644 Script +=
" saga_api.SG_UI_Msg_Add_Error('failed to execute tool: ' + Tool.Get_Name().c_str())\n";
1645 Script +=
" return False\n";
1647 if( bHeader ) Script +=
" # Request the results:\n";
1665 default : type =
"" ; ext =
"" ;
break;
1670 Script +=
" Data = Tool.Get_Parameter('" +
id +
"').as" + type +
"()\n";
1674 Script +=
" Data.Save('{:s}/{:s}.{:s}'.format(Results, Data.Get_Name(), '" + ext +
"'))\n\n";
1679 Script +=
" List = Tool.Get_Parameter('" +
id +
"').as" + type +
"List()\n";
1680 Script +=
" for i in range(0, List.Get_Item_Count()):\n";
1684 Script +=
" List.Get_Item(i).Save('{:s}/{:s}_{:d}.{:s}'.format(Results, List.Get_Name(), i, '" + ext +
"'))\n\n";
1688 Script +=
" Data = List.Get_Item(i)\n";
1697 Script +=
" # job is done, free memory resources:\n";
1698 Script +=
" saga_api.SG_Get_Data_Manager().Delete_All()\n";
1700 Script +=
" return True\n";
1703 Script +=
"#_________________________________________\n";
1704 Script +=
"##########################################\n";
1705 Script +=
"print('This is a simple template for using a SAGA tool through Python.')\n";
1706 Script +=
"print('Please edit the script to make it work properly before using it!')\n";
1708 Script +=
"# Run_" + Name +
"('.')\n";
1717 for(
int iParameter=0; iParameter<pParameters->
Get_Count(); iParameter++)
1730 ID.Prepend(Prefix +
".");
1785 Script +=
CSG_String::Format(
" Tool.Set_Parameter('%s', saga_api.SG_Create_Table('table.txt'))\n", ID.c_str());
1791 Script +=
CSG_String::Format(
" Tool.Set_Parameter('%s', saga_api.CSG_Grid_System(%g, %g, %g, %d, %d))\n", ID.c_str(),
1807 Script +=
CSG_String::Format(
" Tool.Set_Parameter('%s', saga_api.SG_Get_Data_Manager().Add('%s input file%s'))\n", ID.c_str(),
1813 Script +=
CSG_String::Format(
" Tool.Set_Parameter('%s', saga_api.SG_Get_Create_Pointer()) # optional output, remove this line, if you don't want to create it\n", ID.c_str());
1825 Script +=
CSG_String::Format(
" Tool.Get_Parameter('%s').asList().Add_Item('%s input list%s')\n", ID.c_str(),
1832 _Get_Script_Python(Script, p->
asParameters(), bAllParameters, ID);
1844 CSG_String CSG_Tool::_Get_Script_Python_Wrap(
bool bHeader,
bool bName,
bool bCall)
1865 _Get_Script_Python_Wrap(
Parameters[i], 0 , Arguments, Description, Code, bCall && bHeader);
1903 Script +=
"#! /usr/bin/env python\n";
1904 Script +=
"from PySAGA import saga_api; from PySAGA.tools import " +
Get_Library() +
"\n\n";
1905 Script += Code +
"\n";
1909 Script +=
"#! /usr/bin/env python\n";
1910 Script +=
"from PySAGA.tools import " +
Get_Library() +
"\n\n";
1913 Script +=
Get_Library() +
'.' + Name +
"(" + Arguments +
", Verbose=2)\n";
1919 Script +=
"#! /usr/bin/env python\n";
1920 Script +=
"from PySAGA.helper import Tool_Wrapper\n\n";
1923 Script +=
"def " + Name +
"(" + Arguments +
", Verbose=2):\n";
1926 Script +=
" ----------\n";
1928 for(
int i=0; i<_Description.
Get_Count(); i++)
1930 _Description[i].Trim_Both(); Script +=
" " + _Description[i] +
"\\n\n";
1932 Script +=
" Arguments\n";
1933 Script +=
" ----------\n";
1934 Script += Description +
"\n";
1935 Script +=
" - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\\n\n";
1936 Script +=
" Returns\n";
1937 Script +=
" ----------\n";
1938 Script +=
" `boolean` : `True` on success, `False` on failure.\n";
1940 Script +=
" Tool = Tool_Wrapper('" +
Get_Library() +
"', '" +
Get_ID() +
"', '" + Expected +
"')\n";
1941 Script +=
" if Tool.is_Okay():\n";
1943 Script +=
" return Tool.Execute(Verbose)\n";
1944 Script +=
" return False\n\n";
1971 ID.Prepend(Prefix +
".");
1976 if( Argument[0] >=
'0' && Argument[0] <=
'9' )
1978 Argument.Prepend(
'_');
1981 Argument.Replace(
".",
"_");
1982 Argument.Replace(
"|",
"_");
1983 Argument.Replace(
" ",
"_");
1985 if( Argument.Length() > 2 )
1987 Argument.Make_Upper();
1993 bool bResult =
false;
1995 for(
int i=0; i<(*Parameter.
asParameters()).Get_Count(); i++)
1997 if( _Get_Script_Python_Wrap((*Parameter.
asParameters())[i], Constraint, Arguments, Description, Code, bCall, ID) )
2010 if( Parameter.
is_Option() && Constraint != 0 ) {
return(
false ); }
2030 ); File.Replace(
"\\",
"/");
2091 default :
return(
false );
2102 Arguments += Argument +
"=" + Value;
2114 Arguments += Argument +
"=None";
2119 if( Parameter.
is_Input () ) { Code +=
"Tool.Set_Input "; }
2120 if( Parameter.
is_Output() ) { Code +=
"Tool.Set_Output"; }
2121 if( Parameter.
is_Option() ) { Code +=
"Tool.Set_Option"; }
2126 Description +=
" - " + Argument +
" [`";
2130 Description += Parameter.
is_Optional() ?
"optional input " :
"input ";
2134 Description +=
"output ";
2143 Description +=
". " + s;
2146 Description +=
"\n";
2190 void CSG_Tool::_Set_Output_History(
void)
2199 for(
int i=0; i<pParameters->
Get_Count(); i++)
2224 History = _Get_Output_History();
2226 pHistory = &History;
2258 for(
int j=0; j<pParameter->
asList()->Get_Item_Count(); j++)
bool Set_Array(sLong nValues, bool bShrink=true)
const CSG_String & Get_Description(void) const
CSG_Parameter * Get_Parameter(int i) const
@ PARAMETER_TYPE_FilePath
CSG_Grid_System * Get_Grid_System(void) const
int Printf(const char *Format,...)
double Get_Cellsize(void) const
CSG_String & Make_Lower(void)
sLong Get_Size(void) const
bool SG_UI_DataObject_Params_Set(CSG_Data_Object *pDataObject, CSG_Parameters *pParameters)
@ PARAMETER_TYPE_Table_Fields
CSG_MetaData & Get_History(void)
size_t Length(void) const
bool SG_UI_Dlg_Continue(const CSG_String &Message, const CSG_String &Caption)
static CSG_DateTime Now(void)
double Get_Max(void) const
int Get_Hours(void) const
bool Set_Grid_System(const CSG_Grid_System &System)
bool Reset_Grid_System(void)
const SG_Char * Get_Identifier(void) const
class CSG_Data_Manager * Get_Manager(void) const
CSG_String Get_Type_Name(void) const
const SG_Char * asString(void) const
double Get_XMin(bool bCells=false) const
@ SG_UI_MSG_STYLE_SUCCESS
@ PARAMETER_TYPE_Grids_List
CSG_String SG_Get_DataObject_Name(TSG_Data_Object_Type Type)
CSG_Data_Object * Get_Item(int Index) const
void SG_UI_Msg_Add_Execution(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
bool SG_UI_Process_Get_Okay(bool bBlink)
bool SG_UI_Process_Set_Okay(bool bOkay)
bool is_Input(void) const
bool is_Managed(void) const
bool SG_UI_Stop_Execution(bool bDialog)
sLong Get_Milliseconds(void) const
const SG_Char * Get_Name(void) const
const SG_Char * Get_Description(void) const
CSG_Data_Manager & SG_Get_Data_Manager(void)
@ SG_DATAOBJECT_TYPE_Grids
bool do_UseInCMD(void) const
@ PARAMETER_TYPE_FixedTable
CSG_String SG_UI_Get_Application_Path(bool bPathOnly)
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
CSG_Data_Object * asDataObject(void) const
@ PARAMETER_TYPE_PointCloud
size_t Replace(const CSG_String &sOld, const CSG_String &sNew, bool bReplaceAll=true)
CSG_Parameters * Get_Parameters(void) const
#define PARAMETER_INPUT_OPTIONAL
bool Msg_String(bool bOptionsOnly)
@ PARAMETER_TYPE_Shapes_List
virtual TSG_Parameter_Type Get_Type(void) const =0
@ SG_UI_MSG_STYLE_FAILURE
bool is_DataObject_List(void) const
@ PARAMETER_TYPE_Data_Type
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
void Set_Name(const CSG_String &Name)
void SG_UI_Dlg_Message(const CSG_String &Message, const CSG_String &Caption)
bool Assign_Values(CSG_Parameters *pSource)
CSG_String Format(const CSG_String &format="") const
bool Delete_All(bool bDetach=false)
int Get_Count(void) const
bool Restore_Defaults(bool bClearData=false)
@ PARAMETER_TYPE_Table_Field
bool is_Enabled(bool bCheckEnv=true) const
class CSG_Parameters * asParameters(void) const
bool SG_UI_Dlg_Parameters(CSG_Parameters *pParameters, const CSG_String &Caption)
class CSG_Tool * Get_Tool(void) const
size_t Get_Value_Size(void) const
bool SG_UI_DataObject_Update(CSG_Data_Object *pDataObject, int Show, CSG_Parameters *pParameters)
void Add_Reference(const CSG_String &Authors, const CSG_String &Year, const CSG_String &Title, const CSG_String &Where, const SG_Char *Link=NULL, const SG_Char *Link_Text=NULL)
bool SG_UI_DataObject_Add(CSG_Data_Object *pDataObject, int Show)
bool SG_UI_DataObject_Colors_Set(CSG_Data_Object *pDataObject, CSG_Colors *pColors)
void SG_UI_Process_Set_Text(const CSG_String &Text)
SAGA_API_DLL_EXPORT CSG_Strings SG_String_Tokenize(const CSG_String &String, const CSG_String &Delimiters=SG_DEFAULT_DELIMITERS, TSG_String_Tokenizer_Mode Mode=SG_TOKEN_DEFAULT)
const SG_Char * Get_File_Name(bool bNative=true) const
CSG_String SG_Color_To_Text(long Color, bool bHexadecimal)
const CSG_Strings & Get_References(void) const
@ PARAMETER_TYPE_Grid_List
size_t Get_uSize(void) const
void * Create(const CSG_Array &Array)
bool is_Optional(void) const
bool Create(const CSG_Parameters &Parameters)
void * Get_Array(void) const
@ SG_DATAOBJECT_TYPE_Grid
bool Del_Parameter(int i)
bool is_Output(void) const
static CSG_String Format(const char *Format,...)
int Get_Minutes(void) const
int Get_Item_Count(void) const
bool SG_UI_DataObject_Colors_Get(CSG_Data_Object *pDataObject, CSG_Colors *pColors)
int SG_UI_Dlg_Error(const CSG_String &Message, const CSG_String &Caption)
@ SG_DATAOBJECT_TYPE_Shapes
CSG_String AfterFirst(char Character) const
int Get_Children_Count(void) const
bool is_DataObject(void) const
bool SG_UI_DataObject_Params_Get(CSG_Data_Object *pDataObject, CSG_Parameters *pParameters)
sLong Get_Seconds(void) const
void Set_Description(const CSG_String &Description)
bool Contains(const CSG_String &String) const
bool Add(CSG_Data_Object *pObject)
@ SG_DATAOBJECT_TYPE_Table
int SG_Get_History_Depth(void)
bool is_Empty(void) const
bool SG_UI_Process_Set_Progress(int Position, int Range)
#define PARAMETER_DESCRIPTION_TEXT
double asDouble(void) const
@ PARAMETER_TYPE_Grid_System
@ PARAMETER_TYPE_PointCloud_List
@ PARAMETER_TYPE_Table_List
class CSG_Parameter_Range * asRange(void) const
void * SG_UI_Get_Window_Main(void)
void Set_Manager(class CSG_Data_Manager *pManager)
@ PARAMETER_TYPE_DataObject_Output
bool Set_Parameter(const CSG_String &ID, CSG_Parameter *pValue)
#define PARAMETER_DESCRIPTION_PROPERTIES
#define PARAMETER_CHECK_VALUES
const CSG_String & Get_Name(void) const
const SG_Char * c_str(void) const
TSG_PFNC_Parameter_Changed Set_Callback_On_Parameter_Changed(TSG_PFNC_Parameter_Changed pCallback)
bool SG_UI_Process_Set_Ready(void)
int Get_Count(void) const
#define PARAMETER_CHECK_ENABLE
TSG_Data_Object_Type Get_DataObject_Type(void) const
double Get_Min(void) const
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
void SG_UI_Msg_Add_Error(const char *Message)
bool Cmp_Identifier(const CSG_String &Identifier) const
CSG_Grid_System * asGrid_System(void) const
const SG_Char * Get_Name(void) const
virtual bool is_Default(void) const
bool is_Option(void) const
@ PARAMETER_TYPE_TIN_List
double Get_YMin(bool bCells=false) const
@ PARAMETER_TYPE_Parameters
class CSG_Parameter_List * asList(void) const
CSG_String SG_Get_DataObject_Class_Name(TSG_Data_Object_Type Type)
@ SG_DATAOBJECT_TYPE_PointCloud
bool Set_History(CSG_MetaData &History, bool bOptions=true, bool bDataObjects=true)
bool Set_Callback(bool bActive=true)
bool is_Information(void) const
CSG_String Get_Type_Identifier(void) const
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)