|
| 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. |
0 commit comments