46
46
# ' A confusion matrix assessment produced by the caret package.
47
47
#
48
48
# ' @note
49
- # ' The validation data needs to contain the following columns: "latitude",
49
+ # ' The ` validation` data needs to contain the following columns: "latitude",
50
50
# ' "longitude", "start_date", "end_date", and "label". It can be either a
51
- # ' path to a CSV file, a sits tibble or a data frame.
51
+ # ' path to a CSV file, a sits tibble, a data frame, or an sf object.
52
+ # '
53
+ # ' When `validation` is an sf object, the columns "latitude" and "longitude" are
54
+ # ' not required as the locations are extracted from the geometry column. The
55
+ # ' `centroid` is calculated before extracting the location values for any
56
+ # ' geometry type.
52
57
# '
53
58
# ' @examples
54
59
# ' if (sits_run_examples()) {
@@ -131,20 +136,42 @@ sits_accuracy.sits <- function(data, ...) {
131
136
# ' @export
132
137
sits_accuracy.class_cube <- function (data , ... , validation ) {
133
138
.check_set_caller(" sits_accuracy_class_cube" )
134
- # generic function
135
- # Is this a file?
139
+ # handle sample files in CSV format
136
140
if (is.character(validation )) {
137
141
# Is this a valid file?
138
142
.check_validation_file(validation )
139
143
# Read sample information from CSV file and put it in a tibble
140
144
validation <- .csv_get_samples(validation )
141
145
}
142
- # Precondition - check if validation samples are OK
146
+ # handle `sf` objects
147
+ if (inherits(validation , " sf" )) {
148
+ # Pre-condition - check for the required columns
149
+ .check_chr_contains(colnames(validation ), c(
150
+ " label" , " start_date" , " end_date"
151
+ ))
152
+ # transform the `sf` object in a valid
153
+ validation <- validation | >
154
+ dplyr :: mutate(
155
+ geom = sf :: st_geometry(validation )
156
+ ) | >
157
+ dplyr :: mutate(
158
+ geom = sf :: st_centroid(.data [[" geom" ]])
159
+ ) | >
160
+ dplyr :: mutate(
161
+ coords = sf :: st_coordinates(.data [[" geom" ]])
162
+ ) | >
163
+ dplyr :: mutate(
164
+ longitude = .data [[" coords" ]][, 1 ],
165
+ latitude = .data [[" coords" ]][, 2 ]
166
+ ) | >
167
+ dplyr :: select(
168
+ " start_date" , " end_date" , " label" , " longitude" , " latitude"
169
+ )
170
+ }
171
+ # Pre-condition - check if validation samples are OK
143
172
validation <- .check_samples(validation )
144
-
145
173
# Find the labels of the cube
146
174
labels_cube <- sits_labels(data )
147
-
148
175
# Create a list of (predicted, reference) values
149
176
# Consider all tiles of the data cube
150
177
pred_ref_lst <- slider :: slide(data , function (tile ) {
@@ -210,7 +237,6 @@ sits_accuracy.class_cube <- function(data, ..., validation) {
210
237
pred_ref <- do.call(rbind , pred_ref_lst )
211
238
# is this data valid?
212
239
.check_null_parameter(pred_ref )
213
-
214
240
# Create the error matrix
215
241
error_matrix <- table(
216
242
factor (pred_ref [[" predicted" ]],
@@ -222,7 +248,6 @@ sits_accuracy.class_cube <- function(data, ..., validation) {
222
248
labels = labels_cube
223
249
)
224
250
)
225
-
226
251
# Get area for each class of the cube
227
252
class_areas <- .cube_class_areas(cube = data )
228
253
# Compute accuracy metrics
0 commit comments