RSAGA

In SAGA GIS we have seen how hydrological analysis is done with SAGA GIS. However, the work we did there is not optimal:

  • it required manual clicking
  • the overall workflow was brocken
  • no documentation
  • hard to reproduce

We want to change all of this by working with SAGA GIS modules from inside a R Script.

Task: SAGA R Workflows

Create a reproducible workflow with SAGA and RSAGA for flow accumulation from a digital elevation model.

  • Fill Sinks
  • Channel Network and Drainage Basins
  • Flow Accumulation

Visualize the results in R.

RSAGA Setup

SAGA has to be installed on your computer and R needs to know where.

library(RSAGA)
rsaga.env()
Search for SAGA command line program and modules... 
Done
$workspace
[1] "."

$cmd
[1] "saga_cmd"

$path
[1] "/usr/bin"

$modules
[1] "/usr/lib64/saga"

$version
[1] "9.3.1"

$cores
[1] NA

$parallel
[1] FALSE

$lib.prefix
[1] "lib"

If for some reason (e.g. working with a university computer) RSAGA throws an error, try the following:

library(link2GI)
linkSAGA() # wenn Pfad zu SAGA unbekannt
myenv <- rsaga.env(path="C:\\Program Files\\QGIS 3.8\\apps\\saga-ltr")

RSAGA Functions

rsaga.get.libraries()
Search for SAGA command line program and modules... 
Done
 [1] "climate_tools"           "contrib_perego"         
 [3] "db_odbc"                 "db_pgsql"               
 [5] "docs_html"               "garden_fractals"        
 [7] "garden_games"            "garden_webservices"     
 [9] "grid_analysis"           "grid_calculus_bsl"      
[11] "grid_calculus"           "grid_filter"            
[13] "grid_gridding"           "grid_spline"            
[15] "grid_tools"              "grid_visualisation"     
[17] "grids_tools"             "imagery_classification" 
[19] "imagery_isocluster"      "imagery_maxent"         
[21] "imagery_opencv"          "imagery_photogrammetry" 
[23] "imagery_segmentation"    "imagery_svm"            
[25] "imagery_tools"           "io_esri_e00"            
[27] "io_gdal"                 "io_gps"                 
[29] "io_grid_image"           "io_grid"                
[31] "io_shapes"               "io_table"               
[33] "io_virtual"              "io_webservices"         
[35] "pj_georeference"         "pj_geotrans"            
[37] "pj_proj4"                "pointcloud_tools"       
[39] "shapes_grid"             "shapes_lines"           
[41] "shapes_points"           "shapes_polygons"        
[43] "shapes_tools"            "shapes_transect"        
[45] "sim_air_flow"            "sim_cellular_automata"  
[47] "sim_ecosystems_hugget"   "sim_erosion"            
[49] "sim_fire_spreading"      "sim_geomorphology"      
[51] "sim_hydrology"           "sim_ihacres"            
[53] "sim_landscape_evolution" "sim_qm_of_esp"          
[55] "sim_rivflow"             "statistics_grid"        
[57] "statistics_kriging"      "statistics_points"      
[59] "statistics_regression"   "ta_channels"            
[61] "ta_cliffmetrics"         "ta_compound"            
[63] "ta_hydrology"            "ta_lighting"            
[65] "ta_morphometry"          "ta_preprocessor"        
[67] "ta_profiles"             "ta_slope_stability"     
[69] "table_calculus"          "table_tools"            
[71] "tin_tools"               "vis_3d_viewer"          
rsaga.get.modules(libs = "ta_hydrology")
Search for SAGA command line program and modules... 
Done
$ta_hydrology
   code                                   name interactive
1     0           Flow Accumulation (Top-Down)       FALSE
2     1          Flow Accumulation (Recursive)       FALSE
3     2       Flow Accumulation (Flow Tracing)       FALSE
4     4                           Upslope Area       FALSE
5     6                       Flow Path Length       FALSE
6     7                           Slope Length       FALSE
7    10                           Cell Balance       FALSE
8    13                     Edge Contamination       FALSE
9    15                     SAGA Wetness Index       FALSE
10   18   Flow Accumulation (Mass-Flux Method)       FALSE
11   19 Flow Width and Specific Catchment Area       FALSE
12   20              Topographic Wetness Index       FALSE
13   21                     Stream Power Index       FALSE
14   22                              LS Factor       FALSE
15   23               Melton Ruggedness Number       FALSE
16   24                                TCI Low       FALSE
17   25                 LS-Factor, Field Based       FALSE
18   26        Slope Limited Flow Accumulation       FALSE
19   27               Maximum Flow Path Length       FALSE
20   28                    Flow between fields       FALSE
21   29     Flow Accumulation (Parallelizable)       FALSE
22   30              Isochrones Variable Speed       FALSE
23   31                              CIT Index       FALSE
24   32                       Terrain Flooding       FALSE
rsaga.get.usage(lib = "ta_hydrology", module = "SAGA Wetness Index")
Search for SAGA command line program and modules... 
Done
library path: /usr/lib/saga/
library path: /usr/lib/saga/
library name: libta_hydrology
library     : ta_hydrology
Usage: saga_cmd ta_hydrology 15 [-DEM <str>] [-WEIGHT <str>] [-AREA <str>] [-SLOPE <str>] [-AREA_MOD <str>] [-TWI <str>] [-SUCTION <double>] [-AREA_TYPE <str>] [-SLOPE_TYPE <str>] [-SLOPE_MIN <double>] [-SLOPE_OFF <double>] [-SLOPE_WEIGHT <double>]
  -DEM:<str>                Elevation
    grid, input
  -WEIGHT:<str>             Weights
    grid, input, optional
  -AREA:<str>               Catchment Area
    grid, output, optional
  -SLOPE:<str>              Catchment Slope
    grid, output, optional
  -AREA_MOD:<str>           Modified Catchment Area
    grid, output, optional
  -TWI:<str>                Topographic Wetness Index
    grid, output
  -SUCTION:<double>         Suction
    floating point number
    Minimum: 0.000000
    Default: 10.000000
  -AREA_TYPE:<str>          Type of Area
    choice
    Available Choices:
    [0] total catchment area
    [1] square root of catchment area
    [2] specific catchment area
    Default: 2
  -SLOPE_TYPE:<str>         Type of Slope
    choice
    Available Choices:
    [0] local slope
    [1] catchment slope
    Default: 1
  -SLOPE_MIN:<double>       Minimum Slope
    floating point number
    Minimum: 0.000000
    Default: 0.000000
  -SLOPE_OFF:<double>       Offset Slope
    floating point number
    Minimum: 0.000000
    Default: 0.100000
  -SLOPE_WEIGHT:<double>    Slope Weighting
    floating point number
    Minimum: 0.000000
    Default: 1.000000 

Topographic Wetness Index

params <- list(DEM = "dem.tif", TWI ="twi")
rsaga.geoprocessor(lib = "ta_hydrology",
                   module = "SAGA Wetness Index",
                   param = params)
Important

dem.tif is a file on your computer! NOT an R Object!

Some SAGA modules are easier to execute via wrapper functions (see https://cran.r-project.org/web/packages/RSAGA/index.html)

With such a wrapper, the Topographic Wetness Index can be computed like this:

rsaga.wetness.index(in.dem = "dem.tif", out.wetness.index = "twi")