Skip to content
Prev 2853 / 29559 Next

mapping introduction

Dear Tom,

Thanks for the lecture notes, very interesting. I don't use the 
combination R - SAGA for the more advanced geostatistics you're 
describing, but rather to access more basic GIS modules such as the grid 
calculator and resample modules. But I don't see any reason why you 
couldn't use it for the other modules too.

I would recommend to have a look at the RSAGA package. It makes it 
easier to use SAGA. Moreover, it offers extended functionality, which, 
and I quote from the package help file, "include specific functions that 
are intended to be more user-friendly interfaces to the most frequently 
used SAGA modules. These higher-level interfaces support default values 
for the arguments and perform some error checking".

For those modules for which there is no direct interface (most), there 
is a low-level function (rsaga.geoprocessor) that can be used to run any 
SAGA modules and pass arguments. The interface is slightly different 
than when using the command line directly, see the RSAGA documentation 
for that. I just started using RSAGA so maybe you can also contact the 
author of the RSAGA package or use the SAGA user to user forum for more 
information.

Whether you use RSAGA or not, finding out the syntax of the different 
modules is probably the most complicated step because of missing 
documentation. For those interested, I normally use the following steps 
to figure out the arguments to a function (no R involved yet). I assume 
here you know which module you want to use; in this example I want to 
use the grid calculator to multiply grid 'inputA.sgrd' with grid 
'inputB.sgrd'

1) Before you run any command, you have to set the SAGA path, module 
path and data (working) directory, using the following commands (when 
you use RSAGA in R, this is easier):

 > cd c:\\program files\\saga_vc
 > set SAGA=.
 > SET SAGA_MLB=./modules
 > PATH=PATH;%SAGA%;%SAGA_MLB%

2) To find out which module library to call, you can select the module 
library that contains your module in the SAGA GUI (grid-calculus in this 
case) and than the description tab in the object properties window for 
the name of the dll (grid_calculus.dll).

3) Go back to the command line window and type in:

 > saga_cmd grid_calculus

4) You will be presented with a list of modules. Find the one you want 
to use. The grid calculator is module 1.

 > saga_cmd grid_calculus 1

5) you will be presented with the required syntax options. In my 
example, I'll need to give the names of the input and output layers and 
the equation:

 > saga_cmd grid_calculus 1 -INPUT inputA.sgrd;inputB.sgrd -RESULT 
output.sgrd -FORMUL a*b

 From within R, using the RSAGA package, you would use the following code:

rsaga.geoprocessor("grid_calculus", module=1, param=list(INPUT 
="inputA.sgrd;inputB.sgrd", RESULT=output.sgrd, FORMUL= 'a*b')

---
Below a small example of some real (but simple) code I used to resample 
grids to a certain extent and resolution. I wrote it as a function 
because I had to resample many grids to the same extent and resolution.

resample.SAGA <- function(sgrd.in, sgrd.out=sgrd, column=4294, 
rows=5415, east=12.210798, south=-13.451510, cell.size=0.008333, 
up.scale=0, down.scale=0){
options(digits=15)
rsaga.geoprocessor("grid_tools", module=0, param=list(INPUT=sgrd.in, 
GRID=sgrd.out, METHOD=1, KEEP_TYPE="", SYSTEM_SYSTEM_NX=column, 
SYSTEM_SYSTEM_NY=rows, SYSTEM_SYSTEM_X=east, SYSTEM_SYSTEM_Y=south, 
SYSTEM_SYSTEM_D=cell.size, SCALE_DOWN_METHOD=up.scale, 
SCALE_UP_METHOD=down.scale))}

---
In the following example I use the 'change grid values' module to change 
the grid values of a raster. The lookup table 'change.clim', with the 
mandatory three columns "Low Value", "High Value", and "Replace with", 
was generated in R:

library(foreign); library(RSAGA)
write.dbf(change.clim, "c:\\temp\\change_clim.dbf")
rsaga.geoprocessor("grid_tools", module=12, 
param=list(GRID_IN=c:\\temp\\clim.sgrd, GRID_OUT=c:\\temp\\climzon, 
METHOD=0, LOOKUP= c:\\temp\\chang_clim.dbf)
shell("del c:\\temp\\change_clim.dbf")

These are very simple examples, but hopefully they are useful nonetheless.

Paulo
Tomislav Hengl wrote:

        
Tomislav Hengl wrote: