Skip to content

Commit 9a8ab16

Browse files
new error messages
2 parents 1e18e46 + f0a09ff commit 9a8ab16

35 files changed

+366
-345
lines changed

R/api_check.R

+8-37
Original file line numberDiff line numberDiff line change
@@ -920,12 +920,9 @@
920920
)
921921
# check extension
922922
if (!is.null(extensions)) {
923-
.check_chr_within(ext_file(x),
924-
within = extensions,
925-
case_sensitive = FALSE,
926-
local_msg = local_msg,
927-
msg = .conf("messages", ".check_file_extension")
928-
)
923+
extension <- ext_file(x)
924+
.check_that(extension %in% extensions,
925+
local_msg = local_msg)
929926
}
930927
if (file_exists) {
931928
existing_files <- file.exists(x)
@@ -1166,7 +1163,7 @@
11661163
#' @noRd
11671164
.check_int_parameter <- function(x, min = -2^31 + 1, max = 2^31 - 1,
11681165
len_min = 1, len_max = 2^31 - 1,
1169-
is_odd = FALSE,
1166+
is_odd = FALSE, is_named = FALSE,
11701167
allow_null = FALSE, msg = NULL) {
11711168
# check parameter name
11721169
param <- deparse(substitute(x, environment()))
@@ -1181,6 +1178,7 @@
11811178
len_min = len_min,
11821179
len_max = len_max,
11831180
is_integer = TRUE,
1181+
is_named = is_named,
11841182
is_odd = is_odd,
11851183
local_msg = local_msg,
11861184
msg = msg
@@ -1785,30 +1783,6 @@
17851783
.check_that(length(smoothness) == 1 || length(smoothness) == nlabels)
17861784
return(invisible(smoothness))
17871785
}
1788-
#' @title Check that cube is regular
1789-
#' @name .check_is_regular
1790-
#' @keywords internal
1791-
#' @noRd
1792-
#' @param cube datacube
1793-
#' @return Called for side effects.
1794-
.check_is_regular <- function(cube) {
1795-
.check_set_caller(".check_is_regular")
1796-
is_regular <- TRUE
1797-
if (!.cube_is_complete(cube)) {
1798-
is_regular <- FALSE
1799-
}
1800-
if (!.cube_has_unique_bbox(cube)) {
1801-
is_regular <- FALSE
1802-
}
1803-
if (!.cube_has_unique_tile_size(cube)) {
1804-
is_regular <- FALSE
1805-
}
1806-
if (length(.cube_timeline(cube)) > 1) {
1807-
is_regular <- FALSE
1808-
}
1809-
.check_that(is_regular)
1810-
return(invisible(cube))
1811-
}
18121786
#' @title Check if data contains predicted and reference values
18131787
#' @name .check_pred_ref_match
18141788
#' @param reference vector with reference labels
@@ -1924,7 +1898,7 @@
19241898
.check_set_caller(".check_cube_bands")
19251899
# all bands are upper case
19261900
bands <- toupper(bands)
1927-
cube_bands <- .cube_bands(cube = cube, add_cloud = add_cloud)
1901+
cube_bands <- toupper(.cube_bands(cube = cube, add_cloud = add_cloud))
19281902
.check_that(all(bands %in% cube_bands))
19291903
return(invisible(cube))
19301904
}
@@ -2148,7 +2122,7 @@
21482122
.check_endmembers_tbl <- function(em) {
21492123
.check_set_caller(".check_endmembers_tbl")
21502124
# Pre-condition
2151-
.check_that(any(is.na(em)))
2125+
.check_that(!any(is.na(em)))
21522126
# Pre-condition
21532127
.check_chr_contains(
21542128
x = colnames(em),
@@ -2404,10 +2378,7 @@
24042378
#' @noRd
24052379
.check_filter_fn <- function(filter_fn){
24062380
.check_set_caller(".check_filter_fn")
2407-
name <- deparse(substitute(filter_fn))
2408-
2409-
.check_that(grepl("whittaker", name) ||
2410-
grepl("sgolay"), name)
2381+
.check_that(is.function(filter_fn))
24112382
}
24122383
.check_dist_method <- function(dist_method){
24132384
.check_set_caller(".check_dist_method")

R/api_cube.R

+23
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,29 @@ NULL
512512
is_complete <- .cube_is_complete(cube)
513513
return(is_complete)
514514
}
515+
#' @title Check that cube is regular
516+
#' @name .cube_is_regular
517+
#' @keywords internal
518+
#' @noRd
519+
#' @param cube datacube
520+
#' @return Called for side effects.
521+
.cube_is_regular <- function(cube) {
522+
.check_set_caller(".cube_is_regular")
523+
is_regular <- TRUE
524+
if (!.cube_is_complete(cube)) {
525+
is_regular <- FALSE
526+
}
527+
if (!.cube_has_unique_bbox(cube)) {
528+
is_regular <- FALSE
529+
}
530+
if (!.cube_has_unique_tile_size(cube)) {
531+
is_regular <- FALSE
532+
}
533+
if (length(.cube_timeline(cube)) > 1) {
534+
is_regular <- FALSE
535+
}
536+
return(is_regular)
537+
}
515538
#' @title Find out how many images are in cube during a period
516539
#' @noRd
517540
#' @param cube A data cube.

R/api_download.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@
123123
if (.file_is_local(file)) {
124124
file <- .file_path("file://", file, sep = "")
125125
}
126-
httr::GET(
127-
url = file, httr::write_disk(path = out_file, overwrite = TRUE)
126+
# httr::GET(
127+
# url = file, httr::write_disk(path = out_file, overwrite = TRUE)
128+
# )
129+
download.file(
130+
url = file, destfile = out_file,
131+
quiet = FALSE
128132
)
129133
# Return file name
130134
out_file

R/api_shp.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
# set caller to show in errors
6464
.check_set_caller(".shp_transform_to_sf")
6565
# precondition - does the shapefile exist?
66-
.check_file(shp_file, extensions = ".shp")
66+
.check_file(shp_file, extensions = "shp")
6767

6868
# read the shapefile
6969
sf_shape <- sf::read_sf(shp_file)
@@ -78,7 +78,7 @@
7878
# postcondition - can the function deal with the geometry_type?
7979
.check_that(as.character(geom_type) %in% .conf("sf_geom_types_supported"))
8080
# postcondition - is the shape attribute valid?
81-
.check_shp_attribute(sf_shape)
81+
.check_shp_attribute(sf_shape, shp_attr)
8282

8383
return(sf_shape)
8484
}

R/api_source_local.R

+2-5
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@
370370
pattern = paste0("\\.(", paste0(file_ext, collapse = "|"), ")$")
371371
)
372372
# post-condition
373-
.check_that(all(file.exists(gpkg_files)))
373+
gpkg_files_path <- paste0(vector_dir,"/",gpkg_files)
374+
.check_that(all(file.exists(gpkg_files_path)))
374375

375376
# remove the extension
376377
gpkg_files_noext <- tools::file_path_sans_ext(gpkg_files)
@@ -385,10 +386,6 @@
385386
})
386387
# subset gkpg files
387388
gpkg_files_ok <- gpkg_files_lst[are_gpkg_files_ok]
388-
389-
# post condition
390-
.check_that(all(file.exists(gpkg_files_ok)))
391-
392389
# filter only valid files
393390
gpkg_files_filt <- gpkg_files[are_gpkg_files_ok]
394391
# bind rows

R/api_source_mpc.R

+25-1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@
269269
if (!nzchar(access_key)) {
270270
access_key <- NULL
271271
}
272+
# Clean old tokens cached in rstac
273+
.mpc_clean_token_cache()
272274
items_info <- suppressWarnings(
273275
rstac::items_sign(
274276
items_info, sign_fn = rstac::sign_planetary_computer(
@@ -285,7 +287,7 @@
285287
collection,
286288
stac_query, ...,
287289
tiles = NULL,
288-
orbit = "descending") {
290+
orbit = "descending") {
289291
`.source_items_new.mpc_cube_sentinel-1-grd`(
290292
source = source,
291293
collection = collection,
@@ -348,6 +350,8 @@
348350
if (!nzchar(access_key)) {
349351
access_key <- NULL
350352
}
353+
# Clean old tokens cached in rstac
354+
.mpc_clean_token_cache()
351355
items_info <- suppressWarnings(
352356
rstac::items_sign(
353357
items_info,
@@ -377,6 +381,8 @@
377381
stac_query, ...,
378382
tiles = NULL,
379383
platform = NULL) {
384+
.check_set_caller(".source_items_new_mpc_cube_landsat_c2_l2")
385+
.check_that(is.null(tiles))
380386
if (.has(platform)) {
381387
platform <- .stac_format_platform(
382388
source = source,
@@ -399,6 +405,8 @@
399405
if (!nzchar(access_key)) {
400406
access_key <- NULL
401407
}
408+
# Clean old tokens cached in rstac
409+
.mpc_clean_token_cache()
402410
items <- suppressWarnings(
403411
rstac::items_sign(
404412
items,
@@ -506,3 +514,19 @@
506514
.check_that(.has_not(tiles))
507515
return(invisible(source))
508516
}
517+
#' @title Cleak MPC token cache
518+
#' @name .mpc_clean_token_cache
519+
#' @description
520+
#' Cleans the the token cache for MPC to reduce timeout effects
521+
#' @return Called for side effects.
522+
#' @keywords internal
523+
#' @noRd
524+
#' @export
525+
.mpc_clean_token_cache <- function() {
526+
mpc_token <- get("ms_token", envir = asNamespace("rstac"), inherits = TRUE)
527+
cached_tokens <- names(mpc_token)
528+
lapply(cached_tokens, function(cached_token) {
529+
assign(cached_token, NULL, envir = mpc_token)
530+
})
531+
return(invisible(NULL))
532+
}

R/sits_apply.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ sits_apply.raster_cube <- function(data, ...,
126126
progress = FALSE) {
127127
# Check cube
128128
.check_is_raster_cube(data)
129-
.check_is_regular(data)
129+
.check_that(.cube_is_regular(data))
130130
# Check window size
131131
.check_int_parameter(window_size, min = 3, is_odd = TRUE)
132132
# Check normalized index

R/sits_classify.R

+5-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ sits_classify.raster_cube <- function(data,
216216
.check_set_caller("sits_classify_raster")
217217
# preconditions
218218
.check_is_raster_cube(data)
219-
.check_is_regular(data)
219+
.check_that(.cube_is_regular(data))
220220
.check_is_sits_model(ml_model)
221221
.check_int_parameter(memsize, min = 1, max = 16384)
222222
.check_int_parameter(multicores, min = 1, max = 2048)
@@ -249,7 +249,8 @@ sits_classify.raster_cube <- function(data,
249249
cube = data, start_date = start_date, end_date = end_date
250250
)
251251
}
252-
.check_filter_fn(filter_fn)
252+
if (.has(filter_fn))
253+
.check_filter_fn(filter_fn)
253254
# Retrieve the samples from the model
254255
samples <- .ml_samples(ml_model)
255256
# Do the samples and tile match their timeline length?
@@ -405,7 +406,8 @@ sits_classify.segs_cube <- function(data,
405406
cube = data, start_date = start_date, end_date = end_date
406407
)
407408
}
408-
.check_filter_fn(filter_fn)
409+
if (.has(filter_fn))
410+
.check_filter_fn(filter_fn)
409411
# Check memory and multicores
410412
# Get block size
411413
block <- .raster_file_blocksize(.raster_open_rast(.tile_path(data)))

R/sits_cube_copy.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ sits_cube_copy <- function(cube,
6262
# Pre-conditions
6363
.check_is_raster_cube(cube)
6464
# Cupe copy does not work for SAR data
65-
if ("sar_cube" %in% class(cube) && !.check_is_regular(cube)) {
65+
if ("sar_cube" %in% class(cube) && !.cube_is_regular(cube)) {
6666
warning(.conf("messages"), "sits_cube_copy_sar_no_copy")
6767
return(cube)
6868
}

R/sits_filters.R

-6
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ sits_filter <- function(data, filter = sits_whittaker()) {
6666
#' }
6767
#' @export
6868
sits_whittaker <- function(data = NULL, lambda = 0.5) {
69-
.check_set_caller("sits_filter")
70-
if (.has(data))
71-
.check_that(inherits(data, "sits") || inherits(data, "raster_cube"))
7269
filter_fun <- function(data) {
7370
if (inherits(data, "matrix")) {
7471
return(smooth_whit_mtx(data, lambda = lambda, length = ncol(data)))
@@ -126,9 +123,6 @@ sits_whittaker <- function(data = NULL, lambda = 0.5) {
126123
#' }
127124
#' @export
128125
sits_sgolay <- function(data = NULL, order = 3, length = 5) {
129-
.check_set_caller("sits_filter")
130-
if (.has(data))
131-
.check_that(inherits(data, "sits") || inherits(data, "raster_cube"))
132126
# compute filter coefficients once
133127
f_res <- .signal_sgolay_coef(p = order, n = length, ts = 1)
134128
# function to be applied

R/sits_get_data.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ sits_get_data <- function(cube,
116116
.check_set_caller("sits_get_data")
117117
# Pre-conditions
118118
.check_is_raster_cube(cube)
119-
.check_is_regular(cube)
119+
.check_that(.cube_is_regular(cube))
120120
.check_raster_cube_files(cube)
121121
.check_cube_bands(cube, bands = bands)
122122
.check_crs(crs)

R/sits_mixture_model.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ sits_mixture_model.raster_cube <- function(data, endmembers, ...,
176176
# is added as a band
177177
data <- .cube_filter_bands(cube = data, bands = bands)
178178
# Check if cube is regular
179-
.check_is_regular(data)
179+
.check_that(.cube_is_regular(data))
180180
# Pre-condition
181181
.check_endmembers_bands(
182182
em = em,

R/sits_reduce.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ sits_reduce.raster_cube <- function(data, ...,
141141

142142
# Check cube
143143
.check_is_raster_cube(data)
144-
.check_is_regular(data)
144+
.check_that(.cube_is_regular(data))
145145
# Check memsize
146146
.check_num_parameter(memsize, min = 1, max = 16384)
147147
# Check multicores

R/sits_sample_functions.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ sits_stratified_sampling <- function(cube,
431431
# retrieve samples class
432432
samples_class <- unlist(sampling_design[,alloc])
433433
# check samples class
434-
.check_int_parameter(samples_class,
434+
.check_int_parameter(samples_class, is_named = TRUE,
435435
msg = .conf("messages", "sits_sampling_design_samples")
436436
)
437437
.check_int_parameter(multicores, min = 1, max = 2048)

R/sits_segmentation.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ sits_segment <- function(cube,
9292
.check_set_caller("sits_segment")
9393
# Preconditions
9494
.check_is_raster_cube(cube)
95-
.check_is_regular(cube)
95+
.check_that(.cube_is_regular(cube))
9696
.check_int_parameter(memsize, min = 1, max = 16384)
9797
.check_output_dir(output_dir)
9898
version <- .check_version(version)

R/sits_summary.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ summary.raster_cube <- function(object, ..., tile = NULL, date = NULL) {
161161
cli::cli_li("Bands: {.field {sits_bands(object)}}")
162162
timeline <- unique(lubridate::as_date(unlist(.cube_timeline(object))))
163163
cli::cli_li("Timeline: {.field {timeline}}")
164-
is_regular <- .check_is_regular(object)
164+
is_regular <- .cube_is_complete(object)
165165
cli::cli_li("Regular cube: {.field {is_regular}}")
166166
# Display cube cloud coverage
167167
if ("CLOUD" %in% .cube_bands(object) &&

R/sits_view.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ sits_view.uncertainty_cube <- function(x, ...,
246246
cube <- .view_filter_tiles(x, tiles)
247247
# more than one tile? needs regular cube
248248
if (nrow(cube) > 1) {
249-
.check_is_regular(cube)
249+
.check_that(.cube_is_regular(data))
250250
}
251251
# check the view_max_mb parameter
252252
view_max_mb <- .view_set_max_mb(view_max_mb)

0 commit comments

Comments
 (0)