-
Notifications
You must be signed in to change notification settings - Fork 30
/
wbt.R
1254 lines (1113 loc) · 44.7 KB
/
wbt.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Initialize 'WhiteboxTools'
#'
#' `wbt_init()`: Check if a suitable 'WhiteboxTools' executable is present. Search default path in package directory or set it manually with `exe_path`.
#'
#' @param exe_path Default `exe_path` is result of `wbt_exe_path()` which checks a few user-settable options before defaulting to the package installation directory sub-directory "WBT". May be overridden if a custom path is needed.
#' @param ... additional arguments to `wbt_options()`
#' @param check_version Check version of 'WhiteboxTools' installed against version R package was built for? Default: `TRUE`
#'
#' @return `wbt_init()`: logical; `TRUE` if binary file is found at `exe_path`
#' @export
#' @keywords General
#' @seealso [install_whitebox()] [whitebox]
#' @examples
#' \dontrun{
#' ## wbt_init():
#'
#' # set path to binary as an argument
#' # wbt_init(exe_path = "not/a/valid/path/whitebox_tools.exe")
#' }
wbt_init <- function(exe_path = wbt_exe_path(shell_quote = FALSE),
...,
check_version = TRUE) {
# optional parameters specified by ...
initargs <- list(...)
wd <- initargs[["wd"]]
verbose <- initargs[["verbose"]]
compress_rasters <- initargs[["compress_rasters"]]
if (!is.character(exe_path) || length(exe_path) != 1) {
stop("exe_path must be a character vector with length 1", call. = FALSE)
}
exe_path <- path.expand(exe_path)
# if exe_path is not NULL and file exists, and value differs from the wbt_exe_path() result
if ((!is.null(exe_path) &&
file.exists(exe_path) &&
exe_path != wbt_exe_path(shell_quote = FALSE)) ||
!is.null(wd) ||
!is.null(verbose) ||
!is.null(compress_rasters)) {
if (!is.null(wd) && length(wd) > 0 && (is.na(wd) || wd == "")) {
.wbt_wd_unset()
}
# set the path with wbt_options
wbt_options(exe_path = exe_path, ...)
}
# check whether path exists (using new options)
res1 <- check_whitebox_binary()
# success? binary found matches path the user (may have) set, return TRUE
new_exe_path <- wbt_exe_path(shell_quote = FALSE)
res2 <- new_exe_path == exe_path
# if the wbt_exe_path() output doesnt match user input
if (!res2) {
if (wbt_verbose()) {
message("WhiteboxTools Executable Path (whitebox.exe_path) reverted to:\n\t", new_exe_path)
}
}
if (check_version) {
# check version info, provide ONE message per session if mismatched
exv <- try(wbt_version(extract = TRUE), silent = TRUE)
if (is.na(exv)) {
exv <- "<NA>"
}
pkv <- try(attr(whitebox::wbttools, 'version'), silent = TRUE)
if (is.na(pkv)) {
pkv <- "<NA>"
}
if (!inherits(exv, 'try-error') && !inherits(pkv, 'try-error')) {
if (isTRUE(exv != pkv)) {
warned <- try(get("whitebox.warned_version_difference",
envir = whitebox::whitebox.env),
silent = TRUE)
if (inherits(warned, 'try-error')) {
warned <- FALSE
}
if (wbt_verbose() && isFALSE(warned)) {
message("NOTE: Installed WhiteboxTools version (", exv,
") is ", ifelse(exv > pkv, "newer", "older"),
" than the package data (", pkv, ").")
try(assign("whitebox.warned_version_difference",
value = TRUE,
envir = whitebox::whitebox.env),
silent = TRUE)
}
}
}
}
return(invisible(res1 && res2))
}
#' @description `wbt_options()`: Get/set package options
#'
#' - **`whitebox.exe_path`** - character. Path to executable file. The default value is the package installation directory, subdirectory `"WBT"`, followed by `whitebox_tools.exe` or `whitebox_tools`. Set the `whitebox.exe_path` option using `wbt_init()` `exe_path` argument
#'
#' - **`whitebox.wd`** - character. Path to WhiteboxTools working directory. Used as `--wd` argument for tools that support it when `wd` is not specified elsewhere.
#'
#' - **`whitebox.verbose`** - logical. Should standard output from calls to executable be `cat()` out for readability? When `whitebox.verbose=FALSE` no output is produced. Set the value of `whitebox.verbose` with `wbt_verbose()` `verbose` argument. Default is result of `interactive()` if R package options are unset.
#'
#' - **`whitebox.compress_rasters`** - logical. Should raster output from WhiteboxTools be compressed? Default: `NULL` uses existing WhiteboxTools settings. Set the value of `whitebox.compress_rasters` with `wbt_compress_rasters()` `compress_rasters` argument.
#'
#' - **`whitebox.max_procs`** - integer. Maximum number of processes for tools that run in parallel or partially parallelize. Default: `-1` uses all of the available cores.
#'
#' @return `wbt_options()`: an invisible list containing current `whitebox.exe_path`, `whitebox.verbose`, `whitebox.compress_rasters`, and `whitebox.max_procs` options
#' @rdname wbt_init
#' @export
#' @keywords General
#' @examples
#' \dontrun{
#'
#' ## wbt_options():
#'
#' # set multiple options (e.g. exe_path and verbose) with wbt_options()
#' wbt_options(exe_path = "not/a/valid/path/whitebox_tools.exe", verbose = TRUE)
#'
#' }
wbt_options <- function(exe_path = NULL,
wd = NULL,
verbose = NULL,
compress_rasters = NULL,
max_procs = NULL) {
# get the system value
syswbt <- Sys.getenv("R_WHITEBOX_EXE_PATH")
syswd <- Sys.getenv("R_WHITEBOX_WD")
sysvrb <- Sys.getenv("R_WHITEBOX_VERBOSE")
syscpr <- Sys.getenv("R_WHITEBOX_COMPRESS_RASTERS")
sysmxp <- Sys.getenv("R_WHITEBOX_MAX_PROCS")
# check user input, set package options
if (!is.null(exe_path)) {
if (file.exists(exe_path)) exe_path <- path.expand(exe_path)
options(whitebox.exe_path = exe_path)
}
if (!is.null(wd)) {
# preserve attributes if any on wd
if (dir.exists(wd)) wd[1] <- path.expand(wd)
options(whitebox.wd = wd)
}
if (!is.null(verbose)) {
options(whitebox.verbose = verbose)
}
if (!is.null(compress_rasters)) {
options(whitebox.compress_rasters = compress_rasters)
}
if (!is.null(max_procs)) {
options(whitebox.max_procs = max_procs)
}
invisible(list(
whitebox.exe_path = ifelse(nchar(syswbt) == 0,
getOption("whitebox.exe_path",
default = wbt_exe_path(shell_quote = FALSE)),
syswbt),
whitebox.wd = ifelse(nchar(syswd) == 0,
getOption("whitebox.wd",
default = ""),
syswd),
whitebox.verbose = ifelse(nchar(sysvrb) == 0,
getOption("whitebox.verbose",
default = ""),
sysvrb),
whitebox.compress_rasters = ifelse(nchar(syscpr) == 0,
as.logical(getOption("whitebox.compress_rasters",
default = NA)),
syscpr),
whitebox.max_proc = ifelse(nchar(sysmxp) == 0,
as.integer(getOption("whitebox.max_proc",
default = -1)),
sysmxp)
))
}
#' @description `wbt_exe_path()`: Get the file path of the 'WhiteboxTools' executable.
#'
#' @details `wbt_exe_path()`: Checks system environment variable `R_WHITEBOX_EXE_PATH` and package option `whitebox.exe_path`. Set your desired path with either `Sys.setenv(R_WHITEBOX_EXE_PATH = "C:/path/to/whitebox_tools.exe")` or `options(whitebox.exe_path = "C:/path/to/whitebox_tools.exe")`. The default, backwards-compatible path is returned by `wbt_default_path()`
#'
#' @param exe_path Optional: User-supplied path to 'WhiteboxTools' executable. Default: `NULL`
#' @param shell_quote Return `shQuote()` result?
#'
#' @return Returns the file path of 'WhiteboxTools' executable.
#' @export
#' @keywords General
#' @rdname wbt_init
#' @examples
#' \dontrun{
#' wbt_exe_path()
#' }
wbt_exe_path <- function(exe_path = NULL, shell_quote = TRUE) {
syswbt <- Sys.getenv("R_WHITEBOX_EXE_PATH")
pkgwbt <- getOption("whitebox.exe_path")
defwbt <- wbt_default_path()
pthwbt <- Sys.which(basename(defwbt))
if (!is.null(exe_path) && file.exists(exe_path)) {
# user specified as argument
res <- exe_path
} else if (!is.null(syswbt) && file.exists(syswbt)) {
# user specified as system option
res <- syswbt
} else if (!is.null(pkgwbt) && file.exists(pkgwbt)) {
# user specified as package option
res <- pkgwbt
} else if (!is.na(pthwbt) && pthwbt != "" && file.exists(pthwbt)) {
# whitebox_tools is on PATH
res <- pthwbt
} else {
res <- defwbt
}
if (shell_quote) {
return(shQuote(res))
}
res
}
#' @description `wbt_runner_path()`: Get the file path of the 'WhiteboxTools Runner' executable.
#'
#' @details `wbt_runner_path()`: Returns a path to 'WhiteboxTools Runner' including a platform-specific executable (with or without .exe extension)
#' @export
#' @keywords General
#' @rdname wbt_init
wbt_runner_path <- function(shell_quote = TRUE) {
bn <- "whitebox_runner"
os <- Sys.info()["sysname"]
if (os == "Windows") {
bn <- "whitebox_runner.exe"
}
res <- file.path(dirname(wbt_exe_path(shell_quote = FALSE)), bn)
if (shell_quote) {
return(shQuote(res))
}
res
}
#' @title Launch 'WhiteboxTools Runner' GUI
#' @description `wbt_launch_runner()`: Launch the 'WhiteboxTools Runner' GUI at `wbt_runner_path()`
#' @param clear_app_state Clear application state memory? Default: `FALSE`
#' @details Opens the 'WhiteboxTools Runner' GUI included with WhiteboxTools Open Core v2.3.0 or higher.
#' @seealso [wbt_runner_path()]
#' @export
#' @keywords General
#' @rdname wbt_launch_runner
wbt_launch_runner <- function(clear_app_state = FALSE) {
system(paste0(wbt_runner_path(), ifelse(clear_app_state, "--clear_app_state", "")))
}
#' @description `wbt_default_path()`: Get the default file path of the 'WhiteboxTools' executable.
#'
#' @details `wbt_default_path()`: Returns a path to 'WhiteboxTools' executable including a platform-specific executable (with or without .exe extension)
#' @export
#' @keywords General
#' @rdname wbt_init
wbt_default_path <- function() {
exe <- "whitebox_tools"
# system specific executable filename
os <- Sys.info()["sysname"]
if (os == "Windows") {
exe <- "whitebox_tools.exe"
}
file.path(wbt_data_dir(), "WBT", exe)
}
#' @description `wbt_data_dir()`: Get the directory where 'WhiteboxTools' data are stored.
#'
#' @details `wbt_data_dir()`: Uses platform-specific user data directory from `tools::R_user_dir(package = "whitebox", which = "data")` on R 4.0+. On R<4 returns the original default `find.package("whitebox")`.
#' @export
#' @keywords General
#' @rdname wbt_init
wbt_data_dir <- function() {
if (R.version$major >= 4) {
tools::R_user_dir(package = "whitebox", which = "data")
} else {
# backwards compatible path
find.package("whitebox")
}
}
#' @description `wbt_wd()`: Get or set the 'WhiteboxTools' working directory. Default: `""` (unset) is your R working directory if no other options are set.
#'
#' @param wd character; Default: `NULL`; if set the package option `whitebox.wd` is set specified path (if directory exists)
#'
#' @return `wbt_wd()`: character; when working directory is unset, will not add `--wd=` arguments to calls and should be the same as using `getwd()`. See Details.
#'
#' @details `wbt_wd()`: Before you set the working directory in a session the default output will be in your current R working directory unless otherwise specified. You can change working directory at any time by setting the `wd` argument to `wbt_wd()` and running a tool. Note that once you have set a working directory, the directory needs to be set somewhere to "replace" the old value; just dropping the flag will not change the working directory back to the R working directory. To "unset" the option in the R package you can use `wbt_wd("")` which removes the `--wd` flag from commands and sets the `working_directory` value in the WhiteboxTools _settings.json_ to `""`.
#' @rdname wbt_init
#' @export
#' @keywords General
#' @examples
#' \dontrun{
#'
#' ## wbt_wd():
#'
#' # no working directory set
#' wbt_wd(wd = "")
#'
#' # set WBT working directory to R working directory
#' wbt_wd(wd = getwd())
#' }
wbt_wd <- function(wd = NULL) {
if (length(wd) > 0 && (is.na(wd) || wd == "")) {
.wbt_wd_unset()
}
if (is.character(wd)) {
# if character input, set the package option "wd"
wbt_options(wd = wd)
}
# system environment var takes precedence
syswd <- Sys.getenv("R_WHITEBOX_WD")
if (nchar(syswd) > 0 && dir.exists(syswd)) {
return(syswd)
}
# package option checked next; if missing default is getwd() (unspecified should be same as getwd())
res <- getOption("whitebox.wd")
# an empty string silently stays an empty string
if (is.null(res) || nchar(res) == 0) {
res <- ""
# otherwise, if the option is invalid directory message
} else if (!is.null(res) && !dir.exists(res)) {
message("Invalid path for `whitebox.wd`: directory does not exist.\nDefaulting to \"\"")
res <- ""
}
invisible(res)
}
.wbt_wd_unset <- function() {
# this doesnt actually set the value ""
# - try(wbt_system_call(paste0("--wd=", shQuote(""))), silent = TRUE)
try({
f <- file.path(dirname(wbt_exe_path(shell_quote = FALSE)), "settings.json")
x <- readLines(f, warn = FALSE)
x[grepl('^ *"working_directory": .*$', x)] <- ' "working_directory": "",'
writeLines(x, f)
})
}
#' @description `wbt_verbose()`: Check verbose options for 'WhiteboxTools'
#'
#' @param verbose logical. Default: `NULL`; if `TRUE` or `FALSE`, set the package option `whitebox.verbose` to specified value. Tool verbosity settings can be overridden in any `wbt_*()` function call by passing the `verbose_mode` argument directly.
#'
#' @return `wbt_verbose()`: logical; returns the result of option `"whitebox.verbose_mode"`, if unset defaults to result of `interactive()`.
#' @rdname wbt_init
#' @export
#' @keywords General
#' @examples
#' \dontrun{
#'
#' ## wbt_verbose():
#'
#' wbt_verbose(verbose = TRUE)
#' }
wbt_verbose <- function(verbose = NULL) {
# NA is treated NULL (no effect)
if (length(verbose) != 1 || is.na(verbose)) {
verbose <- NULL
}
# system environment var takes precedence
sysverbose <- Sys.getenv("R_WHITEBOX_VERBOSE", unset = "")
if (sysverbose == "all") {
# wbt_verbose always return logical, "all" is a flavor of true
return(TRUE)
} else if (sysverbose != "") {
# take up to first 5 characters, uppercase eval/parse/convert to logical
# this should also allow for 0/1 to be specified and converted as needed to logical
sysverbose <- as.logical(eval(parse(text = toupper(substr(sysverbose, 0, 5)))))
}
# if logical system env var, use that
if (is.logical(sysverbose) && !is.na(sysverbose)) {
return(sysverbose)
}
# if logical input, set the package option "verbose"
if (is.logical(verbose) || (!is.null(verbose) && verbose == "all")) {
wbt_options(verbose = verbose)
}
# package option subsequently, default true for interactive use
res <- as.logical(getOption("whitebox.verbose", default = interactive()))
if (is.na(res) || (!is.null(verbose) && verbose == "all")) {
res <- TRUE
} else if (is.na(res) || !is.logical(res)) {
message('Invalid value for whitebox.verbose, defaulting to interactive()')
res <- interactive()
}
invisible(res)
}
#' @description `wbt_compress_rasters()`: Check raster compression option for 'WhiteboxTools'. Default behavior is based on WhiteboxTools settings.json, package options (if set). Raster compression settings can be overridden in any `wbt_*()` function call by passing the `compress_rasters` argument directly.
#'
#' @param compress_rasters logical. Default: `NULL`; if `TRUE` or `FALSE`, set the package option `whitebox.compress_rasters` to specified value.
#'
#' @return `wbt_compress_rasters()`: logical; returns the result of option `"whitebox.compress_rasters"`, if unset defaults to `NA`.
#' @rdname wbt_init
#' @export
#' @keywords General
#' @examples
#' \dontrun{
#'
#' ## wbt_compress_rasters():
#'
#' wbt_compress_rasters(compress_rasters = TRUE)
#' }
wbt_compress_rasters <- function(compress_rasters = NULL) {
# NA is treated NULL (no effect)
if (length(compress_rasters) != 1 || is.na(compress_rasters)) {
compress_rasters <- NULL
}
# system environment var takes precedence
syscompress <- Sys.getenv("R_WHITEBOX_COMPRESS_RASTERS", unset = NA)
if (!is.na(syscompress)) {
# take up to first 5 characters, uppercase eval/parse/convert to logical
# this should also allow for 0/1 to be specified and converted as needed to logical
syscompress <- as.logical(eval(parse(
text = toupper(substr(syscompress, 0, 5))
)))
}
# if logical system env var, use that
if (is.logical(syscompress) && !is.na(syscompress)) {
return(syscompress)
}
# if logical input, set the package option "compress_rasters"
if (is.logical(compress_rasters)) {
wbt_options(compress_rasters = compress_rasters)
}
# package option subsequently, default FALSE
res <- as.logical(getOption("whitebox.compress_rasters", default = NA))
if (is.na(res) || !is.logical(res)) {
res <- NA
}
invisible(res)
}
#' @description `wbt_max_procs()`: Check maximum number of processes for for tools that run in parallel or partially parallelize. Default: `-1` uses all of the available cores.
#'
#' @param max_procs Default: `NULL`; if integer, set the package option `whitebox.max_procs` to specified value
#'
#' @return `wbt_max_procs()`: integer; defaults to `NA_integer_`
#' @rdname wbt_init
#' @export
#' @keywords General
#' @examples
#' \dontrun{
#'
#' ## wbt_max_procs():
#'
#' wbt_max_procs(max_procs = 2)
#' }
wbt_max_procs <- function(max_procs = NULL) {
# NA is treated NULL (no effect)
if (length(max_procs) != 1 || is.na(max_procs)) {
max_procs <- NULL
}
# system environment var takes precedence
sysmax_procs <- Sys.getenv("R_WHITEBOX_MAX_PROCS", unset = NA)
if (!is.na(sysmax_procs)) {
sysmax_procs <- as.integer(sysmax_procs)
}
# if integer system env var, use that
if (is.integer(sysmax_procs) && !is.na(sysmax_procs)) {
return(sysmax_procs)
}
# if integer input, set the package option "max_procs"
if (is.numeric(max_procs)) {
wbt_options(max_procs = as.integer(max_procs))
}
# package option subsequently, default FALSE
res <- as.integer(getOption("whitebox.max_procs", default = NA_integer_))
if (!is.integer(res)) {
res <- NA_integer_
}
invisible(res)
}
#' @export
#' @keywords General
#' @rdname install_whitebox
wbt_install <- function(pkg_dir = wbt_data_dir(), platform = NULL, force = FALSE, remove = FALSE) {
stopifnot(is.logical(force))
stopifnot(is.logical(remove))
stopifnot(length(pkg_dir) == 1)
stopifnot(is.character(pkg_dir))
pkg_dir <- normalizePath(pkg_dir, "/", FALSE)
if (!is.na(remove) && remove) {
unlink(list.files(file.path(pkg_dir, "WBT"), recursive = TRUE, full.names = TRUE), force = force, recursive = TRUE)
}
# Check for binary file in 'WBT' directory
exe_path <- wbt_default_path()
os <- Sys.info()["sysname"]
.unsupported <- function(){
message("Sorry, whitebox download from https://www.whiteboxgeo.com/download-whiteboxtools/ is unsupported for your operating system!\n")
message("Pre-built binaries are available only for 64-bit Windows, Mac OS Intel and Linux (compiled w/ Ubuntu 20.04).")
message("See: https://www.whiteboxgeo.com/download-whiteboxtools/ \n")
message("You can follow the instructions at https://github.com/jblindsay/whitebox-tools to use cargo to build the Rust library from source.\n")
message(paste0("If you have WhiteboxTools installed already, run `wbt_init(exe_path=...)`': \n",
"\twbt_init(exe_path='/home/user/path/to/whitebox_tools')\n"))
}
# if not in package directory, and user has not specified a path
if (!file.exists(exe_path) || pkg_dir != wbt_default_path() || force) {
# install_whitebox/wbt_install is 64-bit only
if (.Machine$sizeof.pointer != 8) {
return(invisible(.unsupported()))
}
if (missing(platform) || is.null(platform)) {
if (os == "Linux") {
url <- "https://www.whiteboxgeo.com/WBT_Linux/WhiteboxTools_linux_amd64.zip"
} else if (os == "Windows") {
url <- "https://www.whiteboxgeo.com/WBT_Windows/WhiteboxTools_win_amd64.zip"
} else if (os == "Darwin") {
suffix <- "amd64"
if (Sys.info()["machine"] == "arm64") {
suffix <- "m_series"
}
url <- paste0("https://www.whiteboxgeo.com/WBT_Darwin/WhiteboxTools_darwin_", suffix , ".zip")
} else {
return(invisible(.unsupported()))
}
} else {
# supports alternative platforms/filenames
# e.g. linux_musl, darwin_m_series
url <- paste0("https://www.whiteboxgeo.com/WBT_",
os, "/WhiteboxTools_",
platform, ".zip")
}
filename <- basename(url)
cat("Performing one-time download of WhiteboxTools binary from\n")
cat("\t", url, "\n")
cat("(This could take a few minutes, please be patient...)\n")
# path for downloaded zip file;
# remove downloaded zip file when exiting function
exe_zip <- file.path(pkg_dir, filename)
on.exit(unlink(exe_zip), add = TRUE)
if (!dir.exists(pkg_dir)) {
dir.create(pkg_dir, recursive = TRUE)
}
# this fails on some platforms and with certain URLs
# tried curl::curl_download, httr::GET, and other download.file method options for libcurl
# logic from xfun::download_file used for tinytex::install_tinytex()
if (getOption("timeout") == 60L) {
opts = options(timeout = 3600)
on.exit(options(opts), add = TRUE)
}
res <- -1
for (method in c(if (os == "Windows") "internal", "libcurl", "auto")) {
if (!inherits(try({
res <- utils::download.file(url, exe_zip, method = method)
}, silent = TRUE), "try-error") && res == 0)
break
}
if (res != 0) {
message("Unable to download by any method! Try downloading ZIP manually from https://www.whiteboxgeo.com/download-whiteboxtools/. Installation involves just extracting to your desired directory. Set path to binary with wbt_init(exe_path = '/path/to/whitebox_tools')")
return(invisible(NULL))
}
# unzip to either whitebox package or user specified folder
utils::unzip(exe_zip, exdir = pkg_dir)
ex_dir <- file.path(pkg_dir, gsub("\\.zip$", "", basename(exe_zip)))
file.copy(file.path(ex_dir, "WBT"), pkg_dir, recursive = TRUE)
file.remove(list.files(ex_dir, recursive = TRUE, full.names = TRUE))
# subfolder WBT/whitebox_tools
exe_path_out <- file.path(pkg_dir, "WBT", basename(exe_path))
Sys.chmod(exe_path_out, '755')
Sys.chmod(file.path(dirname(exe_path_out), basename(wbt_runner_path(shell_quote = FALSE))), '755')
# if (os == "Windows") {
# utils::unzip(exe_zip, exdir = pkg_dir)
# } else {
# utils::untar(exe_zip, exdir = pkg_dir)
# }
# if we can find the file where we extracted it, let the user know
if (file.exists(exe_path_out)) {
cat("WhiteboxTools binary is located here: ", exe_path_out, "\n")
cat("You can now start using whitebox\n")
cat(" library(whitebox)\n")
cat(" wbt_version()\n")
# call wbt_init (sets package option)
wbt_init(exe_path = exe_path_out, wd = "")
}
} else if (!force) {
cat("WhiteboxTools binary is located here: ", exe_path, "\n")
}
# return installed path
if (check_whitebox_binary()) {
return(invisible(wbt_exe_path(shell_quote = FALSE)))
}
invisible(NULL)
}
# many packages provide an "install_*" method; alias wbt_install mirrors the wbt_ prefix for most operations. Documentation refers to install_whitebox()
#' Download and Install 'WhiteboxTools'
#'
#' This function downloads the 'WhiteboxTools' binary if needed. Pre-compiled binaries are
#' only available for download for 64-bit Linux (default compiled with glibc on Ubuntu 22.04;
#' use `platform="linux_musl"` for musl/earlier versions of glibc), Windows and Mac OS (ARM and
#' Intel) platforms. If you need WhiteboxTools for another platform follow the instructions to
#' build from source: \url{https://github.com/jblindsay/whitebox-tools}
#'
#' 'WhiteboxTools' and all of its extensions can be uninstalled by passing the `remove=TRUE` argument.
#'
#' @param pkg_dir default install path is to whitebox package "WBT" folder
#' @param platform character. Optional: suffix used for alternate platform names. On Linux, you can choose `"linux_amd64"` (default; Linux) or `"linux_musl"` for older glibc versions. On macOS Darwin you can choose `"darwin_amd64"` (default; macOS) or `"darwin_m_series"` for Apple M series hardware. Note that for `wbt_install_extension()` on the Apple M series use `"MacOS_ARM"`. Only one Windows binary is available: `"win_amd64"` (default; Windows).
#' @param force logical. Force install? Default `FALSE`. When `remove=TRUE` passed to `unlink()` to change permissions to allow removal of files/directories.
#' @param remove logical. Remove contents of "WBT" folder from `pkg_dir`? Default: `FALSE`
#' @return Prints out the location of the WhiteboxTools binary, if found. `NULL` otherwise.
#' @aliases wbt_install
#' @examples
#' \dontrun{
#' install_whitebox()
#' }
#' @export
#' @keywords General
install_whitebox <- function(pkg_dir = wbt_data_dir(), platform = NULL, force = FALSE, remove = FALSE) {
wbt_install(pkg_dir = pkg_dir, platform = platform, force = force, remove = remove)
}
#' @param extension Extension name
#' @param destdir Directory to create `/plugins/` directory for extracting extensions
#' @export
#' @keywords General
#' @rdname install_whitebox
#' @importFrom utils unzip
wbt_install_extension <- function(extension = c(
"GeneralToolsetExtension",
"AgricultureToolset",
"DemAndSpatialHydrologyToolset",
"LidarAndRemoteSensingToolset"
),
platform = NULL,
destdir = dirname(wbt_exe_path(shell_quote = FALSE))) {
extension <- match.arg(extension, c(
"GeneralToolsetExtension",
"AgricultureToolset",
"DemAndSpatialHydrologyToolset",
"LidarAndRemoteSensingToolset"
), several.ok = TRUE)
sn <- Sys.info()[["sysname"]]
fn <- tempfile(extension, fileext = ".zip")
if (missing(platform) || is.null(platform)) {
sufx <- switch(sn,
"Windows" = "win",
"Linux" = "linux",
"Darwin" = switch(Sys.info()[["machine"]],
arm64 = "MacOS_ARM",
"MacOS_Intel"))
} else {
# non-default options include: linux_musl, MacOS_ARM
sufx <- platform
}
if (sn == "Darwin" && Sys.info()["machine"] == "arm64") {
suffix <- "MacOS_ARM"
}
# GTE
if ("GeneralToolsetExtension" %in% extension) {
url <- sprintf("https://www.whiteboxgeo.com/GTE_%s/%s_%s.zip", sn, "GeneralToolsetExtension", sufx)
fn <- "GeneralToolsetExtension.zip"
} else {
url <- sprintf("https://www.whiteboxgeo.com/%s/%s_%s.zip", extension, extension, sufx)
}
ed <- file.path(destdir, "plugins")
download.file(url, destfile = fn, mode = "wb")
unzip(fn, exdir = ed, junkpaths = TRUE)
Sys.chmod(list.files(ed, full.names = TRUE), mode = '0755')
invisible(unlink(fn))
}
#' Activate 'WhiteboxTools' Extensions
#'
#' @param email Email Address
#' @param activation_key Activation Key
#' @param seat Seat Number (Default `1`)
#' @param destdir Directory containing `whitebox_tools` and `/plugins/` folder.
#'
#' @return `0` for success (invisibly). Try-error on error.
#' @export
#' @keywords General
wbt_activate <- function(email, activation_key, seat = 1,
destdir = dirname(wbt_exe_path(shell_quote = FALSE))) {
exeactivate <- ifelse(Sys.info()[["sysname"]] == "Windows",
"register_license.exe", "register_license")
input <- c("register", email, seat, activation_key, "y", "N")
invisible(try(system(file.path(destdir, "plugins", exeactivate), input = input)))
}
#' Help description for 'WhiteboxTools'
#'
#' @return Returns the help description for 'WhiteboxTools' as an R character vector.
#' @export
#' @keywords General
#'
#' @examples
#' \dontrun{
#' wbt_help()
#' }
wbt_help <- function() {
ret <- wbt_system_call("--help")
if (wbt_verbose()) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' License information for 'WhiteboxTools'
#'
#' @return Returns the license information for WhiteboxTools as an R character vector.
#' @export
#' @keywords General
#'
#' @examples
#' \dontrun{
#' wbt_license()
#' }
wbt_license <- function() {
ret <- wbt_system_call("--license")
if (wbt_verbose()) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' Version information for 'WhiteboxTools'
#'
#' @param extract Extract semantic version number from first line of result? Default: `FALSE`
#'
#' @return Returns the version information for 'WhiteboxTools' as an R character vector.
#' @export
#' @keywords General
#' @examples
#' \dontrun{
#' wbt_version()
#' }
wbt_version <- function(extract = FALSE) {
ret <- wbt_system_call("--version", check_version = FALSE)
if (extract) {
return(gsub(".*\\bv([0-9\\.]+)\\b.*", "\\1", ret[1]))
}
if (wbt_verbose()) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' All available tools in 'WhiteboxTools'
#'
#' @param keywords Keywords may be used to search available tools. Default `"''"` returns all available tools.
#'
#' @return Return all available tools in WhiteboxTools that contain the keywords.
#' @export
#' @keywords General
#'
#' @examples
#' \dontrun{
#' wbt_list_tools("lidar")
#' }
wbt_list_tools <- function(keywords = "''") {
ret <- wbt_system_call(paste("--listtools", keywords))
ret <- ret[ret != ""]
if (wbt_verbose()) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' The toolbox for a specific tool in WhiteboxTools
#'
#' Retrieve the toolbox for a specific tool.
#'
#' @param tool_name The name of the tool.
#' @details Leaving `tool_name` as default `NULL` returns results for all tools, but does not work on Windows.
#' @return Returns the toolbox for a specific tool.
#' @export
#' @keywords General
#'
#' @examples
#' \dontrun{
#' wbt_toolbox("breach_depressions")
#' }
wbt_toolbox <- function(tool_name = NULL) {
ret <- wbt_system_call(paste0("--toolbox", ifelse(!is.null(tool_name),
paste0("=", tool_name), ""))
)
# , command_only = TRUE)
# TODO: shell problems; fix null tool_name not working on windows
# system(paste(wbt_exe_path(shell_quote = TRUE), '--toolbox'), intern = TRUE)
# thread 'main' panicked at 'Unrecognized tool name C:\PROGRA~1\UNIVER~1.0_W\WBT\WHITEB~1.EXE.', src\main.rs:72:21 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
if (wbt_verbose()) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' Help description for a specific tool in 'WhiteboxTools'
#'
#' Retrieves the help description for a specific tool.
#'
#' @param tool_name The name of the tool.
#'
#' @return Returns the help description for a specific tool.
#' @details Leaving `tool_name` as default `NULL` returns results for all tools, but does not work on Windows.
#' @export
#' @keywords General
#'
#' @examples
#' \dontrun{
#' wbt_tool_help("lidar_info")
#' }
wbt_tool_help <- function(tool_name = NULL) {
ret <- wbt_system_call(paste0("--toolhelp",
ifelse(!is.null(tool_name),
paste0("=", tool_name), "")))
if (wbt_verbose()) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' Tool parameter descriptions for a specific tool in 'WhiteboxTools'
#'
#' Retrieves the tool parameter descriptions for a specific tool.
#'
#' @param tool_name The name of the tool.
#' @param quiet Prevent tool output being printed. Default: `FALSE`
#'
#' @details `quiet` argument can be set to `TRUE` to allow for "quiet" internal use within other functions.
#'
#' @return Returns the tool parameter descriptions for a specific tool.
#' @export
#' @keywords General
#'
#' @examples
#' \dontrun{
#' wbt_tool_parameters("lidar_info")
#' }
wbt_tool_parameters <- function(tool_name, quiet = FALSE) {
ret <- wbt_system_call(paste0("--toolparameters=", tool_name))
if (wbt_verbose() && !quiet) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' Source code for a specific tool in 'WhiteboxTools'
#'
#' Opens a web browser to view the source code for a specific tool on the projects source code repository.
#' @param tool_name Name of the tool.
#' @param viewer Show source code in browser? default: `TRUE`
#' @return Returns a GitHub URL to view the source code of the tool.
#' @export
#' @keywords General
#'
#' @examples
#' \dontrun{
#' wbt_view_code("breach_depressions")
#' }
#' @importFrom utils browseURL
wbt_view_code <- function(tool_name, viewer = FALSE) {
ret <- wbt_system_call(paste0("--viewcode=", tool_name))
if (viewer) {
utils::browseURL(ret)
}
if (wbt_verbose()) {
cat(ret, sep = "\n")
}
invisible(ret)
}
#' Run a tool in 'WhiteboxTools' by name
#'
#' Runs a tool and specifies tool arguments. If the prefix "whitebox::" or "wbt_" is in `tool_name` it is removed to match the definitions in `wbt_list_tools()`
#'
#' @param tool_name The name of the tool to run.
#' @param args Tool arguments.
#' @param verbose_mode Verbose mode. Without this flag, tool outputs will not be printed.
#' @param command_only Return command that would be run with `system()`? Default: `FALSE`
#'
#' @return Returns the (character) output of the tool.
#' @export
#' @keywords General
#' @seealso \link{wbt_list_tools}
#' @examples
#' \dontrun{
#' tool_name <- "breach_depressions"
#' dem <- system.file("extdata", "DEM.tif", package="whitebox")
#' output <- "./output.tif"
#' arg1 <- paste0("--dem=", dem)
#' arg2 <- paste0("--output=", output)
#' args <- paste(arg1, arg2)
#' wbt_run_tool(tool_name, args)
#' }
wbt_run_tool <- function(tool_name, args, verbose_mode = FALSE, command_only = FALSE) {
if (length(tool_name) > 1) {
# take last tool_name in case of vector length >1
# e.g. whitebox::wbt_tool_name match.call() output
tool_name <- tool_name[length(tool_name)]
}
# build the call with wbt_system_call()
ret <- wbt_system_call(paste(args, "-v"),
tool_name = tool_name,
command_only = command_only)
if (command_only) {
return(ret)
}
# identify any warning messages in output
warn.idx1 <- grep("\\*{82}", ret)
warn.idx2 <- grep("warning\\:", ret, ignore.case = TRUE)
# produce a custom error message for tools to indicate it did not run
if (length(ret) == 0 || all(nchar(ret) == 0) || !is.null(attr(ret, 'status'))) {
ret <- paste(tool_name, "-", "Elapsed Time: NA [did not run]")
} else if (wbt_verbose() && length(warn.idx2) > 0) {
# show all output from first warning indicator to last, and find elapsed time
cat(ret[min(c(warn.idx1, warn.idx2)):max(c(warn.idx1, warn.idx2))], sep = "\n")
# supports warning messages all on one line, or multi-line
# at end of processing, surrounded by "*"x82
mx <- length(ret)
if (length(warn.idx1) > 0) {
mx <- warn.idx1[1] - 1
}
ret <- paste(tool_name, "-", ret[mx])
} else if (wbt_options()$whitebox.verbose == "all") {
# in "all" mode the full output is shown
cat(ret, sep = "\n")
ret <- paste(tool_name, "-", ret[length(ret)])
} else if (is.null(verbose_mode) || !verbose_mode) {
ret <- paste(tool_name, "-", ret[length(ret)])
}