Run QGIS processing algorithms in R

04 Oct 2020 ᛫ 5 min read

SHARE:

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.

Installing the qgisprocess R package

We can install the qgisprocess package from its GitHub repo using the remotes package. To do so, type:

install.packages("remotes")
remotes::install_github("paleolimbot/qgisprocess")
Install QGIS qgisprocess R package
Install the qgisprocess R package

Checking if the package was installed

We can then check if the qgisprocess package was installed by calling some functions.

library(qgisprocess)
qgis_path()
[1] "qgis_process"

# check QGIS version
qgis_version()
[1] "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

Checking if the qgisprocess package is installed in R
Checking if the qgisprocess package is installed in R

If you’re running RStudio, the qgisprocess package should be available in the Packages tab.

Running a simple QGIS algorithms

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))
Loading a vector file of Bicol provinces in R
Loading a vector file of Bicol provinces in R
Dissolving the Bicol provinces vector using the QGIS dissolve function in R
Dissolving the Bicol provinces vector using the QGIS dissolve function in R

And that’s how you can run QGIS algorithms in R. Please give the package a test and provide feedback here.

Like and follow BNHR on Facebook and Twitter for more #FOSS4G and #QGIS stuff. :)

SHARE:
comments powered by Disqus

You may also like:

Towards a spatial analysis of shooting in Philippine basketball (FOSS4G2021)

01 Oct 2021 ᛫ 1 min read

The opposite of free/libre and open source isn't commercial, it's proprietary

27 May 2021 ᛫ 2 min read

Towards a Spatial Analysis of Philippine Basketball: Applications in the UAAP MBT (Season 81) [Part 1]

02 May 2021 ᛫ 4 min read

Win and let win: On being unconventional, openness, and building communities

30 Apr 2021 ᛫ 1 min read

QGIS Styles based on HLURB Land Use Categories and Color Coding (CLUP Guidebook Vol 3, 2014)

15 Dec 2020 ᛫ 2 min read

Support BNHR

If you find my website or any of the materials I share useful, you can consider donating to the cause below.

Donate and support BNHR

BNHR

[email protected]

Creative Commons License
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)