Skip to content

Commit 5fc5416

Browse files
authored
Merge pull request #213 from r-spatial/vign
Add vignettes on algorithm arguments + options; update qgis_run_algorithm() docs
2 parents 159ecab + 0c1b533 commit 5fc5416

File tree

7 files changed

+227
-16
lines changed

7 files changed

+227
-16
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: qgisprocess
22
Title: Use 'QGIS' Processing Algorithms
3-
Version: 0.3.0.9002
3+
Version: 0.3.0.9003
44
Authors@R: c(
55
person("Dewey", "Dunnington", , "dewey@fishandwhistle.net", role = "aut",
66
comment = c(ORCID = "0000-0002-9415-4582", affiliation = "Voltron Data")),

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
This benefits package startup time and the timing of `qgis_run_algorithm()`, `qgis_show_help()`, and several other functions.
55
This feature needs QGIS >= 3.36.
66
- Don't renew the package cache when no plugins were successfully enabled or disabled (#212).
7+
- Add new vignettes (#213):
8+
- `vignette("qgis_arguments")`: passing R objects as algorithm arguments.
9+
- `vignette("qgis_options")`: configuration with options or environment variables.
710
- Update the 'getting started' vignette (#206, #207):
811
- advise to use `qgis_search_algorithms()`.
912
- in QGIS >= 3.36 the GRASS GIS provider is called `grass` instead of `grass7`.

R/qgis-run-algorithm.R

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' 'out of the box' on QGIS.
77
#'
88
#' `qgis_run_algorithm()` accepts various R objects as algorithm arguments.
9+
#' An overview is given by `vignette("qgis_arguments")`.
910
#' Examples include an R matrix or data frame for the
1011
#' argument type 'matrix', R colors for the argument type 'color',
1112
#' sf or terra (SpatVector) objects for the argument type 'vector' and
@@ -14,16 +15,20 @@
1415
#' `qgis_run_algorithm()` preprocesses the provided objects into the format that
1516
#' QGIS expects for a given argument.
1617
#'
17-
#' For data objects in R that already exist as a stored file, it is best to
18-
#' instead provide the file path in order to prevent a superfluous file writing
19-
#' step from R, as QGIS expects a file path.
20-
#' However terra and stars objects can contain the file path as metadata: in
21-
#' these cases this path is retrieved from the R object and passed to QGIS;
22-
#' potential pitfalls are taken care of.
23-
#'
2418
#' Providing R objects that cannot be converted to the applicable argument type
2519
#' will lead to an error.
2620
#'
21+
#' Algorithm arguments can be passed as arguments of [qgis_run_algorithm()], but
22+
#' they can also be combined as a JSON string and fed into the `.raw_json_input`
23+
#' argument.
24+
#' A JSON string can be obtained from the QGIS GUI, either from the
25+
#' processing tool dialog or from the processing history dialog, by selecting
26+
#' 'Copy as JSON' in the 'Advanced' dropdown menu.
27+
#' So a user can first try out a geoprocessing step in the QGIS GUI, and
28+
#' once the chosen algorithm arguments are satisfactory, copy the JSON string
29+
#' to reproduce the operation in R.
30+
#' A screenshot is available at the package homepage.
31+
#'
2732
#' @section Running QGIS models and Python scripts:
2833
#' QGIS models and Python scripts can be added to the Processing Toolbox in the
2934
#' QGIS GUI, by pointing at their corresponding file.
@@ -51,6 +56,8 @@
5156
#'
5257
#' @family functions to run one geoprocessing algorithm
5358
#'
59+
#' @seealso `vignette("qgis_arguments")`
60+
#'
5461
#' @param algorithm A qualified algorithm name (e.g., `"native:buffer"`) or
5562
#' a path to a QGIS model file.
5663
#' @param PROJECT_PATH,ELLIPSOID Global values for QGIS project file and
@@ -61,6 +68,7 @@
6168
#' @param .quiet Use `FALSE` to get extra output from 'qgis_process'.
6269
#' This can be useful in debugging.
6370
#' @param .raw_json_input The raw JSON to use as input in place of `...`.
71+
#' See _Details_ section.
6472
#'
6573
#' @returns A `qgis_result` object.
6674
#'

man/qgis_run_algorithm.Rd

Lines changed: 16 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
```{r setup, message=FALSE}
2+
library(qgisprocess)
3+
```
4+
5+
This article addresses the question how algorithm arguments in `qgis_run_algorithm()` should be formatted.
6+
7+
When you run `qgis_show_help()` or `qgis_get_argument_specs()` for a given algorithm, you will quickly find out that QGIS has a diverse set of possible argument types.
8+
9+
As an example, take a look at the `qgis_type` column below:
10+
11+
```{r}
12+
qgis_get_argument_specs("native:joinbynearest") |>
13+
subset(select = name:qgis_type)
14+
```
15+
16+
# String or R object?
17+
18+
Although you can pass a string [^string] to a QGIS argument in `qgis_run_algorithm()`, `{qgisprocess}` makes it possible to pass familiar R objects that naturally match the QGIS argument type.
19+
This is often easier than constructing a string in the format required by QGIS.
20+
21+
[^string]: A string is a character vector of length 1.
22+
23+
The tables further below show which R objects can be passed to each QGIS argument type!
24+
You can find a bit more background information in issue [#13](https://github.com/r-spatial/qgisprocess/issues/13).
25+
26+
# Defaults
27+
28+
Some arguments will receive a default value if they are unspecified.
29+
Defaults are provided by either `{qgisprocess}` or QGIS.
30+
Typical defaults by `{qgisprocess}` are temporary filepaths for (missing) output argument types `sink`, `vectorDestination`, `rasterDestination`, `fileDestination` and `folderDestination`.
31+
32+
# Supported R objects
33+
34+
## For spatial QGIS arguments
35+
36+
QGIS argument type | Supported R object
37+
-- | -----
38+
`source`, `vector` | a string (filepath) or an appropriate spatial vector object (`sf`, `SpatVector`, `SpatVectorProxy`)
39+
`raster` | a string (filepath) or an appropriate spatial raster object (`SpatRaster`, `stars`, `stars_proxy`, `RasterLayer`, `RasterBrick`)
40+
`layer` | a string (filepath) or an appropriate spatial object (`sf`, `SpatVector`, `SpatVectorProxy`, `SpatRaster`, `stars`, `stars_proxy`, `RasterLayer`, `RasterBrick`)
41+
`multilayer` | a list of layers created by `qgis_list_input()` (alternatively, repeat the same argument providing each layer in turn)
42+
`aggregates`, `field_mapping`, `tininputlayers`, `vectortilewriterlayers` | a nested list created by `qgis_list_input()` (unnamed list) or `qgis_dict_input()` (named list) or a combination of both (a `native:aggregate` example is found [here](https://github.com/r-spatial/qgisprocess/issues/133#issuecomment-1488490056)). These argument types are typically not supported by the legacy (no-JSON) input method.
43+
`point` | a vector of 2 point coordinates, a simple feature geometry (`sfg`) of class `POINT`, or an `sfc` (geometry set) or `sf` object with exactly one `POINT` geometry
44+
`band` | an integer value
45+
`extent` | a vector of the form `c(xmin, xmax, ymin, ymax)`, a `bbox` object from `{sf}`, a `SpatExtent` object from `{terra}` or an `Extent` object from `{raster}`
46+
`crs` | a `crs` object from `{sf}`, a `CRS` object from `{raster}` or a WKT2 string (e.g. obtained with `terra::crs()`)
47+
`coordinateoperation` | PROJ string of a coordinate operation, possibly obtained using `sf::sf_proj_pipelines()`
48+
`sink`, `vectorDestination` | a string: filepath to a vector file format (defaults to a temporary GeoPackage if argument is missing)
49+
`rasterDestination` | a string: filepath to a raster file format (defaults to a temporary GeoTIFF file if argument is missing)
50+
51+
### Note
52+
53+
An important group of spatial QGIS argument types are those used in specifying an input layer.
54+
QGIS essentially needs a filepath string here.
55+
If a spatial R object is provided instead, `{qgisprocess}` will:
56+
57+
- either write out the object to a temporary file and pass the filepath to QGIS,
58+
- or use the object's filepath metadata if present (supported by some classes of `{terra}` and `{stars}`).
59+
60+
However if the spatial R object simply results from reading a spatial file and if its filepath is _not_ included in the object's metadata (e.g. for `sf` or `SpatVector` objects), then you will get most efficiency if you pass the original filepath directly.
61+
62+
## For non-spatial QGIS arguments
63+
64+
QGIS argument types | Supported R object
65+
-- | -----
66+
`enum` | a character vector with one or more acceptable string values. A numeric vector with the corresponding index number(s) can also be provided, but contrary to the character vector its acceptability won't be checked.
67+
`range` | a vector of length 2, defining minimum and maximum value respectively
68+
`file`, `field`, `layout`, `layoutitem`, `maptheme`, `execute_sql` | string
69+
`string` | any string, including data-defined overriding (`"field:..."` or `"expression:..."`)
70+
`distance`, `number` | numeric (length 1), or a string for data-defined overriding (`"field:..."` or `"expression:..."`)
71+
`boolean` | logical (length 1), or a string for data-defined overriding (`"field:..."` or `"expression:..."`)
72+
`color` | a colour string that `col2rgb()` understands (e.g. `"pink1"` or `"#1A664D80"`), or a string for data-defined overriding (`"field:..."` or `"expression:..."`)
73+
`expression`, `raster_calc_expression` | a string (formatted as `"expression:..."`). See `vignette("qgis_expressions")`.
74+
`matrix` | a matrix or a data frame with contents as required by the algorithm
75+
`relief_colors` | a matrix or a data frame with three columns and with rows corresponding to intervals. The first two columns define the interval (minimum and maximum respectively) and the third column must be a colour string that `col2rgb()` understands (e.g. `"pink1"` or `"#1A664D80"`).
76+
`fileDestination`, `folderDestination` | a string: path to a file or directory (defaults to a temporary file or directory if argument is missing)
77+
78+
# Passing output from a previous processing step
79+
80+
The object returned by `qgis_run_algorithm()` is a `qgis_result`.
81+
It contains one or several _output elements_, shown when printing the `qgis_result` object.
82+
83+
A single output element can be extracted with `qgis_extract_output()`, and it has one of the following classes:
84+
`qgis_outputFile`,
85+
`qgis_outputFolder`,
86+
`qgis_outputLayer`,
87+
`qgis_outputMultilayer`,
88+
`qgis_outputNumber`,
89+
`qgis_outputRaster`,
90+
`qgis_outputString` or
91+
`qgis_outputVector`.
92+
93+
These objects essentially represent a string.
94+
They can be passed directly to an appropriate argument in a next `qgis_run_algorithm()` step.
95+
96+
Also note that the pipe-friendly function `qgis_run_algorithm_p()` (notice the `_p`) is able to accept an 'incoming' `qgis_result` object as its first argument.
97+
It will extract the appropriate output element on the fly.

vignettes/qgis_arguments.Rmd

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: "Passing R objects as algorithm arguments"
3+
author: "Floris Vanderhaeghe"
4+
date: |
5+
| Last update: 2024-06-28
6+
| Last run: `r Sys.Date()`
7+
output: rmarkdown::html_vignette
8+
vignette: >
9+
%\VignetteIndexEntry{Passing R objects as algorithm arguments}
10+
%\VignetteEngine{knitr::rmarkdown}
11+
%\VignetteEncoding{UTF-8}
12+
---
13+
14+
```{r, include = FALSE}
15+
knitr::opts_chunk$set(
16+
collapse = TRUE,
17+
comment = "#>"
18+
)
19+
can_build <- qgisprocess::has_qgis()
20+
```
21+
22+
```{r child=if (can_build) file.path(rprojroot::find_root(rprojroot::is_r_package), "man/vignette_childs/_qgis_arguments.Rmd")}
23+
```
24+
25+
```{r eval=!can_build, echo=FALSE, results="asis"}
26+
cat("This vignette has been built in absence of a QGIS installation.\n\n")
27+
cat("Read it online at <https://r-spatial.github.io/qgisprocess/articles/qgis_arguments.html>.")
28+
```

vignettes/qgis_options.Rmd

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: "Configuration with options or environment variables"
3+
author: "Floris Vanderhaeghe"
4+
date: |
5+
| Last update: 2024-06-28
6+
| Last run: `r Sys.Date()`
7+
output: rmarkdown::html_vignette
8+
vignette: >
9+
%\VignetteIndexEntry{Configuration with options or environment variables}
10+
%\VignetteEngine{knitr::rmarkdown}
11+
%\VignetteEncoding{UTF-8}
12+
---
13+
14+
```{r, include = FALSE}
15+
knitr::opts_chunk$set(
16+
collapse = TRUE,
17+
comment = "#>"
18+
)
19+
```
20+
21+
Options and environment variables are available to manage `{qgisprocess}` behaviour.
22+
Several options have a corresponding environment variable.
23+
When both have a value, the _option_ will be given priority.
24+
25+
Options are typically harder to isolate from the R code that you collaborate on.
26+
Consequently, it is advised to:
27+
28+
- use `options()` where this affects behaviour that must be the same across
29+
users and machines for reproducibility.
30+
Put these inside your script, or at least in a `.Rprofile` file that is
31+
shared together with the other project files.
32+
- use environment variables where behaviour must be machine-specific.
33+
For example, you can create a `.Renviron` file in your working directory
34+
and ignore it in distributed version control.
35+
Or you can set the environment variable at a higher level, e.g. in a
36+
`.Renviron` file in your home directory.
37+
See `base::Startup` for more information.
38+
39+
# Overview
40+
41+
Below table lists the available options and environment variables.
42+
43+
Option | Environment variable | Type
44+
-- | -- | --
45+
`qgisprocess.path` | `R_QGISPROCESS_PATH` | string (filepath)
46+
`qgisprocess.tmp_vector_ext` | not available | string (file extension)
47+
`qgisprocess.tmp_raster_ext` | not available | string (file extension)
48+
`qgisprocess.detect_newer_qgis` | `R_QGISPROCESS_DETECT_NEWER_QGIS` | logical
49+
`qgisprocess.use_json_input` | `R_QGISPROCESS_USE_JSON_INPUT` | logical
50+
`qgisprocess.use_json_output` | `R_QGISPROCESS_USE_JSON_OUTPUT` | logical
51+
`qgisprocess.cachefiles_days_keep` | `R_QGISPROCESS_CACHEFILES_DAYS_KEEP` | numeric
52+
`qgisprocess.use_cached_help` | `R_QGISPROCESS_USE_CACHED_HELP` | logical
53+
`qgisprocess.env` | not available | named list
54+
55+
# Descriptions
56+
57+
Following descriptions are applicable to both option and environment variable:
58+
59+
- `qgisprocess.path`: sets the path to 'qgis_process'; see `qgis_configure()`
60+
- `qgisprocess.tmp_vector_ext`: sets the vector file extension for temporary files; see `qgis_tmp_vector()`
61+
- `qgisprocess.tmp_raster_ext`: sets the raster file extension for temporary files; see `qgis_tmp_raster()`
62+
- `qgisprocess.detect_newer_qgis`: for Windows and macOS; will report availability of an installed (standalone) QGIS version that is more recent than the cached version
63+
- `qgisprocess.use_json_input`: control whether the JSON input method is used; see `qgis_using_json_input()`
64+
- `qgisprocess.use_json_output`: control whether the JSON output method is used; see `qgis_using_json_output()`
65+
- `qgisprocess.cachefiles_days_keep`: control a cache file's age (as days) that must be exceeded for it to be deleted when loading the package (default is 90); see `?qgis_delete_old_cachefiles`
66+
- `qgisprocess.use_cached_help`: whether to fetch an algorithm's documentation from a cached file (corresponding to the QGIS and `{qgisprocess}` versions in use) if this file exists (default is `TRUE`; if `FALSE`, always (re)query documentation from QGIS)
67+
- `qgisprocess.env`: named list of environment variable values used in each call to 'qgis_process'; see `qgis_run()`

0 commit comments

Comments
 (0)