Skip to content

Commit 280eb90

Browse files
committed
Merge branch 'main' into cran_submission_0.2.0
2 parents b041583 + 27be51f commit 280eb90

File tree

9 files changed

+148
-34
lines changed

9 files changed

+148
-34
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
^pkgdown$
2525
^qgisprocess\.Rproj$
2626
^CRAN-SUBMISSION$
27+
^revdep$

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export(qgis_configure)
7171
export(qgis_description)
7272
export(qgis_detect_macos)
7373
export(qgis_detect_macos_paths)
74+
export(qgis_detect_paths)
7475
export(qgis_detect_windows)
7576
export(qgis_detect_windows_paths)
7677
export(qgis_dict_input)

NEWS.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
## New features
44

5-
- Add vector support for {terra} (#184).
5+
- Add vector support for `{terra}` (#184).
66
This makes it possible to use `SpatVector` or `SpatVectorProxy` objects as input arguments, and to coerce processing results to `SpatVector` or `SpatVectorProxy`.
77
- `qgis_detect_windows_paths()` and `qgis_detect_macos_paths()` put paths at the top that contain a QGIS version string and these paths are sorted according to decreasing QGIS version (#189).
8-
This lets `qgis_configure()` prefer the newest QGIS version from `qgis_process` file paths that have a version string.
8+
This lets `qgis_configure()` select the newest QGIS version among `qgis_process` file paths that have a version string.
9+
Furthermore, a wrapper `qgis_detect_paths()` has been added that works on both Windows and macOS (#192).
910

10-
## Other changes
11+
## Minor changes
1112

1213
- Allow half-configured states with abundant messages, so that remaining functionality can be used in debugging or even for some real stuff (#177).
13-
- `qgis_run_algorithm()` documentation gains a section on QGIS models and scripts (8a20669).
14-
- Solve a CRAN check error on `r-oldrel-macos-x86_64`, by adding support for {stars} 0.5-5 (#175).
14+
- `qgis_run_algorithm()` documentation gains a section on QGIS models and scripts ([8a20669](https://github.com/r-spatial/qgisprocess/commit/8a20669ea50b4b9c14194dd864ed119e137732a9)).
15+
- An option `qgisprocess.detect_newer_qgis` is available (mirrored by environment variable `R_QGISPROCESS_DETECT_NEWER_QGIS`) for Windows and macOS (#192).
16+
If set as `TRUE`, during package loading `{qgisprocess}` will check whether a more recent (standalone) QGIS version is also installed while the package cache still dictates to use an older version.
17+
In this specific scenario a question will be asked to switch to the newer version.
18+
Without setting this option default behaviour remains in place, i.e. the user must manually intervene by setting the `qgisprocess.path` option, or by uninstalling the older QGIS version.
19+
- Solve a CRAN check error on `r-oldrel-macos-x86_64`, by adding support for `{stars}` 0.5-5 (#175).
1520

1621
# qgisprocess 0.1.0
1722

R/qgis-configure.R

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,82 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) {
145145

146146
# CACHE CONDITION: qgis_process is indeed available in the cached path
147147

148-
tryCatch(
149-
{
150-
qgis_run(path = cached_data$path)
151-
},
152-
error = function(e) {
153-
if (quiet) message()
154-
abort(
155-
glue(
156-
"'{cached_data$path}' (cached path) is not available anymore.\n",
157-
"Will try to reconfigure qgisprocess and build new cache ..."
158-
)
148+
outcome <- try(qgis_run(path = cached_data$path), silent = TRUE)
149+
if (inherits(outcome, "try-error")) {
150+
if (quiet) packageStartupMessage()
151+
packageStartupMessage(
152+
glue(
153+
"'{cached_data$path}' (cached path) is not available anymore.\n",
154+
"Will try to reconfigure qgisprocess and build new cache ..."
159155
)
160-
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
161-
return(invisible(has_qgis()))
156+
)
157+
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
158+
return(invisible(has_qgis()))
159+
}
160+
161+
# CACHE CONDITION: the path element does not contradict the
162+
# environment variable/option to automatically switch to a newer
163+
# available QGIS version
164+
if (is_windows() || is_macos()) {
165+
opt <- getOption(
166+
"qgisprocess.detect_newer_qgis",
167+
Sys.getenv("R_QGISPROCESS_DETECT_NEWER_QGIS")
168+
)
169+
assert_that(
170+
assertthat::is.flag(opt) ||
171+
(assertthat::is.string(opt) && opt %in% c("", "TRUE", "FALSE", "true", "false")),
172+
msg = "Option 'qgisprocess.detect_newer_qgis' must be 'TRUE' or 'FALSE'."
173+
)
174+
if (identical(opt, "")) opt <- NA
175+
opt || grepl("TRUE|true", opt)
176+
177+
first_qgis <- qgis_detect_paths()[1]
178+
newer_available <- !is.na(extract_version_from_paths(first_qgis)) &&
179+
!identical(cached_data$path, first_qgis)
180+
181+
if (isTRUE(opt) && isTRUE(newer_available) && interactive()) {
182+
packageStartupMessage()
183+
packageStartupMessage(glue(
184+
"A newer QGIS installation seems to be available: ",
185+
"{extract_version_from_paths(first_qgis)}."
186+
))
187+
answer <- ""
188+
while (!grepl("^[Yy](?:[Ee][Ss])?$|^[Nn](?:[Oo])?$", answer)) {
189+
answer <- readline("Do you want to try it and rebuild the cache? (y/n) ")
190+
}
191+
if (grepl("^[Yy]", answer)) {
192+
newer_ok <- FALSE
193+
tryCatch(
194+
{
195+
qgis_run(path = first_qgis)
196+
newer_ok <- TRUE
197+
},
198+
error = function(e) {
199+
packageStartupMessage(
200+
glue(
201+
"'{first_qgis}' does not work as expected.\n",
202+
"So will not try it further."
203+
)
204+
)
205+
}
206+
)
207+
if (newer_ok) {
208+
packageStartupMessage(
209+
"Will try to reconfigure qgisprocess and build new cache ..."
210+
)
211+
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
212+
return(invisible(has_qgis()))
213+
}
214+
} else if (!quiet) {
215+
packageStartupMessage(
216+
"\nNOTE: if you don't want to autodetect QGIS version updates ",
217+
"in the future, unset the qgisprocess.detect_newer_qgis ",
218+
"option or the R_QGISPROCESS_DETECT_NEWER_QGIS environment ",
219+
"variable."
220+
)
221+
}
162222
}
163-
)
223+
}
164224

165225
# CACHE CONDITION: the cached QGIS version equals the one reported by
166226
# qgis_process

R/qgis-detect.R

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
#' Detect QGIS installations that provide the 'qgis_process' command
1+
#' Detect QGIS installations with 'qgis_process' on Windows and macOS
22
#'
33
#' Discovers existing 'qgis_process' executables on the system and returns their
44
#' filepath.
55
#' Only available for Windows and macOS systems.
6+
#' `qgis_detect_paths()` is a shortcut to `qgis_detect_windows_paths()` on
7+
#' Windows and `qgis_detect_macos_paths()` on macOS.
68
#'
79
#' @concept functions to manage and explore QGIS and qgisprocess
810
#'
@@ -16,15 +18,33 @@
1618
#'
1719
#' @param drive_letter The drive letter on which to search. By default,
1820
#' this is the same drive letter as the R executable.
21+
#' Only applicable to Windows.
1922
#'
2023
#' @returns A character vector of possible paths to the 'qgis_process'
2124
#' executable.
2225
#' @export
2326
#'
2427
#' @examples
25-
#' if (.Platform$OS.type == "windows") qgis_detect_windows_paths()
26-
#' if (Sys.info()["sysname"] == "Darwin") qgis_detect_macos_paths()
27-
#'
28+
#' if (.Platform$OS.type == "windows") {
29+
#' qgis_detect_paths()
30+
#' identical(qgis_detect_windows_paths(), qgis_detect_paths())
31+
#' }
32+
#' if (Sys.info()["sysname"] == "Darwin") {
33+
#' qgis_detect_paths()
34+
#' identical(qgis_detect_macos_paths(), qgis_detect_paths())
35+
#' }
36+
qgis_detect_paths <- function(drive_letter = strsplit(R.home(), ":")[[1]][1]) {
37+
if (is_windows()) {
38+
qgis_detect_windows_paths(drive_letter = drive_letter)
39+
} else if (is_macos()) {
40+
qgis_detect_macos_paths()
41+
} else {
42+
abort("Can use `qgis_detect_paths()` on Windows and macOS only.")
43+
}
44+
}
45+
46+
#' @rdname qgis_detect_paths
47+
#' @export
2848
qgis_detect_windows_paths <- function(drive_letter = strsplit(R.home(), ":")[[1]][1]) {
2949
if (!is_windows()) {
3050
abort("Can't use `qgis_detect_windows_paths()` on a non-windows platform.")
@@ -50,7 +70,7 @@ qgis_detect_windows_paths <- function(drive_letter = strsplit(R.home(), ":")[[1]
5070
sort_paths(possible_locs_win)
5171
}
5272

53-
#' @rdname qgis_detect_windows_paths
73+
#' @rdname qgis_detect_paths
5474
#' @export
5575
qgis_detect_macos_paths <- function() {
5676
if (!is_macos()) {

man/qgis_detect_windows_paths.Rd renamed to man/qgis_detect_paths.Rd

Lines changed: 17 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

revdep/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
checks
2+
library
3+
checks.noindex
4+
library.noindex
5+
cloud.noindex
6+
data.sqlite
7+
*.html

tests/testthat/test-qgis-configure.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,10 @@ test_that("qgis_configure() works OK if cache conditions unmet", {
131131
),
132132
cache_data_file
133133
)
134-
msg <- paste(capture.output(
134+
expect_message(
135135
qgis_configure(use_cached_data = TRUE),
136-
type = "message"
137-
), collapse = "\n")
138-
expect_true(stringr::str_detect(msg, "is not available anymore"))
136+
"is not available anymore"
137+
)
139138

140139
saveRDS(
141140
list(

tests/testthat/test-qgis-detect.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ test_that("qgis_detect_windows_paths() works", {
1414
}
1515
})
1616

17+
test_that("qgis_detect_paths() works", {
18+
if (is_windows()) {
19+
expect_identical(qgis_detect_paths(), qgis_detect_windows_paths())
20+
} else if (is_macos()) {
21+
expect_identical(qgis_detect_paths(), qgis_detect_macos_paths())
22+
} else {
23+
expect_error(qgis_detect_paths(), "only")
24+
}
25+
})
26+
1727
test_that("extract_version_from_paths() works", {
1828
expect_identical(extract_version_from_paths(character()), character())
1929
path <- "/QGIS 3.28.6/bin/qgis_process-qgis-ltr.bat"

0 commit comments

Comments
 (0)