Skip to content

Commit 3e4555a

Browse files
Merge pull request #1097 from OldLipe/feat/temp_seg
Fix error when creating classified local cubes with NA pixels
2 parents 1638531 + aa54a0c commit 3e4555a

5 files changed

+30
-9
lines changed

R/api_check.R

+1
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@
18511851
return(classes_tile)
18521852
})
18531853
classes_num <- unique(unlist(classes_list))
1854+
classes_num <- classes_num[!is.na(classes_num)]
18541855
labels_num <- names(sits_labels(cube))
18551856
# do the labels and raster numbers match?
18561857
.check_that(

R/api_cube.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ NULL
154154
# Include class names
155155
freq <- dplyr::mutate(freq,
156156
area = area,
157-
class = labels_cube[.as_chr(freq$value)]
157+
class = labels_cube[as.character(freq$value)]
158158
)
159159
return(freq)
160160
})

R/sits_sample_functions.R

+17-4
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,10 @@ sits_sampling_design <- function(cube,
380380
#' @param sampling_design Result of sits_sampling_design
381381
#' @param alloc Allocation method chosen
382382
#' @param overhead Additional percentage to account for border points
383+
#' @param multicores Number of cores that will be used to
384+
#' sample the images in parallel.
383385
#' @param shp_file Name of shapefile to be saved (optional)
386+
#' @param progress Show progress bar? Default is TRUE.
384387
#' @return samples Point sf object with required samples
385388
#'
386389
#' @examples
@@ -418,7 +421,9 @@ sits_stratified_sampling <- function(cube,
418421
sampling_design,
419422
alloc = "alloc_prop",
420423
overhead = 1.0,
421-
shp_file = NULL){
424+
multicores = 2,
425+
shp_file = NULL,
426+
progress = TRUE){
422427
# check the cube is valid
423428
.check_raster_cube_files(cube)
424429
# check cube is class cube
@@ -443,13 +448,21 @@ sits_stratified_sampling <- function(cube,
443448
.check_that(all(unname(samples_class) == floor(unname(samples_class))),
444449
msg = "allocation values should be integer"
445450
)
451+
.check_multicores(multicores, min = 1, max = 2048)
452+
.check_progress(progress)
446453
# name samples class
447454
names(samples_class) <- rownames(sampling_design)
448455
# include overhead
449456
samples_class <- ceiling(samples_class * overhead)
450457
# estimate size
451458
size <- ceiling(max(samples_class)/nrow(cube))
452-
samples_lst <- slider::slide(cube, function(tile) {
459+
# Prepare parallel processing
460+
.parallel_start(workers = multicores)
461+
on.exit(.parallel_stop(), add = TRUE)
462+
# Create assets as jobs
463+
cube_assets <- .cube_split_assets(cube)
464+
# Process each asset in parallel
465+
samples <- .jobs_map_parallel_dfr(cube_assets, function(tile) {
453466
robj <- .raster_open_rast(.tile_path(tile))
454467
cls <- data.frame(id = 1:n_labels,
455468
cover = labels)
@@ -463,8 +476,8 @@ sits_stratified_sampling <- function(cube,
463476
samples_sf <- sf::st_as_sf(samples_sv)
464477
samples_sf <- dplyr::mutate(samples_sf,
465478
label = labels[.data[["cover"]]])
466-
})
467-
samples <- do.call(rbind, samples_lst)
479+
samples_sf <- sf::st_transform(samples_sf, crs = "EPSG:4326")
480+
}, progress = progress)
468481

469482
samples <- purrr::map_dfr(labels, function(lab){
470483
samples_class <- samples |>

man/sits_get_data.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sits_stratified_sampling.Rd

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)