# Generated by fusen: do not edit by hand
#' Simulate individuals with a inhomogenous Poisson point process
#'
#' From an sf class density map `map_obj` (data.frame), an inohomogene Poisson point process is used to simulate the presence of individuals in the study area. The probability of presence of an individual is dependent on the density given by the map.
#' @param map_obj Dataframe. Sf map with a colum containg density informations density_m
#' @param crs Numeric. Projection system.
#'
#' @importFrom glue glue
#' @importFrom assertthat assert_that
#' @importFrom dplyr mutate select filter
#' @importFrom sf st_centroid st_coordinates st_crs
#' @importFrom sp coordinates<- proj4string<- gridded<- CRS
#' @importFrom maptools as.im.SpatialGridDataFrame
#'
#' @return Dataframe. Indivduals with their coordinates associated.
#' @export
#' @examples
#'
#' data(dataset_map)
#'
#' ind <- simulate_ind(map_obj = dataset_map,
#' crs = 2154)
#'
#' head(ind)
#'
simulate_ind <- function(map_obj, crs){
# Function checks
assert_that(inherits(map_obj, "sf"))
if (!all(c("density_m") %in% names(map_obj))) {stop("map_obj must contain `density_m` column. Verify your column names.")}
assert_that(is.numeric(map_obj$density_m))
# Function
#st_make_grid
# Create grid
grid <- map_obj %>%
st_centroid() %>%
mutate(X = st_coordinates(.)[,1],
Y = st_coordinates(.)[,2]) %>%
as.data.frame() %>%
select("X","Y","density_m")
# Convert in grid class
coordinates(grid) <- ~ X + Y
proj4string(grid) <- CRS(st_crs(crs)$proj4string)
gridded(grid) <- TRUE
X_grid <- maptools::as.im.SpatialGridDataFrame(grid)
# Inhomogenous Poisson point process
ppp <- spatstat.core::rpoispp(lambda = X_grid, drop = TRUE)
sim_ind <- data.frame(x = ppp$x, y = ppp$y)
# Possibility to add group size
sim_ind <- sim_ind %>%
mutate(size = 1)
return(sim_ind)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.