R/cleanDepth.R
cleanDepth.Rdfunction to remove or flag coordinates on land, coordinates which are deeper than the known bathymetry at a given xy location, and/or coordinates outside of a specified depth range when cleaning coordinates for species in marine environments
cleanDepth(
occs,
bathy,
land_poly,
depth_range,
flag = FALSE,
bottom_correct = FALSE
)dataframe containing columns named "longitude", "latitude", and "depth," where depth values are positive
a spatRaster of bathymetry in the same units as the occurrence depth, where values are negative
optional polygon of continents in same projection as occurrence data
optional range of depth values in the form of c(upper, lower) in which all points should fall between. depth value should be positive, as in the case of the occs
default is F, if T, points are not removed, but flagged if they intersect, and a column is added to the output indicating what points have been flagged
default is F, but if T, points are not removed, and a column is added to the output displaying the bathymetry value at the flagged point for corrective purposes
an object of class dataframe containing cleaned or flagged occurrences
assumes depths are listed as positive in the occs and depth_range, while the bathymetry layer lists them as negative when below the sea surface
library(terra)
library(dplyr)
# Create sample bathymetry
r1 <- rast(ncol=10, nrow=10)
values(r1) <- c(-100:0)
#> Warning: [setValues] values is larger than the size of cells
# Create test occurrences
set.seed(0)
longitude <- sample(ext(r1)[1]:ext(r1)[2], size = 10, replace = FALSE)
set.seed(0)
latitude <- sample(ext(r1)[3]:ext(r1)[4], size = 10, replace = FALSE)
set.seed(0)
depth <- sample(1:100, size = 10, replace = TRUE)
occs <- data.frame(longitude, latitude, depth)
#Heres the function
result <- cleanDepth(bathy = r1, occs = occs)
#> [1] "removed 5 points intersecting bathymetry"