R/hf_accCost.R

Defines functions hf_accCost

Documented in hf_accCost

#' Accumulated cost-distance
#'
#' Calculates the accumulated cost of travel from one or more points.
#'
#' @usage hf_accCost(x, points, fun = function(x) 1/mean(x))
#'
#' @param x A \code{RasterLayer} with velocity or efficiency values.
#' @param points A \code{POINT} object from package \code{sf} specifying locations
#' from which to calculate accumulated cost distance.
#' @param fun A function to calculate transition weights from the raster.
#'
#' @return A \code{RasterLayer} with accumulated travel costs.
#' @export
#'
#' @examples
#' \dontrun{
#'
#' library(raster)
#' library(rHike)
#' library(sf)
#'
#' dem <- raster(system.file("extdata/slc.tif", package = "rHike"))
#'
#' slope <- hf_slope(dem)
#'
#' velocity <- hf_velocity(slope, hf = "campbell", decile = 30)
#'
#' start_points <- st_sf(id = 1:3,
#'                       geometry = st_sfc(st_point(c(424350, 4514200)),
#'                                         st_point(c(426000, 4515000)),
#'                                         st_point(c(429000, 4516500)),
#'                                         crs = 26912))
#'
#' acc_cost <- hf_accCost(velocity, start_points)
#'
#' plot(acc_cost)
#' plot(st_geometry(start_points), color = "red", add = TRUE)
#'
#' }
#'
hf_accCost <- function(x, points, fun = function(x) 1/mean(x)){

  # generate transition object from friction surface
  transition <- gdistance::transition(x, fun, 8)

  # account for diagonal travel in Moore's neighborhood
  transition <- gdistance::geoCorrection(transition)

  gdistance::accCost(transition, fromCoords = sf::st_coordinates(points))

}
kbvernon/rHike documentation built on May 29, 2020, 7:22 p.m.