-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_spatial_data_type.R
111 lines (99 loc) · 2.94 KB
/
utils_spatial_data_type.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
#' @include internal.R
NULL
#' Spatial data type
#'
#' Identify if a spatial dataset has continuous or categorical data.
#'
#' @param x [sf::st_sf()] or [raster::stack()] or [data.frame] dataset object.
#'
#' @param index `integer` or `character` value indicating the
#' field or layer for which to calculate statistics.
#' Defaults to 1, such that the first field/layer is used to calculate
#' statistics.
#'
#' @param max_sample `integer` maximum number of cells in a raster stack
#' to sample when checking if the data are continuous or categorical.
#' Defaults to 10000.
#'
#' @param ... not used.
#'
#' @return A `character` indicating if the data are `"continuous"` or
#' `"categorical"`.
#'
#' @export
spatial_data_type <- function(x, index = 1, ...) {
UseMethod("spatial_data_type", x)
}
#' @rdname spatial_data_type
#' @export
spatial_data_type.sf <- function(x, index = 1, ...) {
# assert valid arguments
assertthat::assert_that(
inherits(x, "sf"),
assertthat::is.string(index) || assertthat::is.count(index),
assertthat::noNA(index)
)
if (is.character(index)) {
assertthat::assert_that(
assertthat::has_name(x, index)
)
}
# determine if data are continuous or categorical
## if there are more than 20 unique values, then we assume it's continuous
out <-
ifelse(n_distinct(x[[index]]) > 20, "continuous", "categorical")
# return result
out
}
#' @rdname spatial_data_type
#' @export
spatial_data_type.Raster <- function(x, index = 1, max_sample = 10000, ...) {
# assert valid arguments
assertthat::assert_that(
inherits(x, "Raster"),
assertthat::is.count(max_sample),
assertthat::noNA(max_sample),
assertthat::is.string(index) || assertthat::is.count(index),
assertthat::noNA(index)
)
if (is.character(index)) {
assertthat::assert_that(
index %in% names(x)
)
index <- which(names(x) == index)
}
# extract layer
x <- x[[index]]
# identify cells with no missing values
cells <- raster::Which(!is.na(x), cells = TRUE)
if (length(cells) > max_sample) {
cells <- sample(cells, max_sample)
}
# determine if data are continuous or categorical
## if there are more than 20 unique values, then we assume it's continuous
out <-
ifelse(n_distinct(x[cells]) > 20, "continuous", "categorical")
# return result
out
}
#' @rdname spatial_data_type
#' @export
spatial_data_type.data.frame <- function(x, index = 1, ...) {
# assert valid arguments
assertthat::assert_that(
inherits(x, c('tbl_df', 'tbl', 'data.frame')),
assertthat::is.string(index) || assertthat::is.count(index),
assertthat::noNA(index)
)
if (is.character(index)) {
assertthat::assert_that(
assertthat::has_name(x, index)
)
}
# determine if data are continuous or categorical
## if there are more than 20 unique values, then we assume it's continuous
out <-
ifelse(n_distinct(x[[index]]) > 20, "continuous", "categorical")
# return result
out
}