Pages

Sunday, March 31, 2013

RSAGA: Getting Started

RSAGA provides access to geocomputation capabilities of SAGA GIS from within R environment. Having SAGA GIS installed is a (quite obvious) pre-requirement to use RSAGA. 

In Linux x64 sometimes additional preparations are needed. In Linux SAGA as well as other software that would like to use SAGA modules usually searches for  them in /usr/lib/saga, but if your Linux is x64, they usually will be located in /usr/lib64/saga. Of course you may set up proper environmental variables, but the most lazy and overall-effective way is just to add symbolic link to /usr/lib64/saga (or whatever a correct path is) from /usr/lib/saga:
:~> sudo ln -s /usr/lib64/saga /usr/lib/saga
Now no app should miss these modules.

Let's to set up an environment in R.
> library(RSAGA)

# set up rsaga environment:
> work_env <- rsaga.env() 
> work_env 
$workspace [1] "." 
$cmd [1] "saga_cmd" 
$path [1] "/usr/bin" 
$modules [1] "/usr/lib/saga" 
$version [1] "2.0.8"

Note that in this case modules appeared to be located in /usr/lib when they are actually in /usr/lib64. Thanks to the symbolic link, we don't have to provide RSAGA with the real location. If you need to set up detailed environmental parameters run:
> work_env <- rsaga.env(workspace='path_to_your_folder',
+                       path = '/usr/bin',
+                       modules = '/usr/lib64/saga')

It's time to get the list of available libraries (sets of modules):
> rsaga.get.libraries() 
 [1] "contrib_a_perego"            "docs_html" 
 [3] "docs_pdf"                    "garden_3d_viewer" 
 [5] "garden_webservices"          "geostatistics_grid" 
 ...and so on

Each library has a number of modules inside. And we will use modules, not libraries in geoprocessing. Let's get a list of modules inside one of the library:
> rsaga.get.modules('grid_filter') 

$grid_filter
   code                       name interactive
1     0              Simple Filter       FALSE
2     1            Gaussian Filter       FALSE
3     2           Laplacian Filter       FALSE
4     3 Multi Direction Lee Filter       FALSE
5     4        User Defined Filter       FALSE
6     5              Filter Clumps       FALSE
7     6            Majority Filter       FALSE
8     7   DTM Filter (slope-based)       FALSE
9     8       Morphological Filter       FALSE
10    9                Rank Filter       FALSE


In column 'name' we have a module name; column 'interactive' indicates whether this module is interactive, and column 'code' contains the code to be used when the module is called. Now we would like for example to know how to use module 'DTM Filter (slope-based)':

> rsaga.get.usage('grid_filter', 7)

Usage: saga_cmd -INPUT <str> [-RADIUS <num>] [-TERRAINSLOPE <str>] [-STDDEV] -GROUND <str> -NONGROUND <str>
  -INPUT:<str>       Grid to filter
Grid (input)
  -RADIUS:<num>       Search Radius
Integer
Minimum: 1.000000
  -TERRAINSLOPE:<str> Approx. Terrain Slope
Floating point
Minimum: 0.000000
  -STDDEV             Use Confidence Interval
Boolean
  -GROUND:<str>       Bare Earth
Grid (output)
  -NONGROUND:<str>   Removed Objects
Grid (output)

library path: /usr/lib/saga
library name: libgrid_filter
module name : DTM Filter (slope-based)

Now we have detailed information about parameters we have to provide as well as data types that these parameters must have. But before we proceed, we have to prepare the data. RSAGA will accept only grid formats for processing, so one have to convert data to suitable format:
> rsaga.import.gdal('lidar.tif')
As a result 'lidar.srgd' file will be created in the same folder as original file.

Finally we are ready to execute our first RSAGA geoprocessing command:
rsaga.geoprocessor('grid_filter', module = 7, 
+                    env = work_env,
+                    param = list(INPUT = 'lidar.sgrd',
+                                 RADIUS = 20,
+                                 TERRAINSLOPE = 0,
+                                 STDDEV = T,
+                                 GROUND = 'ground',
+                                 NONGROUND = 'non_ground')
+                    )

Instead of conclusion

RSAGA has a certain degree of inconvenience in usage. It only works with files in .srgd format and both input and output have to be physically stored on your hard drive. So it is better to use it [instead of the SAGA itself] only for routine tasks. 

4 comments:

  1. Hi there,
    Could you please open your full feed for the post to be shown on r-bloggers.com ?

    Thanks,
    Tal

    ReplyDelete
  2. thanks for posting this ... I was able to run rsaga on a ubuntu 32 bits ...

    ReplyDelete
  3. Hi. I am Ozias. Thanks for the post. I have to use saga in a linux environment. But after installing RSAGA, it loads very well (library(RSAGA)). Next, one has to give the path where the folder of RSAGA is located. Which I have done. But when I try to use the command related to RSAGA it doesn´t work. I got the
    error:
    Error in rsaga.env(path = "/crex/proj/uppstore2018133/SAGA-GIS") :
    SAGA command line program saga_cmd not found in the specified path:
    /crex/proj/uppstore2018133/SAGA-GIS. I am using the same saga folder which was working for me in window. May be that´s the mistake. I have since try to download a saga that works with linux. Any help will be appreciated.

    ReplyDelete