Skip to content

Commit

Permalink
Notwithstanding the dress standards outlined above, MCC reserves the …
Browse files Browse the repository at this point in the history
…right to refuse entry to any person considered unsuitably attired
  • Loading branch information
geryan committed Jun 17, 2024
1 parent 49eab2b commit 4cd4501
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(set_layer_names)
export(source_R)
export(split_rast)
export(standardise_rast)
export(std_rast)
export(temptif)
export(writereadrast)
importFrom(magrittr,"%>%")
50 changes: 50 additions & 0 deletions R/std_rast.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' @title Standardise raster
#' @description Standardises all layers in a `SpatRaster` to a scale of 0-1,
#' by dividing by the maximum value in each layer.
#'
#' @param x `SpatRaster` to standardise
#' @param filename Optional `character` path and filename to write output
#' @param overwrite `logical` if `TRUE` will overwrite `filename`
#'
#' @return
#' @export
#'
#' @details
#' Will break for non-numeric rasters
#'
#'
#' @examples
#' example_raster() |>
#' std_rast()
std_rast <- function(x, filename = NULL, overwrite = TRUE){
if(terra::nlyr(x)>1){

vals <- terra::values(x)

nvs <- apply(
vals,
MARGIN = 2,
FUN = function(x){
x/max(x, na.rm = TRUE)
}
)

r <- x

r[] <- nvs


} else {
r <- x / max(x)
}

if(is.null(filename)){
return(r)
}

writereadrast(
r,
filename,
overwrite
)
}
26 changes: 26 additions & 0 deletions man/std_rast.Rd

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

0 comments on commit 4cd4501

Please sign in to comment.