Difference between revisions of "Automating SAGA Workflows Using Command Line Scripting"
Erinturnbull (talk | contribs) |
Erinturnbull (talk | contribs) (Added a section on executing the basic help commands to explore the Help functionality in saga_cmd) |
||
Line 39: | Line 39: | ||
|} |
|} |
||
+ | == SAGA GIS Command Line Interface == |
||
+ | SAGA GIS provides access to their tools via a command line interface which allows a user to set the parameters for and run a tool directly. By chaining these together in a script file, a user can build an analysis workflow which can be run repeatedly on-demand or scheduled using Task Scheduler or cron. This section discusses the basics of how to use this interface; for full documentation, refer to the [http://www.saga-gis.org/saga_tool_doc/ SAGA tool documentation] which has an example of how to call the tool from the command line at the bottom of the page. |
||
+ | === Accessing the Help & Module List === |
||
⚫ | |||
+ | [[File:fig1.jpg|frame|The results of running the <code>saga_cmd --help</code> command]] |
||
+ | There are several commands that can help get you started using the command line interface, which are listed below. The SAGA tool documentation is also a useful resource to get started with. |
||
⚫ | |||
+ | ; <code>saga_cmd</code>: When run with no parameters, this will display a list of available libraries. |
||
⚫ | |||
+ | ; <code>saga_cmd --help</code>: The <code>--help</code> flag will show you a summary of how to use the SAGA command line interface. |
||
+ | ; <code>saga_cmd --help LIBRARY_NAME</code>: When you also specify a library after the <code>--help</code> flag, the interface will show you a list of tools in that module with numbers for each. |
||
+ | ; <code>saga_cmd --help LIBRARY_NAME TOOL_#</code>: If you also specify a tool number, the help for that tool will be shown including the list of parameters. |
||
+ | Try out these commands to see if you can get the list of tools in the ta_morphometry library and look at the parameters for the "Slope, Aspect, Curvature" tool. We'll use these in the next section. |
||
⚫ | The following are the basics for understanding the use of SAGA at the command line. Users can use the command line to quickly and efficiently run scripts to execute functions with specified parameters. This can be done either at a Windows command prompt, or a UNIX shell prompt. Although there are various UNIX shells available, probably the most popular one now, thanks to being the default shell for MacOS and most Linux distributions, is Bash. In the examples below, Bash syntax is used, and "[linux]" is used as a short form for any operating system that is running the bash shell. |
||
+ | <br clear="both"> |
||
⚫ | |||
− | To access help files the following script can be used: |
||
− | $ saga_cmd [--help] [windows] |
||
⚫ | |||
− | OR |
||
⚫ | |||
− | saga_cmd [-h] [linux] |
||
⚫ | The following are the basics for understanding the use of SAGA at the command line. Users can use the command line to quickly and efficiently run scripts to execute functions with specified parameters. This can be done either at a Windows command prompt, or a UNIX shell prompt. Although there are various UNIX shells available, probably the most popular one now, thanks to being the default shell for MacOS and most Linux distributions, is Bash. In the examples below, Bash syntax is used, and "[linux]" is used as a short form for any operating system that is running the bash shell. |
||
− | This pulls up a way to decode command scripts (see figure 1) |
||
− | |||
− | [[File:Fig1.jpg]] |
||
− | |||
− | Figure 1. help file outputs in SAGA. |
||
− | By using various series of this language, the user is able to automatically pick from the library, module, options and produce a script. |
||
To call on the library, a simple call can be used (“saga_cmd”). Simply calling on the command line automatically produces a list of the module libraries (see figure 2). |
To call on the library, a simple call can be used (“saga_cmd”). Simply calling on the command line automatically produces a list of the module libraries (see figure 2). |
Revision as of 08:52, 1 October 2020
Please note that this tutorial is not quite finished. Since there isn't a lot of material out there on this topic, it will hopefully prove useful to anyone researching this topic, and future contributions/edits are certainly welcome!
Contents
Introduction
Automating GIS tasks offers industry a means to efficiently repeat the same task. Doing so can reduce manual errors in processes as well as increase productivity. Many platforms offer this feature, but this tutorial will focus on the System for Automated Geoscientific Analyses (SAGA), created by the Department of Physical Geography at the University of Hamburg in Germany. It is one of the more popular free open-source software and offers both a command line option as well as Python integration for executing GIS tasks.
This tutorial is aimed at those who have some comfort using the command line interface to execute commands and some basic Python skills.
Objectives
1. Understand how to call SAGA tasks from the command line
2. Understand how to automate sequences of tasks using the command line and Python
3. Provide implementation differences between Windows and Unix environments
Getting Started
Installation
To install the software, please use the links below to install SAGA or Python as required.
Set PATH variable
To access the SAGA tools from a command line environment, you will use the saga_cmd tool (saga_cmd.exe on Windows). To make running this easier, you can add the path to the SAGA directory to your system path so that your operating system knows where to find saga_cmd. To do so, use the following commands depending on your operating system, replacing [SAGA_PATH] with the path to the directory where SAGA was installed. Otherwise, you'll have to enter the full path every time.
Windows | Mac / Unix |
---|---|
set PATH=%PATH%;[SAGA_PATH] |
export PATH=$PATH:[SAGA_PATH]
|
SAGA GIS Command Line Interface
SAGA GIS provides access to their tools via a command line interface which allows a user to set the parameters for and run a tool directly. By chaining these together in a script file, a user can build an analysis workflow which can be run repeatedly on-demand or scheduled using Task Scheduler or cron. This section discusses the basics of how to use this interface; for full documentation, refer to the SAGA tool documentation which has an example of how to call the tool from the command line at the bottom of the page.
Accessing the Help & Module List
There are several commands that can help get you started using the command line interface, which are listed below. The SAGA tool documentation is also a useful resource to get started with.
saga_cmd
- When run with no parameters, this will display a list of available libraries.
saga_cmd --help
- The
--help
flag will show you a summary of how to use the SAGA command line interface. saga_cmd --help LIBRARY_NAME
- When you also specify a library after the
--help
flag, the interface will show you a list of tools in that module with numbers for each. saga_cmd --help LIBRARY_NAME TOOL_#
- If you also specify a tool number, the help for that tool will be shown including the list of parameters.
Try out these commands to see if you can get the list of tools in the ta_morphometry library and look at the parameters for the "Slope, Aspect, Curvature" tool. We'll use these in the next section.
Methodology
SAGA_CMD
Exploring the environment of the command line application (saga_cmd)
The following are the basics for understanding the use of SAGA at the command line. Users can use the command line to quickly and efficiently run scripts to execute functions with specified parameters. This can be done either at a Windows command prompt, or a UNIX shell prompt. Although there are various UNIX shells available, probably the most popular one now, thanks to being the default shell for MacOS and most Linux distributions, is Bash. In the examples below, Bash syntax is used, and "[linux]" is used as a short form for any operating system that is running the bash shell.
To call on the library, a simple call can be used (“saga_cmd”). Simply calling on the command line automatically produces a list of the module libraries (see figure 2).
Figure 2. Command line results in SAGA for calling on the existing modules by using the command line.
To call on the library, which identifies functionality within a module, the user must input the name of the module after calling on “saga_cmd”. The result ends up with a list of the tools and functions of that specific library (see figure 3).
Figure 3. SAGA output results where calling on the command line and the associated library (this is using saga_cmd ta_lighting to call on the lighting module in SAGA).
For example, a workflow for hillshade analysis (calculating the angle between the surface and the incoming light beams in radians) can be used by automating the process of taking ASCII grids or DEMS, converting them into SAGA grids and then creating an analytical hillshade for each tile. The following command would be used to pull up all parameters in the hillshade analysis (see figure 4). Parameters must be defined and then can be run within the command line.
$ saga_cmd ta_lighting "Analytical Hillshading"
Reminder: (“ta_lighting” refers to the module it belongs to and “Analytical Hillshading” refers to the function name in the library of the module).
Figure 4. Variables to define in hillshade analysis.
Also, when left clicking “Analytical Hillshading” in the index, you can select “Create Script Command File” (see figure 5), where you can copy and paste command snippets to alter parameters (see figure 6).
Figure 5. Left clicking in index allows option to “Create Script Command File” to enable editing process of parameters included in module.
Figure 6. Script command created in process of figure 5.
When doing more basic tasks in SAGA, the command line is the most useful, as it requires the least amount of code associated with these processes. If the user is more interested in doing a more complicated analysis, or combine various tools and functions, this is where an outside program needs to be used. In this case, Python has been chosen to modify scripts and increase the functionality of SAGA.
Using Python
By using a similar workflow as the saga_cmd, the following can be used in Python to do the same tasks.
To call on module library:
mlb= saga_api.CSG_Module_Library
To choose a module from the library:
m= mlb.Get_Module(1)
- The number 1 refers to the ID number of the module (see figure 1 for that list in saga_cmd). You can also add in the name of the module, m= mlb.Get_Module(‘Analytical Hillshade”)
To get and set parameters:
P =m.Get_Parameters()
P(‘FILE’).Set_Value(fASC)
To run module:
print m.Get_Name() + ‘ : ’ = p( ‘FILE’ ) .asString()
(Sorry, to be continued...)
For More Information...
http://sourceforge.net/p/saga-gis/wiki/Creating%20Python%20scripts/