Skip to content

Commit 1dcc73e

Browse files
Merge pull request #1161 from M3nin0/feature/sits-accuracy-sf
add support for sf objects in sits_accuracy
2 parents 908cc9a + b30a95a commit 1dcc73e

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

R/sits_accuracy.R

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@
4646
#' A confusion matrix assessment produced by the caret package.
4747
#
4848
#' @note
49-
#' The validation data needs to contain the following columns: "latitude",
49+
#' The `validation` data needs to contain the following columns: "latitude",
5050
#' "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.
5257
#'
5358
#' @examples
5459
#' if (sits_run_examples()) {
@@ -131,20 +136,42 @@ sits_accuracy.sits <- function(data, ...) {
131136
#' @export
132137
sits_accuracy.class_cube <- function(data, ..., validation) {
133138
.check_set_caller("sits_accuracy_class_cube")
134-
# generic function
135-
# Is this a file?
139+
# handle sample files in CSV format
136140
if (is.character(validation)) {
137141
# Is this a valid file?
138142
.check_validation_file(validation)
139143
# Read sample information from CSV file and put it in a tibble
140144
validation <- .csv_get_samples(validation)
141145
}
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
143172
validation <- .check_samples(validation)
144-
145173
# Find the labels of the cube
146174
labels_cube <- sits_labels(data)
147-
148175
# Create a list of (predicted, reference) values
149176
# Consider all tiles of the data cube
150177
pred_ref_lst <- slider::slide(data, function(tile) {
@@ -210,7 +237,6 @@ sits_accuracy.class_cube <- function(data, ..., validation) {
210237
pred_ref <- do.call(rbind, pred_ref_lst)
211238
# is this data valid?
212239
.check_null_parameter(pred_ref)
213-
214240
# Create the error matrix
215241
error_matrix <- table(
216242
factor(pred_ref[["predicted"]],
@@ -222,7 +248,6 @@ sits_accuracy.class_cube <- function(data, ..., validation) {
222248
labels = labels_cube
223249
)
224250
)
225-
226251
# Get area for each class of the cube
227252
class_areas <- .cube_class_areas(cube = data)
228253
# Compute accuracy metrics

man/sits_accuracy.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-accuracy.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ test_that("Accuracy areas", {
159159
expected = 0.75,
160160
tolerance = 0.5
161161
)
162+
163+
# alternative: use a `sf` object
164+
samples_sf <- samples_csv |>
165+
sf::st_as_sf(
166+
coords = c("longitude", "latitude"), crs = 4326
167+
) |>
168+
dplyr::rename("geom" = "geometry")
169+
as3 <- sits_accuracy(label_cube, validation = samples_sf)
170+
171+
expect_true(as.numeric(as3$area_pixels["Forest"]) >
172+
as3$area_pixels["Pasture"])
173+
expect_equal(as.numeric(as3$accuracy$overall),
174+
expected = 0.75,
175+
tolerance = 0.5
176+
)
162177
})
163178

164179
test_that("Accuracy areas when samples labels do not match cube labels", {

0 commit comments

Comments
 (0)