# \donttest{
# check if R can run the distance functions
aux <- c(
length(suppressWarnings(packageDescription("raster"))),
length(suppressWarnings(packageDescription("gdistance"))),
length(suppressWarnings(packageDescription("sp"))),
length(suppressWarnings(packageDescription("terra"))))
missing.packages <- sapply(aux, function(x) x == 1)
if (any(missing.packages)) {
message("Sorry, this function requires packages '",
paste(c("raster", "gdistance", "sp", "terra")[missing.packages], collapse = "', '"),
"' to operate. Please install ", ifelse(sum(missing.packages) > 1, "them", "it"),
" before proceeding.")
} else {
# move to a temporary directory
old.wd <- getwd()
setwd(tempdir())
# Fetch location of actel's example files
aux <- system.file(package = "actel")[1]
# create a temporary spatial.csv file
file.copy(paste0(aux, "/example_spatial.csv"), "spatial.csv")
# import the shape file and use the spatial.csv
# to check the extents.
x <- shapeToRaster(shape = paste0(aux, "/example_shapefile.shp"),
coord.x = "x", coord.y = "y", size = 20)
raster::plot(x)
# Build the transition layer
t.layer <- transitionLayer(x)
# compile the distances matrix. Columns x and y in the spatial dataframe
# contain the coordinates of the stations and release sites.
distancesMatrix(t.layer, coord.x = 'x', coord.y = 'y')
# return to original directory
setwd(old.wd)
rm(old.wd)
}
rm(aux, missing.packages)
# }
Run the code above in your browser using DataLab