R/subpop.estimation.R

Defines functions subpop.estimation

Documented in subpop.estimation

#' @title Internal function
#'
#' @description Subpopulations estimation
#' 
#' @param XY data.frame with first two columns are projected coordinates (numeric)
#' @param resol_sub_pop numeric. Defines the radius of the circles
#'   around each occurrence, in kilometres.
#' @param export_shp logical. Whether the resulting shapefiles should be
#'   exported. FALSE by default.
#' @inheritParams proj_crs
#'
#' @import sf
#' @keywords internal
#' @export
subpop.estimation <- function(XY,
                         resol_sub_pop, 
                         export_shp = FALSE,
                         proj_type = "cea") {
  
  points_sf <- st_as_sf(XY, coords = c(2, 1))
  buff_sf <- st_buffer(points_sf, dist = resol_sub_pop * 1000)
  buff_sf <- st_union(buff_sf) 
  buff_sf <- st_cast(buff_sf, "POLYGON")
  SubPopPoly <-
    st_as_sf(data.frame(buff_sf))

  st_crs(SubPopPoly) <- proj_type
  
  NbeSubPop <- nrow(SubPopPoly)

  if (export_shp) {
    OUTPUT <- list(NbeSubPop, SubPopPoly)
    names(OUTPUT) <- c("number_subpop", "poly_subpop")
  
  } else {
    OUTPUT <- NbeSubPop
    names(OUTPUT) <- c("number_subpop")
  }
  
  return(OUTPUT)
}
gdauby/ConR documentation built on Jan. 30, 2024, 11:10 p.m.