R/extract_raster_postprob.R

Defines functions extract_raster_postprob

Documented in extract_raster_postprob

#' @title convert post probability raster to a dataframe
#' @author Melody Premaillon
#'
#' @param path folder path where raster is saved
#' @param name_rast_postprob name of susceptibility raster
#' @param name_rast_conf (optional) name of confidence raster
#'
#' @return data frame with single post probability values, their frequency and associated statistical confidence.
#'
#' @description This function read the susceptibility raster ans optionally the confidence raster (for validation step). It creates a data frame of unique post probability value (associated confidence) andthe number of pixels concerned.
#'
#' @importFrom raster raster
#' @importFrom raster values
#' @import dplyr
#' @export

extract_raster_postprob <- function(path, name_rast_postprob, name_rast_conf = ""){

  postprob_raster <- raster(paste(path, "/", name_rast_postprob, ".tif", sep=""))

  if (name_rast_conf != ""){
    conf_raster <- raster(paste(path, "/", name_rast_conf, ".tif", sep=""))
    rast_df <- data.frame(POST_PROB = values(postprob_raster), CONF = values(conf_raster))

    rast_df <- rast_df %>%
      filter(!is.na(POST_PROB)) %>%
      group_by(POST_PROB) %>%
      summarise(COUNT = n(),
                CONF = mean(CONF, na.rm=T)) %>%
      ungroup()
  }else {
    rast_df <- data.frame(POST_PROB = values(postprob_raster))

    rast_df <- rast_df %>%
      filter(!is.na(POST_PROB)) %>%
      group_by(POST_PROB) %>%
      summarise(COUNT = n()) %>%
      ungroup()
  }



  return(rast_df)
}
MelodyPremaillon/valwoe documentation built on April 16, 2022, 12:46 a.m.