|
6 | 6 |
|
7 | 7 | # check points fall within the raster layer
|
8 | 8 | .check_within <- function(x, r) {
|
9 |
| - bbox <- sf::st_bbox(x) |
10 |
| - ex <- terra::ext(r) |
| 9 | + bbox <- sf::st_bbox(x) |
| 10 | + ex <- terra::ext(r) |
11 | 11 |
|
12 |
| - y <- bbox[1] >= terra::xmin(ex) & |
13 |
| - bbox[3] <= terra::xmax(ex) & |
14 |
| - bbox[2] >= terra::ymin(ex) & |
15 |
| - bbox[4] <= terra::ymax(ex) |
| 12 | + y <- bbox[1] >= terra::xmin(ex) & |
| 13 | + bbox[3] <= terra::xmax(ex) & |
| 14 | + bbox[2] >= terra::ymin(ex) & |
| 15 | + bbox[4] <= terra::ymax(ex) |
16 | 16 |
|
17 |
| - if(!y) stop("The x's bounding box lies outside the raster extent.") |
| 17 | + if(!y) stop("The x's bounding box lies outside the raster extent.") |
18 | 18 | }
|
19 | 19 |
|
20 | 20 |
|
21 | 21 | # check for x
|
22 | 22 | .check_x <- function(x, name = "x"){
|
23 |
| - if(!methods::is(x, "sf")){ |
24 |
| - tryCatch( |
25 |
| - { |
26 |
| - x <- sf::st_as_sf(x) |
27 |
| - }, |
28 |
| - error = function(cond) { |
29 |
| - message(sprintf("'%s' is not convertible to an sf object!", name)) |
30 |
| - message(sprintf("'%s' must be an sf or spatial* object.", name)) |
31 |
| - } |
32 |
| - ) |
33 |
| - } |
34 |
| - return(x) |
| 23 | + if(!methods::is(x, "sf")){ |
| 24 | + tryCatch( |
| 25 | + { |
| 26 | + x <- sf::st_as_sf(x) |
| 27 | + }, |
| 28 | + error = function(cond) { |
| 29 | + message(sprintf("'%s' is not convertible to an sf object!", name)) |
| 30 | + message(sprintf("'%s' must be an sf or spatial* object.", name)) |
| 31 | + } |
| 32 | + ) |
| 33 | + } |
| 34 | + return(x) |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | # check for column matching colnames(x)
|
38 | 38 | .check_column <- function(column, x){
|
39 |
| - if(!is.null(column)){ |
40 |
| - if(!column %in% colnames(x)){ |
41 |
| - warning(sprintf("There is no column named '%s' in 'x'. Column is ignored!\n", column)) |
42 |
| - column <- NULL |
| 39 | + if(!is.null(column)){ |
| 40 | + if(!column %in% colnames(x)){ |
| 41 | + warning(sprintf("There is no column named '%s' in 'x'. Column is ignored!\n", column)) |
| 42 | + column <- NULL |
| 43 | + } |
43 | 44 | }
|
44 |
| - } |
45 |
| - return(column) |
| 45 | + return(column) |
46 | 46 | }
|
47 | 47 |
|
48 | 48 | # column should be binary or categorical
|
49 | 49 | .check_classes <- function(clen, column, th = 15){
|
50 |
| - if(clen > th){ |
51 |
| - warning( |
52 |
| - sprintf( |
53 |
| - paste( |
54 |
| - "The are too many unique values in '%s'.", |
55 |
| - "Use 'column' only for binary or categorical responses (ignore this if it is).\n" |
56 |
| - ), |
57 |
| - column |
58 |
| - ) |
59 |
| - ) |
60 |
| - } |
| 50 | + if(clen > th){ |
| 51 | + warning( |
| 52 | + sprintf( |
| 53 | + paste( |
| 54 | + "The are too many unique values in '%s'.", |
| 55 | + "Use 'column' only for binary or categorical responses (ignore this if it is).\n" |
| 56 | + ), |
| 57 | + column |
| 58 | + ) |
| 59 | + ) |
| 60 | + } |
61 | 61 | }
|
62 | 62 |
|
63 | 63 |
|
64 | 64 | # check raster extent is valid
|
65 | 65 | .check_ext <- function(r) {
|
66 |
| - tryCatch( |
67 |
| - { |
68 |
| - e <- terra::ext(r) |
69 |
| - vals <- e[1:4] |
70 |
| - }, |
71 |
| - error = function(cond) { |
72 |
| - stop("Failed to extract raster extent: ", conditionMessage(cond)) |
73 |
| - } |
74 |
| - ) |
| 66 | + tryCatch( |
| 67 | + { |
| 68 | + e <- terra::ext(r) |
| 69 | + vals <- e[1:4] |
| 70 | + }, |
| 71 | + error = function(cond) { |
| 72 | + stop("Failed to extract raster extent: ", conditionMessage(cond)) |
| 73 | + } |
| 74 | + ) |
75 | 75 |
|
76 |
| - y <- all(is.finite(vals)) && |
77 |
| - (terra::xmax(e) > terra::xmin(e)) && |
78 |
| - (terra::ymax(e) > terra::ymin(e)) |
| 76 | + y <- all(is.finite(vals)) && |
| 77 | + (terra::xmax(e) > terra::xmin(e)) && |
| 78 | + (terra::ymax(e) > terra::ymin(e)) |
79 | 79 |
|
80 |
| - if (!y) stop("Invalid raster extent: values are non-finite, degenerate, or out of range.") |
| 80 | + if (!y) stop("Invalid raster extent: values are non-finite or out of range.") |
81 | 81 | }
|
82 | 82 |
|
83 | 83 | # check for r
|
84 | 84 | .check_r <- function(r, name = "r"){
|
85 |
| - if(!methods::is(r, "SpatRaster")){ |
86 |
| - tryCatch( |
87 |
| - { |
88 |
| - r <- terra::rast(r) |
89 |
| - }, |
90 |
| - error = function(cond) { |
91 |
| - message(sprintf("'%s' is not convertible to a terra SpatRaster object!", name)) |
92 |
| - message(sprintf("'%s' must be a SpatRaster, stars, Raster* object, or path to a raster file on disk.", name)) |
93 |
| - } |
94 |
| - ) |
95 |
| - } |
96 |
| - .check_ext(r) # check for valid extent |
| 85 | + if(!methods::is(r, "SpatRaster")){ |
| 86 | + tryCatch( |
| 87 | + { |
| 88 | + r <- terra::rast(r) |
| 89 | + }, |
| 90 | + error = function(cond) { |
| 91 | + message(sprintf("'%s' is not convertible to a terra SpatRaster object!", name)) |
| 92 | + message(sprintf("'%s' must be a SpatRaster, stars, Raster* object, or path to a raster file on disk.", name)) |
| 93 | + } |
| 94 | + ) |
| 95 | + } |
| 96 | + # check for a valid extent |
| 97 | + .check_ext(r) |
97 | 98 |
|
98 |
| - return(r) |
| 99 | + return(r) |
99 | 100 | }
|
100 | 101 |
|
101 | 102 | # check for required packages
|
102 | 103 | .check_pkgs <- function(pkg){
|
103 |
| - pkgna <- names(which(sapply(sapply(pkg, find.package, quiet = TRUE), length) == 0)) |
104 |
| - if(length(pkgna) > 0){ |
105 |
| - nm <- paste(pkgna, collapse = ", ") |
106 |
| - message("This function requires these packages: ", nm, "\nWould you like to install them now?\n1: yes\n2: no") |
107 |
| - user <- readline(prompt = paste0("Selection: ")) |
108 |
| - if(tolower(user) %in% c("1", "yes", "y")){ |
109 |
| - utils::install.packages(pkgna) |
110 |
| - } else{ |
111 |
| - stop("Please install these packages for function to work: ", nm) |
| 104 | + pkgna <- names(which(sapply(sapply(pkg, find.package, quiet = TRUE), length) == 0)) |
| 105 | + if(length(pkgna) > 0){ |
| 106 | + nm <- paste(pkgna, collapse = ", ") |
| 107 | + message("This function requires these packages: ", nm, "\nWould you like to install them now?\n1: yes\n2: no") |
| 108 | + user <- readline(prompt = paste0("Selection: ")) |
| 109 | + if(tolower(user) %in% c("1", "yes", "y")){ |
| 110 | + utils::install.packages(pkgna) |
| 111 | + } else{ |
| 112 | + stop("Please install these packages for function to work: ", nm) |
| 113 | + } |
112 | 114 | }
|
113 |
| - } |
114 | 115 | }
|
115 | 116 |
|
0 commit comments