From 3f7824370816045216f245135a70287bf184367e Mon Sep 17 00:00:00 2001 From: florisvdh Date: Thu, 18 Aug 2022 18:24:05 +0200 Subject: [PATCH 1/9] qgis_configure(): compare QGIS version before using cache --- R/qgis-configure.R | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/R/qgis-configure.R b/R/qgis-configure.R index fd768f27..f75e4916 100644 --- a/R/qgis-configure.R +++ b/R/qgis-configure.R @@ -90,22 +90,41 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) { if (use_cached_data && file.exists(cache_data_file)) { try({ cached_data <- readRDS(cache_data_file) - if (!quiet) message(glue("Restoring configuration from '{cache_data_file}'")) + + if (!quiet) message(glue("Checking configuration from '{cache_data_file}'")) # respect environment variable/option for path option_path <- getOption( "qgisprocess.path", Sys.getenv("R_QGISPROCESS_PATH") ) - if (identical(option_path, "") || identical(option_path, cached_data$path)) { + + if (!quiet) message(glue( + "Checking cached QGIS version with version reported by '{cached_data$path}' ..." + )) + + # note the difference with the further qgis_version() statement, + # where it will also respect the outcome of qgis_path(query = TRUE); + # below it uses qgis_path(query = FALSE), hence takes cached_data$path qgisprocess_cache$path <- cached_data$path - qgisprocess_cache$version <- cached_data$version - qgisprocess_cache$use_json_output <- cached_data$use_json_output - qgisprocess_cache$algorithms <- cached_data$algorithms - qgisprocess_cache$loaded_from <- cache_data_file + qversion <- qgis_version(query = TRUE, quiet = quiet) + qgisprocess_cache$path <- NULL + + if (identical(qversion, cached_data$version)) { + if (!quiet) message(glue("QGIS versions match! ({qversion})")) + if (!quiet) message(glue("Restoring configuration from '{cache_data_file}'")) + + qgisprocess_cache$path <- cached_data$path + qgisprocess_cache$version <- cached_data$version + qgisprocess_cache$use_json_output <- cached_data$use_json_output + qgisprocess_cache$algorithms <- cached_data$algorithms + qgisprocess_cache$loaded_from <- cache_data_file + + return(invisible(TRUE)) + + } - return(invisible(TRUE)) } }) } From 799d251ea7fc436352867a85e3c1ddc2ae8e8611 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Thu, 18 Aug 2022 18:26:30 +0200 Subject: [PATCH 2/9] qgis_configure(quiet = FALSE): show similar final messages with or without cache usage --- R/qgis-configure.R | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/R/qgis-configure.R b/R/qgis-configure.R index f75e4916..d2022dc4 100644 --- a/R/qgis-configure.R +++ b/R/qgis-configure.R @@ -121,6 +121,21 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) { qgisprocess_cache$algorithms <- cached_data$algorithms qgisprocess_cache$loaded_from <- cache_data_file + if (!quiet) { + message( + glue::glue( + "Metadata of { nrow(cached_data$algorithms) } algorithms are present in cache.\n", + "Run `qgis_algorithms()` to see them." + ) + ) + if (qgis_use_json_input()) { + message("- Using JSON for input serialization.") + } + if (qgis_use_json_output()) { + message("- Using JSON for output serialization.") + } + } + return(invisible(TRUE)) } @@ -164,11 +179,11 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) { ) if (qgis_use_json_input()) { - message("- Using JSON for input serialization") + message("- Using JSON for input serialization.") } if (qgis_use_json_output()) { - message("- Using JSON for output serialization") + message("- Using JSON for output serialization.") } } }, error = function(e) { From a350a48743271485b373e36062d0809d17d6685e Mon Sep 17 00:00:00 2001 From: florisvdh Date: Thu, 18 Aug 2022 18:30:13 +0200 Subject: [PATCH 3/9] qgis_configure() with attempt to use cache: always report changes & cache rebuild if so --- R/qgis-configure.R | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/R/qgis-configure.R b/R/qgis-configure.R index d2022dc4..0d687bff 100644 --- a/R/qgis-configure.R +++ b/R/qgis-configure.R @@ -138,9 +138,21 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) { return(invisible(TRUE)) + } else { + message(glue( + "QGIS version change detected:\n", + "- in the qgisprocess cache it was: {cached_data$version}\n", + "- while '{cached_data$path}' is at {qversion}" + )) } - + } else { + message(glue( + "The user's qgisprocess.path option or the R_QGISPROCESS_PATH environment ", + "variable specify a different qgis_process path ({option_path}) ", + "than the cache did ({cached_data$path}).")) } + + message("Hence rebuilding cache to reflect this change.") }) } From a1382dc6e5ef55adf2a4fdd0253a11bca39d3819 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Thu, 18 Aug 2022 18:32:33 +0200 Subject: [PATCH 4/9] Startup message: don't advise to run qgis_configure() for details * Instead, make a distinction between cache usage and no cache usage which function to use for more information. But do point at qgis_configure() when the user needs another installed version of QGIS. --- R/zzz.R | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/R/zzz.R b/R/zzz.R index 60ae0218..9f233555 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -26,11 +26,18 @@ "Using 'qgis_process' at '{ qgis_path() }'.\n", "QGIS version: { qgis_version() }\n", if (is.null(qgisprocess_cache$loaded_from)) { - "Metadata of { nrow(qgis_algorithms()) } algorithms successfully cached\n" + paste0( + "Metadata of { nrow(qgis_algorithms()) } algorithms successfully cached.\n", + "Run `qgis_algorithms()` to see them.\n" + ) } else { - "Configuration loaded from '{ qgisprocess_cache$loaded_from }'\n" + paste0( + "Configuration loaded from '{ qgisprocess_cache$loaded_from }'\n", + "Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.\n" + ) }, - "Run `qgis_configure()` for details.\n", + ">>> If you need another installed QGIS version, run `qgis_configure()`; see ", + "its documentation if you need to preset the path of qgis_process.\n", if (qgis_use_json_input()) "Using JSON for input serialization\n" else "", if (qgis_use_json_output()) "Using JSON for output serialization\n" else "", .sep = "" From f27d906978b2723e38c957466ddcaa3eae0da22d Mon Sep 17 00:00:00 2001 From: florisvdh Date: Fri, 19 Aug 2022 14:21:41 +0200 Subject: [PATCH 5/9] Startup message: bring layout of final lines in accordance with qgis_configure(quiet=FALSE) --- R/zzz.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/zzz.R b/R/zzz.R index 9f233555..e7795e3a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -38,8 +38,8 @@ }, ">>> If you need another installed QGIS version, run `qgis_configure()`; see ", "its documentation if you need to preset the path of qgis_process.\n", - if (qgis_use_json_input()) "Using JSON for input serialization\n" else "", - if (qgis_use_json_output()) "Using JSON for output serialization\n" else "", + if (qgis_use_json_input()) "- Using JSON for input serialization.\n" else "", + if (qgis_use_json_output()) "- Using JSON for output serialization.\n" else "", .sep = "" ) ) From 24b84a20054cccbbb52e98ac25a5e9547492fdb5 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Fri, 19 Aug 2022 16:24:48 +0200 Subject: [PATCH 6/9] qgis_configure(): check program availability before doing cache-version check --- R/qgis-configure.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/R/qgis-configure.R b/R/qgis-configure.R index f40bc5e0..c072484e 100644 --- a/R/qgis-configure.R +++ b/R/qgis-configure.R @@ -98,12 +98,26 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) { "qgisprocess.path", Sys.getenv("R_QGISPROCESS_PATH") ) + if (identical(option_path, "") || identical(option_path, cached_data$path)) { if (!quiet) message(glue( "Checking cached QGIS version with version reported by '{cached_data$path}' ..." )) + # since we will query the program, first check that it still works + + tryCatch({ + qgis_run(path = cached_data$path) + }, error = function(e) { + abort( + glue( + "'{cached_data$path}' (cached path) is not available anymore.\n", + "Will try to reconfigure qgisprocess and build new cache ..." + ) + ) + }) + # note the difference with the further qgis_version() statement, # where it will also respect the outcome of qgis_path(query = TRUE); # below it uses qgis_path(query = FALSE), hence takes cached_data$path From f24e90a9cbbd5c5e906abcdf4e82b6ceebc4a81e Mon Sep 17 00:00:00 2001 From: florisvdh Date: Fri, 19 Aug 2022 16:25:30 +0200 Subject: [PATCH 7/9] More elaborate startup message in case qgis_process was not found --- R/zzz.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/zzz.R b/R/zzz.R index 6b5781df..a6a170af 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -45,7 +45,9 @@ ) } else { packageStartupMessage( - "The 'qgis_process' command-line utility was not found.\nRun `qgis_configure()` for details." + "The 'qgis_process' command-line utility was not found.\n", + "Please run `qgis_configure()` to fix this and rebuild the cache.\n", + "See its documentation if you need to preset the path of qgis_process." ) } } From 170dfbde0140ff63081c7ee3c6dc24fc12bd2bd1 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Fri, 19 Aug 2022 19:44:13 +0200 Subject: [PATCH 8/9] Use different startup message when qgis_process is called from PATH * Before, the message was: "Using 'qgis_process' at 'qgis_process'." Which is a bit odd. --- R/zzz.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/zzz.R b/R/zzz.R index a6a170af..eeeadfe0 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -21,9 +21,16 @@ .onAttach <- function(...) { if (has_qgis()) { + path <- qgis_path() + pathstring <- + if (path == "qgis_process") { + "in the system PATH" + } else { + glue("at '{path}'") + } packageStartupMessage( glue( - "Using 'qgis_process' at '{ qgis_path() }'.\n", + "Using 'qgis_process' {pathstring}.\n", "QGIS version: { qgis_version() }\n", if (is.null(qgisprocess_cache$loaded_from)) { paste0( From 99ae27e56e541e334f85a23321454b1fc617d9fb Mon Sep 17 00:00:00 2001 From: florisvdh Date: Fri, 19 Aug 2022 19:49:57 +0200 Subject: [PATCH 9/9] Improve line wrapping of startup message --- R/zzz.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/zzz.R b/R/zzz.R index eeeadfe0..0fb6d1cf 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -43,8 +43,8 @@ "Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.\n" ) }, - ">>> If you need another installed QGIS version, run `qgis_configure()`; see ", - "its documentation if you need to preset the path of qgis_process.\n", + ">>> If you need another installed QGIS version, run `qgis_configure()`;\n", + " see its documentation if you need to preset the path of qgis_process.\n", if (qgis_use_json_input()) "- Using JSON for input serialization.\n" else "", if (qgis_use_json_output()) "- Using JSON for output serialization.\n" else "", .sep = ""