Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing qgis_run_algorithm methods #146

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ S3method(qgis_clean_argument,qgis_list_input)
S3method(qgis_clean_argument,qgis_tempfile_arg)
S3method(qgis_pipe,default)
S3method(qgis_pipe,qgis_result)
S3method(qgis_run_algorithm,character)
S3method(qgis_run_algorithm,default)
S3method(qgis_run_algorithm,qgis_result)
export(as_qgis_argument)
export(assert_qgis)
export(assert_qgis_algorithm)
Expand Down
4 changes: 3 additions & 1 deletion R/qgis-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ qgis_function <- function(algorithm, ...) {
default_run_alg_args[intersect(names(default_run_alg_args), names(qgis_fun_args))]

# generate the call to qgis_run_algorithm()

qgis_algorithm_call <- rlang::call2(
"qgis_run_algorithm",
algorithm,
algorithm = algorithm,
!!!qgis_algorithm_args,
.data = NULL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure, but probably .data = NULL should also be added to the list in line 93 above:

default_run_alg_args <- list(PROJECT_PATH = NULL, ELLIPSOID = NULL, .quiet = TRUE)

.ns = "qgisprocess"
)

Expand Down
66 changes: 64 additions & 2 deletions R/qgis-run-algorithm.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#' Run algorithms using 'qgis_process'
#'
#' @rdname qgis_run_algorithm
#' @export
qgis_run_algorithm <- function(.data = NULL,
algorithm, ..., PROJECT_PATH = NULL, ELLIPSOID = NULL,
.raw_json_input = NULL, .quiet = FALSE) {
Comment on lines +5 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you'll need to add .select = "OUTPUT" and .clean = TRUE as well. Not to make those arguments work, but for the documentation (only the generic will be shown in the documentation, the methods are hidden from documentation and pkg index).

UseMethod("qgis_run_algorithm", .data)
}


#' Run QGIS algorithms.
#' See the [QGIS docs](https://docs.qgis.org/testing/en/docs/user_manual/processing_algs/qgis/index.html)
#' for a detailed description of the algorithms provided
#' 'out of the box' on QGIS (versions >= 3.14).
#'
#' @param .data some data needed for piping
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' @param .data some data needed for piping
#' @param .data Optional.
#' Passed to the first input of `algorithm`.
#' If `.data` is a `qgis_result` (the result of a previous processing
#' step), `.data[[.select]]` is passed instead.
#' It can also be one of the qgis_output* classes
#' (e.g. `qgis_outputVector`, `qgis_outputRaster`),
#' a character, a numeric, a logical, a list or any other
#' possible algorithm input argument type.

The 'pipe-friendly' character of the function can be described in general. It is not necessary for this argument to be used in a piping context.

#' @param algorithm A qualified algorithm name (e.g., "native:filedownloader") or
#' a path to a QGIS model file.
#' @param PROJECT_PATH,ELLIPSOID Global values for QGIS project file and
Expand All @@ -26,7 +35,8 @@
#' )
#' }
#'
qgis_run_algorithm <- function(algorithm, ..., PROJECT_PATH = NULL, ELLIPSOID = NULL,
qgis_run_algorithm.default <- function(.data = NULL,
algorithm, ..., PROJECT_PATH = NULL, ELLIPSOID = NULL,
.raw_json_input = NULL, .quiet = FALSE) {
assert_qgis()
assert_qgis_algorithm(algorithm)
Expand Down Expand Up @@ -104,3 +114,55 @@ qgis_run_algorithm <- function(algorithm, ..., PROJECT_PATH = NULL, ELLIPSOID =
qgis_check_stdout(result)
result
}


#' @keywords internal
#' @export
qgis_run_algorithm.qgis_result <- function(
.data,
algorithm,
...,
PROJECT_PATH = NULL,
ELLIPSOID = NULL,
.raw_json_input = NULL,
.select = "OUTPUT",
.clean = TRUE,
.quiet = TRUE
) {
assert_that(is.string(.select))
withr::with_options(
list(warning.length = 6e3),
assert_that(
.select %in% names(.data),
msg = glue(
"The qgis_result object misses a '{.select}' element.\n",
"The included JSON-output was:\n",
"{jsonlite::prettify(.data$.processx_result$stdout)}"
)
)
)
output <- unclass(.data[[.select]])
fun <- qgis_function(algorithm)
result <- fun(output, ..., .quiet = .quiet)
if (.clean) qgis_result_clean(.data)
result
}

#' @keywords internal
#' @export
qgis_run_algorithm.character <- function(
.data = NULL,
algorithm,
...,
PROJECT_PATH = NULL,
ELLIPSOID = NULL,
.raw_json_input = NULL,
.clean = TRUE,
.quiet = TRUE
) {
browser()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be dropped?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing and your comments, @florisvdh. And no worries, I completely agree with you, the .data argument feels somehow wrong. And yes, qgisprocess does not lend itself naturally to piping, so in my opinion, if you have to pipe, use the native pipe and its placeholder to make crystal clear what you intend to do. So probably, the qgis_pipe() is a good compromise, so let's go with that and close this PR.

assert_that(is.string(.data))

fun <- qgis_function(algorithm)
fun(.data, ..., .quiet = .quiet)
}
3 changes: 0 additions & 3 deletions man/qgis_argument_spec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions man/qgis_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions man/qgis_has_algorithm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 2 additions & 29 deletions man/qgis_run_algorithm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions man/qgis_run_algorithm.default.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions man/qgis_sanitize_arguments.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions man/qgis_show_help.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.