Skip to content

Commit

Permalink
Merge pull request #963 from rolfsimoes/dev2
Browse files Browse the repository at this point in the history
Dev2
  • Loading branch information
rolfsimoes committed May 3, 2023
2 parents 1a7ddd5 + e669a1c commit ad54393
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Generated by roxygen2: do not edit by hand

S3method(".tile_bands<-",raster_cube)
S3method(".tile_labels<-",raster_cube)
S3method(".tile_name<-",raster_cube)
S3method("sits_bands<-",raster_cube)
S3method("sits_bands<-",sits)
S3method("sits_labels<-",class_cube)
S3method("sits_labels<-",probs_cube)
S3method("sits_labels<-",sits)
Expand Down Expand Up @@ -225,6 +228,7 @@ S3method(summary,sits_accuracy)
S3method(summary,sits_area_accuracy)
S3method(summary,variance_cube)
export("%>%")
export("sits_bands<-")
export("sits_labels<-")
export(sits_accuracy)
export(sits_accuracy_summary)
Expand Down
10 changes: 10 additions & 0 deletions R/api_file_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ NULL
.as_chr(fi[["band"]])
}

.fi_rename_bands <- function(fi, rename) {
.check_chr_within(
.fi_bands(fi),
within = names(rename),
msg = "invalid renaming parameter"
)
fi[["band"]] <- unname(rename[.fi_bands(fi)])
fi
}

.fi_filter_bands <- function(fi, bands) {
bands_in_fi <- bands %in% .fi_bands(fi)
if (!all(bands_in_fi)) {
Expand Down
25 changes: 25 additions & 0 deletions R/api_tile.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ NULL
if (add_cloud) return(bands)
setdiff(bands, .band_cloud())
}

#' @title Set bands in tile file_info.
#' @rdname .tile_bands
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return tile with renamed bands
`.tile_bands<-` <- function(tile, value) {
UseMethod(".tile_bands<-", tile)
}
#' @export
`.tile_bands<-.raster_cube` <- function(tile, value) {
tile <- .tile(tile)
bands <- .tile_bands(tile)
.check_that(
length(bands) == length(value),
local_msg = paste0("bands must have length ", length(bands)),
msg = "invalid band list"
)
rename <- value
names(rename) <- bands
.fi(tile) <- .fi_rename_bands(.fi(tile), rename = rename)
tile
}
#'
#' @title Get a band definition from config.
#' @name .tile_band_conf
Expand Down
41 changes: 41 additions & 0 deletions R/sits_bands.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#'
#' @examples
#' bands <- sits_bands(point_mt_6bands)
#' bands[[5]] <- "EVI2"
#' sits_bands(point_mt_6bands) <- bands
#'
#' @export
sits_bands <- function(x) {
Expand Down Expand Up @@ -57,3 +59,42 @@ sits_bands.sits_model <- function(x) {
bands <- .ml_bands(x)
return(bands)
}

#' @rdname sits_bands
#' @export
`sits_bands<-` <- function(x, value) {
# Get the meta-type (sits or cube)
x <- .conf_data_meta_type(x)
UseMethod("sits_bands<-", x)
}

#' @rdname sits_bands
#' @export
`sits_bands<-.sits` <- function(x, value) {
bands <- sits_bands(x)
.check_that(
length(bands) == length(value),
local_msg = paste0("bands must have length ", length(bands)),
msg = "invalid band list"
)
x <- .apply(x, col = "time_series", fn = function(x) {
names(x) <- c("Index", value, "#..")
return(x)
})
return(x)
}
#' @rdname sits_bands
#' @export
`sits_bands<-.raster_cube` <- function(x, value) {
bands <- sits_bands(x)
.check_that(
length(bands) == length(value),
local_msg = paste0("bands must have length ", length(bands)),
msg = "invalid band list"
)
x <- slider::slide_dfr(x, function(tile) {
.tile_bands(tile) <- value
tile
})
return(x)
}
9 changes: 9 additions & 0 deletions man/sits_bands.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad54393

Please sign in to comment.