Today we are proud to announce a major release of the RQGIS package providing an interface between R and QGIS. We have completeley rewritten RQGIS by now using reticulate to establish a tunnel to the Python QGIS API. To make RQGIS even more user-friendly, we have implemented, among others, following features:
set_envnow caches its output, so you have to call it only once
find_algorithmsnow supports regular expressions
run_qgisnow accepts R named arguments as input (see example below). The
load_outputargument is now logical. If
TRUEall specified output files will be loaded into R.
- RQGIS now supports simple features
For a detailed overview of all changes, please refer to the release news.
Let’s use RQGIS for calculating the slope and the aspect of a digital elevation model. First of all, we have to attach RQGIS and a digital elevation model (dem) to the global environment:
library("RQGIS") data("dem", package = "RQGIS")
Next, we need to find all paths necessary to run successfully the Python QGIS API. Please note, that the output of
set_env will be cached and reused in subsequent function calls. Note also, that
set_env is always faster when you indicate the path to your QGIS installation, in my case e.g.,
set_env(root = "C:/OSGeo4W64"). Secondly, we open a QGIS Python application with
Now, that we have established a tunnel to the QGIS Python API, we are ready for some geoprocessing. First, we need a geoalgorithm, that computes the slope and the aspect of a digital elevation model. To find the name of such a geoalgorithm, we use a regular expression which searches for the terms
aspect in all available QGIS geoalgorithms.
find_algorithms("slope(.+)?aspect") ##  "r.slope.aspect - Generates raster layers of slope, aspect, curvatures and partial derivatives from a elevation raster layer.--->grass7:r.slope.aspect" ##  "Slope, aspect, curvature----------------------------->saga:slopeaspectcurvature" ##  "r.slope.aspect - Generates raster layers of slope, aspect, curvatures and partial derivatives from a elevation raster layer.--->grass:r.slope.aspect"
We will use
grass7:r.slope.aspect. To retrieve its function parameters and arguments you can run
get_args_man to collect the parameter-argument list including all default values:
params <- get_args_man("grass7:r.slope.aspect")
As with previous RQGIS releases, you can still modify the parameter-argument list and submit it to
params$elevation <- dem params$slope <- "slope.tif" params$aspect <- "aspect.tif" run_qgis(alg = "grass7:r.slope.aspect", params = params)
But now you can also use R named arguments in
run_qgis, i.e. you can specify the geoalgorithms parameters directly in run_qgis (adapted from package rgrass7). Here, we specify the input- and the output-rasters. For all other parameters, default values will automatically be used. For more information on the R named arguments, please refer also to the documentation by running
out <- run_qgis(alg = "grass7:r.slope.aspect", elevation = dem, slope = "slope.tif", aspect = "aspect.tif", load_output = TRUE) ## $slope ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\RtmpmsSSA4/slope.tif" ## ## $aspect ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\RtmpmsSSA4/aspect.tif" ## ## $dxy ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\ac7b8544e8194dd1a1b8710e6091f1f3\\dxy.tif" ## ## $dxx ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\1576d9dc93434a578b3aeb16bedb17a2\\dxx.tif" ## ## $tcurvature ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\afea27676cc049faaa8526a486f13f70\\tcurvature.tif" ## ## $dx ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\2d71fd26b1aa4868a5cdfd0d7ad47a0c\\dx.tif" ## ## $dy ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\458f38f6c71947d3a37532e4bc6a6a53\\dy.tif" ## ## $pcurvature ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\80ad6fa1843d4d3a92ed0b4c6a39dcfa\\pcurvature.tif" ## ## $dyy ##  "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\6c52d235c8614a719954f1f744e3fef1\\dyy.tif"
TRUE automatically loads the resulting QGIS output back into R. Since we have specified two ouput files, the output will be loaded into R as a list (here named
out) with two elements: a slope and an aspect raster. However, in the background, QGIS calculates all terrain attributes and derivatives provided by
grass7:r.slope.aspect, and saves them to a temporary location. Of course, if you wish you can still access these layers from there.
Before running RQGIS, you need to install third-party software. We wrote a package vignette, which guides you through the installation process of QGIS, GRASS and SAGA on various platforms. To access the vignette, please run:
vignette("install_guide", package = "RQGIS")