GRASS and SAGA: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
m (cosmetics)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This tutorial is prepared to present possibilities for integration GRASS GIS with SAGA. The current version is only for linux installation.  
This tutorial is prepared to present possibilities for integration GRASS GIS with SAGA. The current version is only for Linux installation.  


==What is SAGA==
==What is SAGA==
SAGA GIS System for Automated Geoscientific Analyses is Open Source (GPL)  all puprose GIS system but mainly focused on terrain analysis. More information on [http://www.saga-gis.org SAGA Home Page]. First wersion of SAGA was only MS Windows program but form the 2.0 version is multiplatform program based on GTK wxWidgets and command line interface. This tutorial concerns on using GRASS and SAGA command line together in raster data processing. Vector processing will be added in the future.
SAGA GIS System for Automated Geoscientific Analyses is Open Source (GPL)  all purpose GIS system but mainly focused on terrain analysis. More information on [http://www.saga-gis.org SAGA Home Page]. First version of SAGA was only MS Windows program but form the 2.0 version is multiplatform program based on GTK wxWidgets and command line interface. This tutorial concerns on using GRASS and SAGA command line together in raster data processing. Vector processing will be added in the future.


==Installing SAGA==
==Installing SAGA==
Issues connected with SAGA installation presented below many occur/not occur depending on linux distribution, architecture, and GDAL/OGR/PROJ installation.
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 [http://sourceforge.net/projects/saga-gis/files/ here] The how-to install from source is described  [http://sourceforge.net/apps/trac/saga-gis/wiki/Compiling%20a%20Linux%20Unicode%20version here]. If we want to have direct access to GRASS data we need also to have GDAL/OGR compiled from source with [[Compile and install GDAL-GRASS plugin|grass-gdal plugin]] correctly compiled before SAGA compilation. It is important to check before SAGA configuration of wxWidgets installation: is or not is compiled with unicode support. Consequently we must configure SAGA with --enable-unicode or --disable-unicode. For unknown reason for me compilation from source available on [http://sourceforge.net/projects/saga-gis/files/ SAGA download page] always failed both on 32 and 64 bits architecture
Currently the only trustworthy installation on Linux is a compilation from the source. The source can be found [http://sourceforge.net/projects/saga-gis/files/ here] The how-to install from source is described  [http://sourceforge.net/apps/trac/saga-gis/wiki/Compiling%20a%20Linux%20Unicode%20version here]. If we want to have direct access to GRASS data we need also to have GDAL/OGR compiled from source with [[Compile and install GDAL-GRASS plugin|grass-gdal plugin]] correctly compiled before SAGA compilation. It is important to check before SAGA configuration of wxWidgets installation: is or not is compiled with unicode support. Consequently we must configure SAGA with --enable-unicode or --disable-unicode. For unknown reason for me compilation from source available on [http://sourceforge.net/projects/saga-gis/files/ SAGA download page] always failed both on 32 and 64 bits architecture


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. In that case direct access do GRASS data also is not possible.
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. In that case direct access do GRASS data also is not possible.


==Start to work with SAGA command line on Linux==
==Start to work with SAGA command line on Linux==
Line 16: Line 16:
export SAGA_MLB=/usr/local/lib/saga
export SAGA_MLB=/usr/local/lib/saga
</pre>
</pre>
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_installation_dir/lib. For example:
If libraries have been installed in other directory full path to that directory shall be added. If we use precompiled SAGA installation we shall add path to saga_installation_dir/lib. For example:
<pre>
<pre>
export SAGA_MLB=~/saga_gtk/lib
export SAGA_MLB=~/saga_gtk/lib
Line 67: Line 67:
==Processing data with SAGA command line==
==Processing data with SAGA command line==
Because this tutorial concerns SAGA and GRASS integration, only few information about SAGA command line syntax will be shown. More informations can be found on [http://www.saga-gis.org/saga_modules_doc/index.html SAGA module reference]
Because this tutorial concerns SAGA and GRASS integration, only few information about SAGA command line syntax will be shown. More informations can be found on [http://www.saga-gis.org/saga_modules_doc/index.html SAGA module reference]
Saga command line syntax is not typical linux command syntax and in my private opinion is sometimes inconsistent and difficult to manage. The most important are connected with small/large caps in options in similar commands (-Method, -method, -METHOD, inconsistence in option names (-elev, -ELEVATION, -dem, -DEM, depending on command) so it is difficult to give general rule about SAGA syntax. In general we can find proper option successively using SAGA command in pseudo-interactive mode.
Saga command line syntax is not typical Linux command syntax and in my private opinion is sometimes inconsistent and difficult to manage. The most important are connected with small/large caps in options in similar commands (-Method, -method, -METHOD, inconsistencies in option names (-elev, -ELEVATION, -dem, -DEM, depending on command) so it is difficult to give general rule about SAGA syntax. In general we can find proper option successively using SAGA command in pseudo-interactive mode.
First if we type saga_cmd program will show list of available libraries (with .so extension) which we can use in further data processing.  
First if we type saga_cmd program will show list of available libraries (with .so extension) which we can use in further data processing.  


Line 196: Line 196:
</pre>
</pre>


==5. working example==
==working example==
This working example is a single script which use DEM data stored in the GRASS dataset and calculate all possible accumulation maps available in SAGA (where Rho8, Dinf, KRA, DEMON and Braunscheister methods are not available in GRASS) and export them back to GRASS. Before calculating accumulation map SAGA remove depressions using deeping channel method (known also as breaching) also not available directly in GRASS (is part of r.watershed).
This working example is a single script which use DEM data stored in the GRASS dataset and calculate all possible accumulation maps available in SAGA (where Rho8, Dinf, KRA, DEMON and Braunschweiger Relief Model methods are not available in GRASS) and export them back to GRASS. Before calculating accumulation map SAGA remove depressions using deeping channel method (known also as breaching) also not available directly in GRASS (is part of {{cmd|r.watershed}}).
Run in GRASS environment for example in Spearfish dataset.
Run in GRASS environment for example in Spearfish dataset.


Line 237: Line 237:
</source>
</source>
Resulting map can be now processed in GRASS for example as accumulation maps in r.stream.extract
Resulting map can be now processed in GRASS for example as accumulation maps in r.stream.extract
[[Category: Documentation]]
[[Category: FAQ]]
[[Category: Linking to other languages]]
[[Category: Tutorial]]

Latest revision as of 18:59, 28 April 2013

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

What is SAGA

SAGA GIS System for Automated Geoscientific Analyses is Open Source (GPL) all purpose GIS system but mainly focused on terrain analysis. More information on SAGA Home Page. First version of SAGA was only MS Windows program but form the 2.0 version is multiplatform program based on GTK wxWidgets and command line interface. This tutorial concerns on using GRASS and SAGA command line together in raster data processing. Vector processing will be added in the future.

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 important to check before SAGA configuration of wxWidgets installation: is or not is compiled with unicode support. Consequently we must configure SAGA with --enable-unicode or --disable-unicode. For unknown reason for me compilation from source available on SAGA download page always failed both on 32 and 64 bits architecture

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. In that case direct access do GRASS data also is not possible.

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 installation we shall add path to saga_installation_dir/lib. For example:

export SAGA_MLB=~/saga_gtk/lib

Import and Export GRASS data to SAGA and backward

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 users):

  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.

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.

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 during 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.

Processing data with SAGA command line

Because this tutorial concerns SAGA and GRASS integration, only few information about SAGA command line syntax will be shown. More informations can be found on SAGA module reference Saga command line syntax is not typical Linux command syntax and in my private opinion is sometimes inconsistent and difficult to manage. The most important are connected with small/large caps in options in similar commands (-Method, -method, -METHOD, inconsistencies in option names (-elev, -ELEVATION, -dem, -DEM, depending on command) so it is difficult to give general rule about SAGA syntax. In general we can find proper option successively using SAGA command in pseudo-interactive mode. First if we type saga_cmd program will show list of available libraries (with .so extension) which we can use in further data processing.

saga_cmd

...
libta_hydrology.so
...


saga_cmd libta_hydrology -h

Next after we add library (without extension) saga will list list of commands for this library.

jarekj@jarekj:~$ saga_cmd libta_hydrology
_____________________________________________
  #####   ##   #####    ##
 ###     ###  ##       ###
  ###   # ## ##  #### # ##
   ### ##### ##    # #####
##### #   ##  ##### #   ##

SAGA CMD 2.0.4
_____________________________________________

Load table: /home/jarekj/saga.srs...

failed

error: module

available modules:
 0	- Parallel Processing
 1	- Recursive Upward Processing
 2	- Flow Tracing
[3]	- [interactive] Upslope Area
 4	- Upslope Area
[5]	- [interactive] Downslope Area
 6	- Flow Path Length
 7	- Slope Length
[8]	- [interactive] Isochrones Constant Speed
[9]	- [interactive] Isochrones Variable Speed
 10	- Cell Balance
[11]	- [interactive] Flow Sinuosity
[12]	- [interactive] Flow Depth
 13	- Edge Contamination
 14	- Topographic Indices
 15	- SAGA Wetness Index
 16	- Lake Flood
[17]	- [interactive] Lake Flood
 18	- Flow Accumulation, Mass-Flux Method
 19	- Flow Width

type -h or --help for further information

We can use only modules without square brackets around digits (non-interactive). Interactive modules can only be used in Graphical Mode. We can use command as a name (with parenthesis) or as a number. Both options are correct:

saga_cmd libta_hydrology 0 
saga_cmd libta_hydrology "Parallel Processing"

It will list the syntax with options and default values for given command. If option has default value listed if we do not type this option default value will be used.

 
jarekj@jarekj:~$ saga_cmd libta_hydrology "Parallel Processing"
_____________________________________________
  #####   ##   #####    ##
 ###     ###  ##       ###
  ###   # ## ##  #### # ##
   ### ##### ##    # #####
##### #   ##  ##### #   ##

SAGA CMD 2.0.4
_____________________________________________

Load table: /home/jarekj/saga.srs...

failed
library path:	/usr/local/lib/saga
library name:	libta_hydrology
module name :	Parallel Processing
author      :	(c) 2001 by O.Conrad
_____________________________________________
go...
Usage: Parallel Processing -ELEVATION <str> [-SINKROUTE <str>] [-WEIGHT <str>] -CAREA <str> [-CHEIGHT <str>] [-CSLOPE <str>] [-STEP <num>] [-CASPECT <str>] [-FLWPATH <str>] [-Method <num>] [-DOLINEAR] [-LINEARTHRS <str>] [-CONVERGENCE <str>]
  -ELEVATION:<str>  	Elevation
	Grid (input)
  -SINKROUTE:<str>  	Sink Routes
	Grid (optional input)
  -WEIGHT:<str>     	Weight
	Grid (optional input)
  -CAREA:<str>      	Catchment Area
	Grid (output)
  -CHEIGHT:<str>    	Catchment Height
	Grid (optional output)
  -CSLOPE:<str>     	Catchment Slope
	Grid (optional output)
  -STEP:<num>       	Step
	Integer
	Minimum: 1.000000
  -CASPECT:<str>    	Catchment Aspect
	Grid (optional output)
  -FLWPATH:<str>    	Flow Path Length
	Grid (optional output)
  -Method:<num>     	Method
	Choice
	Available Choices:
	[0] Deterministic 8
	[1] Rho 8
	[2] Braunschweiger Reliefmodell
	[3] Deterministic Infinity
	[4] Multiple Flow Direction
	[5] Multiple Triangular Flow Directon
  -DOLINEAR         	Linear Flow
	Boolean
  -LINEARTHRS:<str> 	Linear Flow Threshold
	Floating point
  -CONVERGENCE:<str>	Convergence
	Floating point
	Minimum: 0.000000

Now we have proper SAGA command:

saga_cmd libta_hydrology 0 -ELEVATION poccessed.sgrd  -CAREA Dinf_elev -Method 3

working example

This working example is a single script which use DEM data stored in the GRASS dataset and calculate all possible accumulation maps available in SAGA (where Rho8, Dinf, KRA, DEMON and Braunschweiger Relief Model methods are not available in GRASS) and export them back to GRASS. Before calculating accumulation map SAGA remove depressions using deeping channel method (known also as breaching) also not available directly in GRASS (is part of r.watershed). Run in GRASS environment for example in Spearfish dataset.

#!/bin/bash
export SAGA_MLB=/usr/local/lib/saga

#variables
methods=(D8 Rho8 Braun Dinf MFD MTFD KRA DEMON)
pref=saga_
input=elevation.dem

#export from grass, import to saga
r.out.gdal input=$input type=Float32 output=tmps_dem.tif nodata=-9999
saga_cmd libio_gdal 0 -GRIDS tmps_dem -FILE tmps_dem.tif

#preprocessing: options: 1 fill sinks; 0 breach
saga_cmd libta_preprocessor 1 -DEM  tmps_dem.sgrd -DEM_PREPROC tmps_proc -METHOD 0

for i in `seq 0 7`
do 

	if [ $i -le 5 ]; then
	
                #use  module parallel processing
                saga_cmd libta_hydrology 0 -ELEVATION tmps_proc.sgrd  -CAREA tmps_accum -Method $i
	else
                #use  module flow tracing for DEMON and KRA
		saga_cmd libta_hydrology 2 -ELEVATION  tmps_proc.sgrd -CAREA tmps_accum -Method $((i-5))
	fi
	
	saga_cmd libio_gdal 2 -GRIDS tmps_accum.sgrd -FILE tmps_accum.tif
	r.in.gdal -o input=tmps_accum.tif output=$pref${methods[i]}

done

#remove tmps
rm tmps*

Resulting map can be now processed in GRASS for example as accumulation maps in r.stream.extract