QGIS 3.14 (Pi) introduced the qgis_process tool – a command-line executable that “allows users to run processing algorithms (both built-in, and those provided by plugins) directly from the console” developed by Nyall Dawson and sponsored by the Swedish User Group. You can find the Pull Request of the feature here. You can check this post to learn more.
Recently, Dewey Dunnington (@paleolimbot) created an R package (aptly named qgisprocess) that allows you to call processing algorithms straight from R. You can check out the qgisprocess R package in this GitHub repo. Read below to learn how you can utilize this with R and QGIS 3.14.
We can install the qgisprocess package from its GitHub repo using the remotes package. To do so, type:
We can then check if the qgisprocess package was installed by calling some functions.
library(qgisprocess) qgis_path()  "qgis_process" # check QGIS version qgis_version()  "3.14.16-Pi" # check available QGIS algorithms qgis_algorithms() # A tibble: 984 x 5 provider provider_title algorithm algorithm_id algorithm_title <chr> <chr> <chr> <chr> <chr> 1 3d QGIS (3D) 3d:tessellate tessellate Tessellate 2 gdal GDAL gdal:aspect aspect Aspect 3 gdal GDAL gdal:assignprojection assignprojection Assign projection 4 gdal GDAL gdal:buffervectors buffervectors Buffer vectors 5 gdal GDAL gdal:buildvirtualraster buildvirtualraster Build virtual raster 6 gdal GDAL gdal:buildvirtualvector buildvirtualvector Build virtual vector 7 gdal GDAL gdal:cliprasterbyextent cliprasterbyextent Clip raster by extent 8 gdal GDAL gdal:cliprasterbymasklayer cliprasterbymasklayer Clip raster by mask layer 9 gdal GDAL gdal:clipvectorbyextent clipvectorbyextent Clip vector by extent 10 gdal GDAL gdal:clipvectorbypolygon clipvectorbypolygon Clip vector by mask layer # … with 974 more rows # show help on a sample algorithm qgis_show_help("native:dissolve") Dissolve (native:dissolve) ---------------- Description ---------------- This algorithm takes a vector layer and combines their features into new features. One or more attributes can be specified to dissolve features belonging to the same class (having the same value for the specified attributes), alternatively all features can be dissolved in a single one. All output geometries will be converted to multi geometries. In case the input is a polygon layer, common boundaries of adjacent polygons being dissolved will get erased. ---------------- Arguments ---------------- INPUT: Input layer Argument type: source Acceptable values: - Path to a vector layer FIELD: Dissolve field(s) Argument type: field Acceptable values: - The name of an existing field - ; delimited list of existing field names OUTPUT: Dissolved Argument type: sink Acceptable values: - Path for new vector layer ---------------- Outputs ---------------- OUTPUT: <outputVector> Dissolved
If you’re running RStudio, the qgisprocess package should be available in the Packages tab.
Now let’s try to run a simple QGIS algorithm. Here, I’ll load a vector file of Bicol provinces and I’ll dissolve them (e.g. merge all features into a single feature) using the QGIS dissolve algorithm.
library(sf) # load input file input_file <- sf::read_sf("/home/bnhr/rdata/vectors/bicol-provinces.shp") # plot input file plot(sf::st_geometry(input_file)) # create temp output file output_file <- file.path(tempdir(), "bicol-dissolved.gpkg") # run QGIS dissolve algorithm qgis_run_algorithm( "native:dissolve", INPUT = input_file, FIELD = "", OUTPUT = output_file ) output_sf <- sf::read_sf(output_file) # plot output (dissolved features) plot(sf::st_geometry(output_sf))
And that’s how you can run QGIS algorithms in R. Please give the package a test and provide feedback here.
If you find my website or any of the materials I share useful, you can consider donating to the cause below.
Except when explicitly stated otherwise, this work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)