Skip to content

Commit

Permalink
Merge pull request #209 from r-spatial/skip_loading_plugins
Browse files Browse the repository at this point in the history
Speed up by using '--skip-loading-plugins' in specific 'qgis_process' calls
  • Loading branch information
florisvdh authored May 20, 2024
2 parents 229062d + 3bce232 commit af218df
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: qgisprocess
Title: Use 'QGIS' Processing Algorithms
Version: 0.3.0.9000
Version: 0.3.0.9001
Authors@R: c(
person("Dewey", "Dunnington", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-9415-4582", affiliation = "Voltron Data")),
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# qgisprocess (development version)

- Speed up various calls to the `qgis_process` backend by automatically implementing `--skip-loading-plugins` where possible (#201).
This benefits package startup time and the timing of `qgis_run_algorithm()`, `qgis_show_help()`, and several other functions.
This feature needs QGIS >= 3.36.
- Update the 'getting started' vignette (#206, #207):
- advise using `qgis_search_algorithms()`.
- in QGIS >= 3.36 the GRASS GIS provider is called `grass` instead of `grass7`.

# qgisprocess 0.3.0

## Enhancements
Expand Down
8 changes: 8 additions & 0 deletions R/qgis-algorithms.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ check_algorithm_deprecation <- function(algorithm, skip = FALSE) {
}



#' @keywords internal
algorithm_is_native <- function(algorithm) {
stringr::str_match(algorithm, "^(\\w+):.*")[, 2] %in%
c("native", "3d", "pdal")
}


#' @keywords internal
qgis_query_algorithms <- function(quiet = FALSE) {
if (qgis_using_json_output()) {
Expand Down
9 changes: 7 additions & 2 deletions R/qgis-help.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ qgis_help_json <- function(algorithm, check_deprecation = TRUE) {
assert_qgis_algorithm(algorithm, check_deprecation = check_deprecation)

result <- qgis_run(
args = c("--json", "help", algorithm),
args = c(
"--json",
arg_skip_loading_plugins(algorithm),
"help",
algorithm
),
encoding = "UTF-8"
)

Expand All @@ -127,7 +132,7 @@ qgis_help_text <- function(algorithm, check_deprecation = TRUE) {
assert_qgis_algorithm(algorithm, check_deprecation = check_deprecation)

result <- qgis_run(
args = c("help", algorithm)
args = c(arg_skip_loading_plugins(algorithm), "help", algorithm)
)

try({
Expand Down
27 changes: 22 additions & 5 deletions R/qgis-plugins.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ qgis_plugins <- function(

#' @keywords internal
qgis_query_plugins <- function(quiet = FALSE) {
arg_skip_loading <- arg_skip_loading_plugins()
if (qgis_using_json_output()) {
result <- qgis_run(args = c("plugins", "--json"))
result <- qgis_run(args = c("plugins", "--json", arg_skip_loading))
if (nchar(result$stderr) > 0L) {
message(
"\nStandard error message from 'qgis_process':\n",
Expand All @@ -112,7 +113,7 @@ qgis_query_plugins <- function(quiet = FALSE) {
plugins$value <- unlist(plugins$value, use.names = FALSE)
colnames(plugins) <- c("name", "enabled")
} else {
result <- qgis_run("plugins")
result <- qgis_run(args = c("plugins", arg_skip_loading))
if (nchar(result$stderr) > 0L) {
message(
"\nStandard error message from 'qgis_process':\n",
Expand All @@ -135,6 +136,18 @@ qgis_query_plugins <- function(quiet = FALSE) {



#' @keywords internal
arg_skip_loading_plugins <- function(algorithm = NULL) {
if (!is.null(algorithm) && !algorithm_is_native(algorithm)) {
return(NULL)
}
if (package_version(qgis_version(full = FALSE)) >= "3.36.0") {
"--skip-loading-plugins"
} else NULL
}




#' @keywords internal
message_disabled_plugins <- function(
Expand Down Expand Up @@ -172,7 +185,9 @@ handle_plugins <- function(names = NULL, quiet = FALSE, mode) {

if (is.null(names)) {
names <- qgis_plugins(which = moded_rev)$name
names <- names[names != "processing"]
if (mode == "disable") {
names <- names[names != "processing"]
}
} else {
assert_that(is.character(names))
names_old <- names
Expand All @@ -187,11 +202,13 @@ handle_plugins <- function(names = NULL, quiet = FALSE, mode) {
"{paste(names_skip, collapse = ', ')}"
))
names <- names_old[names_old %in% qgis_plugins(which = moded_rev)$name]
if (!quiet && "processing" %in% names) message(
if (!quiet && "processing" %in% names && mode == "disable") message(
"Ignoring the 'processing' plugin, because it is always available to ",
"'qgis_process' (not QGIS though)."
)
names <- names[names != "processing"]
if (mode == "disable") {
names <- names[names != "processing"]
}
}

if (length(names) == 0L) {
Expand Down
1 change: 1 addition & 0 deletions R/qgis-run-algorithm.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ qgis_run_algorithm <- function(algorithm, ..., PROJECT_PATH = NULL, ELLIPSOID =
result <- qgis_run(
args = c(
if (use_json_output) "--json",
arg_skip_loading_plugins(algorithm),
"run",
algorithm,
if (use_json_input) "-" else args_str
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-qgis-algorithms.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ test_that("Internal function check_algorithm_deprecation() works", {
expect_no_warning(check_algorithm_deprecation(alg_non_deprecated))
})

test_that("Internal function algorithm_is_native() works", {
expect_true(algorithm_is_native("native:algorithm"))
expect_true(algorithm_is_native("3d:algorithm"))
expect_true(algorithm_is_native("pdal:algorithm"))
expect_false(algorithm_is_native("qgis:algorithm"))
expect_false(algorithm_is_native("grass:algorithm"))
})

test_that("qgis_search_algorithms() works", {
skip_if_not(has_qgis())
expect_error(qgis_search_algorithms(), "at least one of the arguments")
Expand Down
55 changes: 46 additions & 9 deletions tests/testthat/test-qgis-plugins.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ test_that("qgis_plugins(which = \"all\", query = FALSE) returns sensible things"
skip_if_not(has_qgis())
plugins <- qgis_plugins(which = "all", query = FALSE)
expect_correct_plugins_format(plugins)
expect_gte(sum(plugins$enabled), 1)
})

test_that("qgis_plugins(which = \"enabled\", query = FALSE) returns sensible things", {
skip_if_not(has_qgis())
plugins <- qgis_plugins(which = "enabled", query = FALSE)
expect_correct_plugins_format(plugins)
expect_gte(sum(plugins$enabled), 1)
})

test_that("qgis_plugins(which = \"disabled\", query = FALSE) returns sensible things", {
skip_if_not(has_qgis())
plugins <- qgis_plugins(which = "disabled", query = FALSE)
expect_correct_plugins_format(plugins)
expect_gte(sum(!plugins$enabled), 0)
})

test_that("qgis_plugins(query = FALSE, quiet = FALSE) messages are OK", {
Expand All @@ -41,7 +38,6 @@ test_that(glue("qgis_plugins(query = TRUE) works when using JSON output"), {

plugins <- qgis_plugins(query = TRUE)
expect_correct_plugins_format(plugins)
expect_gte(sum(plugins$enabled), 1)
})

test_that(glue("qgis_plugins(query = TRUE) works when NOT using JSON output"), {
Expand All @@ -53,7 +49,6 @@ test_that(glue("qgis_plugins(query = TRUE) works when NOT using JSON output"), {

plugins <- qgis_plugins(query = TRUE)
expect_correct_plugins_format(plugins)
expect_gte(sum(plugins$enabled), 1)
})


Expand All @@ -71,10 +66,6 @@ test_that("message_disabled_plugins() works", {
test_that("qgis_enable_plugins() messages are OK", {
skip_if_not(has_qgis())
expect_message(qgis_enable_plugins(names = ""), "exiting")
expect_message(
qgis_enable_plugins(names = "processing"),
"Ignoring.+processing"
)
expect_message(
qgis_enable_plugins(names = "notaplugin"),
"Ignoring unknown plugins: notaplugin"
Expand All @@ -93,6 +84,19 @@ test_that("qgis_disable_plugins() messages are OK", {
qgis_disable_plugins(names = "notaplugin"),
"Ignoring unknown plugins: notaplugin"
)

# checking that a bare qgis_disable_plugins() never disables processing
local_mocked_bindings(
qgis_plugins = function(...) {
tibble::tibble(
name = c("plugin_1", "processing"),
enabled = TRUE
)
},
qgis_run = function(...) NULL,
qgis_configure = function(...) NULL
)
expect_no_message(qgis_disable_plugins(), message = "\\bprocessing\\b")
})


Expand Down Expand Up @@ -179,3 +183,36 @@ test_that("qgis_*able_plugins() works for an enabled grassprovider plugin", {
)
expect_true(subset(qgis_plugins(), name == "grassprovider")$enabled)
})



test_that("{en,dis}able_plugin can return an error message from qgis_process", {
local_mocked_bindings(qgis_run = function(...) stop("Some error"))
expect_message(enable_plugin("p1"), "not successfully enabled.+Some error")
expect_message(disable_plugin("p1"), "not successfully disabled.+Some error")
})



test_that("Internal function arg_skip_loading_plugins() works", {
expect_null(arg_skip_loading_plugins("grass:algorithm"))

local_mocked_bindings(
qgis_version = function(...) "3.34.0" # --skip-loading-plugins not supported
)

expect_null(arg_skip_loading_plugins("grass:algorithm"))
expect_null(arg_skip_loading_plugins("native:algorithm"))
expect_null(arg_skip_loading_plugins())

local_mocked_bindings(
qgis_version = function(...) "3.36.0" # --skip-loading-plugins supported
)

expect_null(arg_skip_loading_plugins("grass:algorithm"))
expect_identical(
arg_skip_loading_plugins("native:algorithm"),
"--skip-loading-plugins"
)
expect_identical(arg_skip_loading_plugins(), "--skip-loading-plugins")
})

0 comments on commit af218df

Please sign in to comment.