Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< !-- code folding -->
We proudly announce the release of RQGIS! RQGIS establishes an interface between R and QGIS, i.e. it allows the user to access the more than 1000 QGIS geoalgorithms from within R. To install it, run:
install.packages("RQGIS")
Naturally, you need to install other software (such as QGIS) to run RQGIS properly. Therefore, we wrote a package vignette, which guides you through the installation process of QGIS, GRASS and SAGA on various platforms. To access it, run:
vignette("install_guide", package = "RQGIS")
How to use RQGIS
To introduce RQGIS, we will demonstrate how to calculate the SAGA topographic wetness index. The first step is the creation of a QGIS environment. This is a list containing the paths RQGIS needs to access QGIS. Luckily for us, set_env
does this for us. We merely need to specify the root path to the QGIS installation. This is most likely something like C:/OSGeo4W~1
on Windows, /usr
on Linux and /usr/local/Cellar
on a Mac. If we do not specify a path to the QGIS root folder, set_env
tries to figure it out. This, however, might be time-consuming depending on the size of the drives to search.
library("RQGIS") env <- set_env()
Secondly, we need to find out the name of the function in QGIS which calculates the wetness index:
find_algorithms("wetness index", name_only = TRUE, qgis_env = env) ## [1] "taudem:topographicwetnessindex" "saga:sagawetnessindex" ## [3] "saga:topographicwetnessindextwi" ""
There are three algorithms containing the words wetness and index in their short description. Here, we choose saga:sagawetnessindex
. To retrieve the corresponding function arguments, we use get_args_man
. By setting option
to TRUE
, we indicate that we would like to use the default values, if available:
args <- get_args_man(alg = "saga:sagawetnessindex", options = TRUE, qgis_env = env) # print the retrieved list args ## $DEM ## [1] "None" ## ## $SUCTION ## [1] "10.0" ## ## $AREA_TYPE ## [1] "0" ## ## $SLOPE_TYPE ## [1] "0" ## ## $SLOPE_MIN ## [1] "0.0" ## ## $SLOPE_OFF ## [1] "0.1" ## ## $SLOPE_WEIGHT ## [1] "1.0" ## ## $AREA ## [1] "None" ## ## $SLOPE ## [1] "None" ## ## $AREA_MOD ## [1] "None" ## ## $TWI ## [1] "None"
Of course, we need to specify certain function arguments such as the input (DEM) and output (TWI) arguments. Please note that RQGIS accepts as input argument either the path to a spatial object or a spatial object residing in R. Here, we will use a digital elevation model, which comes with the RQGIS package:
# load data into R data("dem", package = "RQGIS") # define input args$DEM <- dem # specify output path args$TWI <- "twi.asc"
Finally, we can access QGIS from within R by supplying run_qgis
with the specified arguments as a list. Specifying also the load_output
-argument directly loads the QGIS output into R.
twi <- run_qgis(alg = "saga:sagawetnessindex", params = args, load_output = args$TWI, qgis_env = env) # visualize the result library("raster") hs <- hillShade(terrain(dem), terrain(dem, "aspect"), 40, 270) pal <- RColorBrewer::brewer.pal(6, "Blues") spplot(twi, col.regions = pal, alpha.regions = 0.8, scales = list(tck = c(1, 0)), colorkey = list(space = "bottom", width = 1, height = 0.5, axis.line = list(col = "black")), cuts = length(pal) - 1) + latticeExtra::as.layer(spplot(hs, col.regions = gray(0:100 / 100)), under = TRUE)
For more information on RQGIS, please refer to https://github.com/jannes-m/RQGIS.
< !-- dynamically load mathjax for compatibility with self-contained -->
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.