/
qgis-configure.R
529 lines (460 loc) · 18.8 KB
/
qgis-configure.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#' Configure qgisprocess
#'
#' Run `qgis_configure()` to bring the package configuration in line with
#' QGIS and to save this configuration to a persistent cache.
#' See the _Details_ section for more information about setting the path of
#' the 'qgis_process' command line tool.
#'
#' The qgisprocess package is a wrapper around the 'qgis_process' command line
#' tool distributed with QGIS (>=3.14). Several functions use heuristics to
#' detect the location of the 'qgis_process' executable.
#'
#' When loading the package, the configuration is automatically read from the
#' cache with `qgis_configure(use_cached_data = TRUE, quiet = TRUE)` in order
#' to save time.
#' Run `qgis_configure(use_cached_data = TRUE)` manually to get more details.
#'
#' Use `qgis_algorithms()`, `qgis_providers()`, `qgis_plugins()`,
#' `qgis_using_json_output()`, `qgis_path()` and `qgis_version()` to inspect cache
#' contents.
#'
#' If the configuration
#' fails or you have more than one QGIS installation, you can set
#' `options(qgisprocess.path = "path/to/qgis_process")` or the
#' `R_QGISPROCESS_PATH` environment variable (useful on CI). On Linux the
#' 'qgis_process' executable is generally available on the user's PATH,
#' on MacOS the executable is within the QGIS*.app/Contents/MacOS/bin folder,
#' and on Windows the executable is named qgis_process-qgis.bat or
#' qgis_process-qgis-dev.bat and is located in Program Files/QGIS*/bin or
#' OSGeo4W(64)/bin.
#'
#' @family topics about configuring QGIS and qgisprocess
#' @concept functions to manage and explore QGIS and qgisprocess
#' @seealso [qgis_unconfigure()]
#' @seealso [qgis_path()], [qgis_version()]
#'
#' @param use_cached_data Use the cached algorithm list and `path` found when
#' configuring qgisprocess during the last session. This saves some time
#' loading the package.
#' @inheritParams qgis_run
#' @inheritParams qgis_path
#'
#' @returns The result of [processx::run()].
#'
#' @examples
#' \donttest{
#' # not running in R CMD check to save time
#' qgis_configure(use_cached_data = TRUE)
#' }
#'
#' \dontrun{
#' # package reconfiguration
#' # (not run in example() as it rewrites the package cache file)
#' qgis_configure()
#' }
#'
#' @export
qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) {
tryCatch(
{
qgis_unconfigure()
cache_data_file <- qgis_pkgcache_file()
# Practically all code of this function now consists of handling
# use_cached_data = TRUE. This includes cases where qgis_reconfigure() must
# be called, when a condition about the cache is not met. Note that
# qgis_configure() by default (use_cached_data = FALSE) calls
# qgis_reconfigure() straight away.
# below message is usually relevant when loading the package, and serves to
# announce some waiting time (checking and reading the cache) in case this
# process will happen silently (as is the case when loading the pkg). Put
# here, since packagestartupmessages in .onLoad are not good practice and
# trigger a note during R CMD check. Same applies to the Success! message
# further down.
# Note: more messages have been turned into startupmessages since CRAN
# wants to be able to suppress them.
if (use_cached_data && quiet) {
packageStartupMessage(
"Attempting to load the package cache ... ",
appendLF = FALSE
)
}
if (use_cached_data && file.exists(cache_data_file)) {
try({
cached_data <- readRDS(cache_data_file)
if (!quiet) message(glue("Checking configuration in cache file ({cache_data_file})"))
# CACHE CONDITION: contains minimum required elements (i.e. objects; the
# cache is an environment)
if (
!all(
c("path", "version", "algorithms", "plugins", "use_json_output")
%in% names(cached_data)
)
) {
if (quiet) packageStartupMessage()
packageStartupMessage(
"The cache does not contain all required elements.\n",
"Will try to reconfigure qgisprocess and build new cache ..."
)
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
# CACHE CONDITION: the path element does not contradict the environment
# variable/option for the qgis_process path
option_path <- getOption(
"qgisprocess.path",
Sys.getenv("R_QGISPROCESS_PATH")
)
if (!identical(option_path, "") && !identical(option_path, cached_data$path)) {
if (quiet) packageStartupMessage()
packageStartupMessage(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}).\n",
"Hence rebuilding cache to reflect this change ..."
))
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
# CACHE CONDITION: the elements checked by has_qgis() look OK
condition <-
is.string(cached_data$version) && nchar(cached_data$version) > 3L &&
is.string(cached_data$path) && nchar(cached_data$path) > 10L &&
inherits(cached_data$algorithms, "data.frame") &&
inherits(cached_data$plugins, "data.frame")
if (!condition) {
if (quiet) packageStartupMessage()
packageStartupMessage(
"The cache does not contain all required data.\n",
"Will try to reconfigure qgisprocess and build new cache ..."
)
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
# CACHE CONDITION: qgis_process is indeed available in the cached path
testopt <- getOption("qgisprocess.test_skip_path_availability_check")
outcome <- try(qgis_run(path = cached_data$path), silent = TRUE)
if (inherits(outcome, "try-error") && !isTRUE(testopt)) {
if (quiet) packageStartupMessage()
packageStartupMessage(
glue(
"'{cached_data$path}' (cached path) is not available anymore.\n",
"Will try to reconfigure qgisprocess and build new cache ..."
)
)
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
# CACHE CONDITION: the path element does not contradict the
# environment variable/option to automatically switch to a newer
# available QGIS version
if (is_windows() || is_macos()) {
opt <- resolve_flag_opt(
option_name = "qgisprocess.detect_newer_qgis",
envvar_name = "R_QGISPROCESS_DETECT_NEWER_QGIS"
)
first_qgis <- qgis_detect_paths()[1]
newer_available <- !is.na(extract_version_from_paths(first_qgis)) &&
!identical(cached_data$path, first_qgis)
if (opt && isTRUE(newer_available) && rlang::is_interactive()) {
packageStartupMessage()
packageStartupMessage(glue(
"A newer QGIS installation seems to be available: ",
"{extract_version_from_paths(first_qgis)}."
))
answer <- ""
while (!grepl("^[Yy](?:[Ee][Ss])?$|^[Nn](?:[Oo])?$", answer)) {
answer <- getOption("qgisprocess.test_try_new_qgis") %||%
readline("Do you want to try it and rebuild the cache? (y/n) ")
}
if (grepl("^[Yy]", answer)) {
newer_ok <- FALSE
tryCatch(
{
qgis_run(path = first_qgis)
newer_ok <- TRUE
},
error = function(e) {
packageStartupMessage(
glue(
"'{first_qgis}' does not work as expected.\n",
"So will not try it further."
)
)
}
)
if (newer_ok) {
packageStartupMessage(
"Will try to reconfigure qgisprocess and build new cache ..."
)
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
} else if (!quiet) {
packageStartupMessage(
"\nNOTE: if you don't want to autodetect QGIS version updates ",
"in the future, unset the qgisprocess.detect_newer_qgis ",
"option or the R_QGISPROCESS_DETECT_NEWER_QGIS environment ",
"variable."
)
}
}
}
# CACHE CONDITION: the cached QGIS version equals the one reported by
# qgis_process
if (!quiet) message(glue(
"Checking cached QGIS version with version reported by '{cached_data$path}' ..."
))
qgisprocess_cache$path <- cached_data$path
qversion <- qgis_query_version(quiet = quiet)
qgisprocess_cache$path <- NULL
if (!identical(qversion, cached_data$version)) {
if (quiet) packageStartupMessage()
packageStartupMessage(glue(
"QGIS version change detected:\n",
"- in the qgisprocess cache it was: {cached_data$version}\n",
"- while '{cached_data$path}' is at {qversion}\n",
"Hence rebuilding cache to reflect this change ..."
))
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
if (!quiet) message(glue("QGIS versions match! ({qversion})"))
# CACHE CONDITION: the use_json_output element does not contradict the environment
# variable/option for the output or input method (JSON vs legacy)
# Since we want library(qgisprocess) to take into account
# pre-existing settings for the JSON output, we must reconfigure if
# it is not consistent with qgis_using_json_output().
# There is good reason to cache 'use_json_output': the value of
# qgis_algorithms() and qgis_plugins() is different when populating
# it with or without the --json flag.
opt <- readopt_json_output()
qversion_short <- strsplit(qversion, "-")[[1]][1]
if (
( ## conflict with explicit json_output AND json_input setting?
!identical(opt, "") &&
# resolving conflicts with explicit JSON INput setting:
!identical(
resolve_explicit_json_output(
json_output_setting = opt,
qgis_version = qversion_short
),
cached_data$use_json_output
)) ||
( ## conflict with explicit json_INput set as TRUE?
identical(opt, "") &&
json_input_set_and_acceptable(qversion_short) &&
isFALSE(cached_data$use_json_output)
)
) {
if (quiet) packageStartupMessage()
packageStartupMessage(glue(
"The outcome of user settings for using JSON input/output ",
"contradict the 'use_json_output' cache value ",
"({cached_data$use_json_output}).\n",
"Hence rebuilding cache to reflect this change ..."
))
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
# CACHE CONDITION: the cached QGIS plugins equal the ones reported by
# qgis_process, including their state
if (!quiet) message(glue(
"Checking cached QGIS plugins (and state) with those reported by '{cached_data$path}' ..."
))
qgisprocess_cache$path <- cached_data$path
qgisprocess_cache$use_json_output <- cached_data$use_json_output
qgisprocess_cache$version <- cached_data$version
qplugins <- qgis_query_plugins(quiet = quiet)
qgisprocess_cache$path <- NULL
qgisprocess_cache$use_json_output <- NULL
qgisprocess_cache$version <- NULL
if (!identical(qplugins, cached_data$plugins)) {
if (quiet) packageStartupMessage()
packageStartupMessage(
"Change detected in (enabled) QGIS processing provider plugins!\n",
"- in the qgisprocess cache it was:"
)
print(cached_data$plugins)
packageStartupMessage(glue(
"- while '{cached_data$path}' currently returns:"
))
print(qplugins)
packageStartupMessage(
"Hence rebuilding cache to reflect this change ..."
)
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))
}
if (!quiet) {
message(glue(
"QGIS plugins match! ({ sum(qplugins$enabled) } ",
"processing provider plugin(s) enabled)"
))
message_disabled_plugins(qplugins, prepend_newline = TRUE)
}
# ASSIGNING CACHE OBJECTS
if (!quiet) message(glue(
"\n\nRestoring 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$plugins <- cached_data$plugins
qgisprocess_cache$loaded_from <- cache_data_file
# FINAL HANDLING OF SUCCESSFUL use_cached_data = TRUE
if (!quiet) { # triggers messages
invisible(qgis_version(query = FALSE, quiet = FALSE))
invisible(qgis_path(query = FALSE, quiet = FALSE))
invisible(qgis_using_json_output(query = FALSE, quiet = FALSE))
message(
ifelse(qgis_using_json_input(), "Using ", "Not using "),
"JSON for input serialization."
)
invisible(qgis_plugins(query = FALSE, quiet = FALSE, msg = FALSE))
invisible(qgis_algorithms(query = FALSE, quiet = FALSE))
message_inspect_cache()
}
# usually applicable only on loading the package:
if (quiet && !is.null(qgisprocess_cache$loaded_from)) {
(packageStartupMessage("Success!"))
}
return(invisible(has_qgis()))
})
}
if (use_cached_data && !file.exists(cache_data_file)) {
packageStartupMessage(
"No cache found.\n",
"Will try to reconfigure qgisprocess and build new cache ..."
)
}
# use_cached_data = FALSE or cache is missing:
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
},
error = function(e) {
qgis_unconfigure()
if (!quiet) message(e)
}
)
invisible(has_qgis())
}
#' @keywords internal
qgis_reconfigure <- function(cache_data_file, quiet = FALSE) {
path <- qgis_path(query = TRUE, quiet = quiet)
if (!quiet) message()
tryCatch(
{
version <- qgis_version(query = TRUE, quiet = quiet)
},
error = function(e) {
message(glue(
"\n\nATTENTION: the QGIS version could not be queried. ",
"You will loose some functionality.\n",
"You may want to run `qgis_version(query = TRUE)` followed by ",
"`qgis_configure()`; see its documentation.\n",
"Error message was:\n\n{e}\n",
ifelse("stderr" %in% names(e) && nchar(e$stderr) > 0, e$stderr, "")
))
}
)
use_json_output <- qgis_using_json_output(query = TRUE, quiet = quiet)
if (!quiet) message(
ifelse(qgis_using_json_input(), "Using ", "Not using "),
"JSON for input serialization."
)
tryCatch(
{
plugins <- qgis_plugins(query = TRUE, quiet = quiet, msg = FALSE)
},
error = function(e) {
message(glue(
"\n\nATTENTION: the QGIS plugins could not be queried. ",
"You will loose some functionality.\n",
"You may want to (re)run `qgis_configure()`; see its documentation.\n",
"Error message was:\n\n{e}\n",
ifelse("stderr" %in% names(e) && nchar(e$stderr) > 0, e$stderr, "")
))
}
)
tryCatch(
{
algorithms <- qgis_algorithms(query = TRUE, quiet = quiet)
},
error = function(e) {
message(glue(
"\n\nATTENTION: the QGIS algorithms could not be queried. ",
"You will loose some functionality.\n",
"You may want to (re)run `qgis_configure()`; see its documentation.\n",
"Error message was:\n\n{e}\n",
ifelse("stderr" %in% names(e) && nchar(e$stderr) > 0, e$stderr, "")
))
}
)
if (!quiet && exists("plugins")) message_disabled_plugins(plugins, prepend_newline = TRUE)
if (has_qgis()) {
if (!quiet) message(glue("\n\nSaving configuration to '{cache_data_file}'"))
try({
if (!dir.exists(dirname(cache_data_file))) {
dir.create(dirname(cache_data_file), recursive = TRUE)
}
saveRDS(
list(
path = path,
version = version,
algorithms = algorithms,
plugins = plugins,
use_json_output = use_json_output
),
cache_data_file
)
})
if (!quiet) message_inspect_cache()
} else if (!quiet) {
message(config_problem_msg)
}
}
#' @keywords internal
message_inspect_cache <- function() {
message(
"Use qgis_algorithms(), qgis_providers(), qgis_plugins(), ",
"qgis_path() and\nqgis_version() ",
"to inspect the cache environment."
)
}
#' @keywords internal
qgis_env <- function() {
getOption(
"qgisprocess.env",
list(QT_QPA_PLATFORM = "offscreen")
)
}
#' Clean the package cache
#'
#' Empties the qgisprocess cache environment.
#'
#' @family topics about programming or debugging utilities
#'
#' @returns `NULL`, invisibly.
#'
#' @examples
#' \dontrun{
#' # not running this function in example() as it clears the cache environment.
#' qgis_unconfigure()
#' }
#'
#' # undoing qgis_unconfigure() by repopulating the cache environment from file:
#' \donttest{
#' # not running in R CMD check to save time
#' qgis_configure(use_cached_data = TRUE)
#' }
#'
#' @export
qgis_unconfigure <- function() {
qgisprocess_cache$path <- NULL
qgisprocess_cache$version <- NULL
qgisprocess_cache$algorithms <- NULL
qgisprocess_cache$plugins <- NULL
qgisprocess_cache$loaded_from <- NULL
qgisprocess_cache$use_json_output <- NULL
invisible(NULL)
}