GRASS and SAGA

From GRASS-Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This tutorial is prepared to present possibilities for integration GRASS GIS with SAGA. The current version is only for linux installation.

1. Installing SAGA

Issues connected with SAGA installation presented below many occur/not occur depending on linux distribution, architecture, and gdal/ogr/proj installation.

Currently the only trustworthy installation on linux is a compilation from the source. The source can be found here The how-to install from source is described here. If we want to have direct access to GRASS data we need also to have gdal/ogr compiled from source with grass-gdal plugin correctly compiled before SAGA compilation.

It is also possible to use precompiled version of SAGA but there are problems with gdal/ogr and proj working, depending on architecture (32 or 64 bits) and gdal installation. Direct access do GRASS data also is not possible.

2. Start to work with SAGA command line on linux

To start to work with SAGA command line on linux we need prior add access to SAGA libraries:

export SAGA_MLB=/usr/local/lib/saga

If libraries have been installed in other directory full path to that directory shall be added. If we use precompiled SAGA instalation we shall add path to saga_instalation_dir/lib. For example:

export SAGA_MLB=~/saga_gtk/lib

3. Import and Export GRASS data to SAGA and backward

3.1 Direct importing GRASS Data to SAGA (requires grass-gdal plugin)

It is possible to import grass data directly to SAGA without transitional export to GeoTIFF or other GDAL format. But this method has some limitation (some of them are well known for R' spgrass6 usres):

  1. plugin can import only the whole map. It ignores both current region resolution and region boundary
  2. there are problems with NULL interpretation if we use plugin to import grass raster directly.

So we can use direct import only if raster map have no NULL values and we need both extend an map resolution. On the other hand we need to use transactional GeoTiff.

How to import: in command line we need to use following SAGA command:

saga_cmd libio_gdal 0 -GRIDS elev -FILE ~/grassdata/spearfish60/PERMANENT/cellhd/elevation.dem

where:

  • saga_cmd: command
  • cmd libio_gdal: library tu use (inpit output gdal)
  • 0: comand read gdal symbol
  • -GRIDS elev option name of SAGA grid file (extensions will be added by SAGA)
  • -FILE ~/grassdata/spearfish60/PERMANENT/cellhd/elevation.dem path to imported GRASS data

More about SAGA command line syntax can be found here Regardless of the import method method SAGA will create three files with extensions: sgrd, sdat and mgrd. The file with extension sgrd shall be used in future operations.

3.2 Import to SAGA with transactional GeoTiff

If we have data with NULL values or we need import only fragment of data or data with resolution differ than original map resolution we need to use transactional GeoTiff to export data from GRASS and import it to SAGA. To do that we need two commands:

r.out.gdal input=dem type=Float32 output=tmp.tif nodata=-9999
saga_cmd libio_gdal 0 -GRIDS tmp_dem -FILE tmp.tif

The SAGA command was described in previous section, for GRASS command syntax see GRASS documentation.

Note that it is important to define nodata as -9999. SAGA use that value to define nodata. Floating point (FCELL and DCELL) GRASS nodata value is interpreted by SAGA as nan (not a number). Integer (CELL) GRASS nodata value is interpreted as a number.

3.2 Export from SAGA with transactional GeoTiff

It is impossible to export data directly to GRASS because GRASS raster format for GDAL is read-only. So whatever method we used to import data to SAGA we can export them only using transactional GeoTiff. Two methods of export are possible:

  • export from SAGA to GeoTiff and import file by file (every file in separate command) or:
  • export from SAGA to multiband GeoTiff (one file) and next import it to GRASS as set of bands. Because durng export to GeoTiff we cannot name separate bands (file names are not preserved) I recommend first method.

To properly export data from SAGA to GRASS we need two commands:

saga_cmd libio_gdal 2 -GRIDS demon.sgrd -FILE output.tif
r.in.gdal -o input=output.tif output=dem_demon

Note that in export SAGA command wee nedd use file name with extension .sgrd to proper export.

For unknown reason SAGA exports no data as -99999 instead of -9999 so if we import data with nodata cell we need to use r.null to change -99999 to no data after importing to GRASS.