MyNixOS website logo
Description

Linking R with the Open-Source 'SAGA-GIS' Software.

Provides an R scripting interface to the open-source 'SAGA-GIS' (System for Automated Geoscientific Analyses Geographical Information System) software. 'Rsagacmd' dynamically generates R functions for every 'SAGA-GIS' geoprocessing tool based on the user's currently installed 'SAGA-GIS' version. These functions are contained within an S3 object and are accessed as a named list of libraries and tools. This structure facilitates an easier scripting experience by organizing the large number of 'SAGA-GIS' geoprocessing tools (>700) by their respective library. Interactive scripting can fully take advantage of code autocompletion tools (e.g. in 'Rstudio'), allowing for each tools syntax to be quickly recognized. Furthermore, the most common types of spatial data (via the 'terra', 'sp', and 'sf' packages) along with non-spatial data are automatically passed from R to the 'SAGA-GIS' command line tool for geoprocessing operations, and the results are loaded as the appropriate R object. Outputs from individual 'SAGA-GIS' tools can also be chained using pipes from the 'magrittr' and 'dplyr' packages to combine complex geoprocessing operations together in a single statement. 'SAGA-GIS' is available under a GPLv2 / LGPLv2 licence from <https://sourceforge.net/projects/saga-gis/> including Windows x86/x64 and macOS binaries. SAGA-GIS is also included in Debian/Ubuntu default software repositories. Rsagacmd has currently been tested on 'SAGA-GIS' versions from 2.3.1 to 9.2 on Windows, Linux and macOS.

Rsagacmd

CRANstatus [Downloads Codecov testcoverage R-CMD-check Lifecycle:stable

Rsagacmd - a package for linking R with SAGA-GIS

Rsagacmd provides an R scripting interface to the open-source SAGA-GIS (https://sourceforge.net/projects/saga-gis/) software. The current version has been tested using SAGA-GIS 2.3.2, 5.0.0 - 9.2 on Windows (x64), OS X and Linux.

Contents

Description

Rsagacmd provides a complete interface between R and SAGA-GIS. The package allows all SAGA-GIS tools and libraries to be used from within R. Further, the most common geospatial datatypes within R (rasters, simple features, and sp objects) are seamlessly passed between R and SAGA-GIS during geoprocessing operations.

Rsagacmd is unrelated to the RSAGA package (https://cran.r-project.org/package=RSAGA), which provides a command line parser and a subset of pre-defined functions to interface with SAGA-GIS. In contrast, Rsagacmd provides links with SAGA-GIS by dynamically generating R functions for every SAGA-GIS tool that is contained in the user’s installed SAGA-GIS version. This means that every SAGA-GIS tool is available for use within R (other than the interactive tools), and Rsagacmd always remains up-to-date with new versions of SAGA-GIS. Custom tools that have been created using SAGA’s toolchains (https://rohanfisher.wordpress.com/saga-tool-chains/) will also be accessible via Rsagacmd.

Package installation

CRAN version

Rsagacmd is now available on CRAN. To install this version run:

install.packages("Rsagacmd")

In your R session.

Development version

First install the devtools package:

install.packages("devtools")

Then install Rsagacmd from github:

library(devtools)

install_github("stevenpawley/Rsagacmd")

Usage

The primary function in Rsagacmd is the saga_gis function that returns an object containing all of the SAGA-GIS libraries and tools in the same structure as what is accessible within the GIS software itself. Each tool is nested within its respective library and can be accessed by:

library(Rsagacmd)
library(terra)
#> terra 1.7.55

# initiate a saga object
saga <- saga_gis(raster_backend = "terra")
#> Multiple installations of SAGA-GIS were found at:
#> /Applications/SAGA.app/Contents/MacOS/saga_cmd
#> /Applications/QGIS.app/Contents/MacOS/bin/saga_cmd
#> Choosing newest version. Manually specify the location when calling saga_gis() to use an older version

# load the example data
srtm <- read_srtm()

# access the libraries and tools
saga$ta_morphometry$mass_balance_index(dem = srtm)
#> class       : SpatRaster 
#> dimensions  : 400, 400, 1  (nrow, ncol, nlyr)
#> resolution  : 100, 100  (x, y)
#> extent      : 310009.9, 350009.9, 5879989, 5919989  (xmin, xmax, ymin, ymax)
#> coord. ref. : NAD83(CSRS) / Alberta 10-TM (Forest) (EPSG:3402) 
#> source      : file19ef60012a7.sdat 
#> name        : file19ef60012a7

This facilitates an easier scripting experience by organizing the large number of SAGA-GIS tools (> 700) by their respective library. Each function’s syntax is the same as when using the SAGA-GIS command line tool directly except that Rsagacmd always uses lowercase arguments). Furthermore, because the arguments (called identifiers in SAGA-GIS) for many SAGA-GIS tools are not consistently named, the user can also take advantage of code autocompletion tools in RStudio, allowing for each tools’ inputs, outputs and options to be more easily recognized.

Passing geospatial and tabular data between R and SAGA-GIS

Rsagacmd aims to facilitate a seamless interface to the open-source SAGA-GIS by providing access to all SAGA-GIS geoprocessing tools in a R-like manner. In addition to mapping R functions to execute SAGA-GIS tools, Rsagacmd automatically handles the passing of geospatial and tabular data contained from the R environment to SAGA-GIS.

Rsagacmd uses the SAGA-GIS command line interface to perform geoprocessing operations. Therefore, all of the Rsagacmd tools allow paths to the input data to be used as arguments, if the data is stored in the appropriate file formats (GDAL-supported single-band rasters, OGR supported vector data, and comma- or tab-delimited text files for tabular data). In addition, Rsagacmd currently supports the following R object classes to pass data to SAGA-GIS, and to load the results back into the R environment:

  • Raster data handling is provided by the R terra package as the default backend. Raster-based outputs from SAGA-GIS tools are loaded as SpatRaster objects. For more details, see the ‘Handling of raster data’. Other raster backends can be specified when creating the link to SAGA-GIS using saga_gis(raster_backend = "stars") for example.

  • Vector features that result from SAGA-GIS geoprocessing operations are output the format specified by the ‘vector_format’ arguments (default is ESRI Shapefile for SAGA-GIS versions < 7.0 and GeoPackage for newer versions) and are loaded into the R environment as simple features (sf) objects. Different vector backends are also supported, including ‘sf’, ‘SpatVector’, and ‘SpatVectorProxy’, which can also be specified when initiating the link to SAGA-GIS using saga_gis(vector_backend = "SpatVector").

  • Tabular data from SAGA-GIS tools are loaded as tibbles

The results from tools that return multiple outputs are loaded into the R environment as a named list of the appropriate R object classes.

Notes on handing multi-band raster datasets by Rsagacmd and SAGA-GIS

SAGA-GIS does not handle multi-band rasters and native SAGA GIS Binary file format (.sgrd) supports only single band data. Therefore when passing raster data to most SAGA-GIS tools using Rsagacmd, the data should represent single raster bands, specified as either the path to the single raster band. Subsetting of raster data is performed automatically by Rsagacmd in the case of when a single band from a SpatRaster or stars object is passed to a SAGA-GIS tool. This occurs in by either passing the filename of the raster to the SAGA-GIS command line, or by writing the data to a temporary file.

Combining SAGA-GIS tools with pipes

For convenience, non-optional outputs from SAGA-GIS are automatically saved to tempfiles if outputs are not explicitly stated, and then loaded as the appropriate R object (e.g., SpatRaster, sf object, or a tibble).

This means that Rsagacmd can be used with %>% to quickly chain together complex geoprocessing operations:

# Generate random terrain and save to file
dem <- saga$grid_calculus$random_terrain(
  target_out_grid = tempfile(fileext = ".sgrd")
)

# Terrain ruggedness index and automatically save the result to a tempfile
tri <- saga$ta_morphometry$terrain_ruggedness_index_tri(dem = dem, radius = 3)
plot(tri)

This example will write the output terrain ruggedness index to a temporary file, and will automatically load the result into the R environment as a SpatRaster object. This was implemented for convenience, and so that the user can also create complex workflows that require very little code. It is also means that you can combine several processing steps with pipes:

library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE

# read project area as a simple features object
prj_bnd <- st_polygon(list(matrix(
  c(320000, 340000, 5880000, 5900000),
  nrow = 1
)))

prj_bnd <- 
  st_bbox(c(xmin = 320000, xmax = 340000, ymin = 5890000, ymax = 5910000)) %>% 
  st_set_crs(3402) %>% 
  st_as_sfc()

prj_bnd <- st_as_sf(data.frame(area = "my area"), prj_bnd)

dem <- read_srtm()

# clip dem to shape, resample, and calculate land surface parameters
lsps <- dem %>%
  saga$shapes_grid$clip_grid_with_rectangle(shapes = prj_bnd) %>%
  aggregate(fact = 2, fun = "mean") %>%
  saga$ta_morphometry$slope_aspect_curvature()

plot(lsps$slope)

In the above example, three tools are joined together using pipes, and only the land surface parameter grids are returned as SpatRaster objects. The intermediate processing steps are dealt with automatically by saving the outputs as tempfiles. When dealing with high-resolution and/or larger raster data, these tempfiles can start to consume a significant amount of disk space over a session. If required, temporary files can be cleaned during the session using:

saga_remove_tmpfiles(h = 0)
#> Removing Rsagacmd temporary files h=0
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef60012a7.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef61f3a27a.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef2ec55176.gpkg
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef1c6731f7.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef561275bc.sdat
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef55efb5c9.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef6c87cb32.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef472d7748.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef79f23c79.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19efe606a7b.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef5beeb89c.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef159a38eb.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef400ed761.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19efe5e4222.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef4e47cd8c.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef4c07ca7a.sgrd
#> /var/folders/_m/kbp8r1612yj1xl6ndb2y8vpm0000gn/T//RtmpJHVnhB/file19ef137e3695.sgrd

where h is minimum age (in number of hours) of tempfiles for removal, so h = 0 will remove all tempfiles that were automatically created by Rsagacmd.

The behaviour of automatically outputting results to tempfiles can be disabled for any specific tool by using .all_outputs = FALSE or can be disabled globally when the link to SAGA-GIS is created using saga_gis(all_outputs = FALSE). In this case, the output arguments need to be specified manually, e.g.:

tri <- saga$ta_morphometry$terrain_ruggedness_index_tri(
    dem = dem, 
    radius = 3, 
    tri = tempfile(fileext = ".sgrd"),
    .all_outputs = FALSE
)
Metadata

Version

0.4.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows