Rsagacmd provides an R scripting interface to the open-source SAGA-GIS software. It dynamically generates an R function for every SAGA-GIS geoprocessing tool in your installed SAGA-GIS version, so all tools (700+) are available from R and always stay in sync with your SAGA-GIS installation.
Installation
Rsagacmd requires SAGA-GIS (>= 2.3.1) to be installed on your system. It is available from SourceForge (Windows and macOS binaries) and from the Debian/Ubuntu software repositories.
Install the R package from CRAN:
install.packages("Rsagacmd")or the development version from GitHub:
# install.packages("pak")
pak::pak("stevenpawley/Rsagacmd")Creating the link to SAGA-GIS
The main entry point is saga_gis(), which locates the
saga_cmd executable (using search_saga() if no
path is given) and returns a saga object: a named list of
libraries, each containing its tools as functions.
library(Rsagacmd)
library(terra)
# initiate a saga object
saga <- saga_gis(raster_backend = "terra")
# load the bundled example DEM
srtm <- read_srtm()
# run a SAGA-GIS tool: library$tool(...)
mbi <- saga$ta_morphometry$mass_balance_index(dem = srtm)Each tool function has the same arguments as the SAGA-GIS command line tool, except that argument names are always lowercase. Code autocompletion in RStudio makes it easy to discover a tool’s inputs, outputs and options. To see a tool’s parameters in the console, just print it:
saga$ta_morphometry$terrain_ruggedness_index_triTo search for tools by name, or to browse what is available in a library:
search_tools(saga, pattern = "terrain")
tidy(saga) # summarize all libraries
tidy(saga$ta_morphometry) # summarize the tools in one library
tidy(saga$ta_morphometry$terrain_ruggedness_index_tri) # summarize one toolPassing data between R and SAGA-GIS
Rsagacmd automatically passes the common spatial and tabular R object classes to SAGA-GIS and loads the results back into R:
- Raster data is handled by terra (default) or
stars, selected with
raster_backend. Raster outputs are loaded asSpatRasterorstarsobjects. The default on-disk format for tool outputs can be changed withraster_format:
show_raster_formats()
#> # A tibble: 3 × 2
#> driver extension
#> <chr> <chr>
#> 1 SAGA .sgrd
#> 2 SAGA Compressed .sg-grd-z
#> 3 GeoTIFF .tif- Vector data is written in the
vector_formatand loaded assf(default),SpatVectororSpatVectorProxyobjects, selected withvector_backend:
show_vector_formats()
#> # A tibble: 3 × 2
#> driver extension
#> <chr> <chr>
#> 1 ESRI Shapefile .shp
#> 2 GeoPackage .gpkg
#> 3 GeoJSON .geojson- Tabular data is loaded as tibbles.
Data can also always be passed as file paths, provided they are in a supported format (GDAL-supported single-band rasters, OGR-supported vector data, comma- or tab-delimited text files). Note that SAGA-GIS does not handle multi-band rasters: if a single band of a multiband raster is passed, Rsagacmd extracts it automatically.
Chaining tools with pipes
For convenience, any tool outputs that are not explicitly specified are automatically written to temporary files and loaded back as R objects. This makes it easy to chain several processing steps with the pipe operator:
library(sf)
# area of interest as a simple features object
prj_bnd <- st_as_sf(
data.frame(area = "my area"),
st_as_sfc(st_bbox(c(xmin = 320000, xmax = 340000,
ymin = 5890000, ymax = 5910000), crs = 3402))
)
dem <- read_srtm()
# clip, resample, and calculate land surface parameters in one statement
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)Temporary files created this way can be listed with
saga_show_tmpfiles() and removed with
saga_remove_tmpfiles(h = 0). Automatic tempfile output can
be disabled per tool with .all_outputs = FALSE, or globally
with saga_gis(all_outputs = FALSE).
Caching and performance
saga_gis() caches the parsed SAGA-GIS tool documentation
in the per-user cache directory so that repeated calls are much faster
(controlled with use_cache and refresh; delete
the cache with saga_clear_cache()). For large rasters that
do not fit in memory, file caching can be enabled with
grid_caching = TRUE (note that this forces
cores = 1 because SAGA-GIS file caching is not
thread-safe).
Getting help
-
?saga_gisfor all options when creating thesagaobject. - Print any tool
(e.g.
saga$ta_morphometry$slope_aspect_curvature) to see its parameters, or usetidy()on it. -
saga_docs(saga$ta_morphometry$slope_aspect_curvature)opens the tool’s official online documentation in your browser. - A helper for the MRVBF algorithm is provided by
mrvbf_threshold():
mrvbf_threshold(res = 10)
#> [1] 27.9632