I am working with a wildlife camera grid. I would like to extract values from SpatRaster's over the camera points, but from the field of view. To simulate a cameras field of view, I would like to create a cone polygon that is pointed in the listed bearings of the cameras in the grid.
Minimal working example:
library(terra)
f <-system.file("ex/elev.tif", package="terra")
r <-rast(f)
WildCams <- spatSample(r,10, "regular", as.points=TRUE)
bearings <- sample.int(180,10)
WildCams$bearings <- bearings
WildCams_buf <- buffer(WildCams,5000)
WildCams_sf <- sf::st_as_sf(WildCams)
The following answer provides an example of the type of result I would like to create in R for multiple points. This answer provides an example for creating a cone polygon for a single point to a portion of sphere.
I started working from the example to create a cone around a single point but do not know how to extend this to a grid of points. The following does not work but builds on this example:
# Attempt 1: Create a cone polygon
theta <- 120 # my angle of interest
radius <- 500 # my radius length of interest (in meters)
WildCams_buf_sf <- sf::st_as_sf(WildCams_buf)
theta = pi*theta/180
cone1 <- vect(WildCams_buf_sf)
conegrid = rast(cone1, nrow=nrow(r), ncol=ncol(r))
conegrid2 = rasterize(cone, conegrid, field="bearings")
conegrid3 <- project(conegrid2, crs(r), res=res(r))
plot(conegrid3)
xy = crds(conegrid3)
xy[,1] = xy[,1] - st_coordinates(WildCams_sf)[1]
xy[,2] = xy[,2] - st_coordinates(WildCams_sf)[2]
v = (atan2(xy[,1], xy[,2]) + theta)
conegrid3[] = (1+sin(v))/2
conegrid4 = mask(conegrid3, vect(WildCams_sfbuf ))
plot(conegrid4 >0.05)
Is there a function in terra or another package that creates a field of view cone from a set of points that can individually be set to sample in the orientation of the camera's bearing? I would like to extract SpatRaster values from the field of view cones.
sample data
assuming we have a data.frame of camera's with ther lon/lat position, camera bearing, camera angle width and camera distance.
code