This function examines which points fall within a raster cell with data (not NA). Returns TRUE for points falling in a raster cell with data, and FALSE otherwise.

point_in_cell(locs = NULL, ras = NULL, layer = 1)

Arguments

locs

An sf::sf() or terra::SpatVector() object with point coordinates, e.g. as generated from locs2sf() or locs2vect().

ras

terra::SpatRaster() object

layer

Integer. Raster layer to use for comparing with point locations (default = 1).

Value

A logical vector.

Examples

locs <- data.frame(lon = c(1, 2, 1, 2), lat = c(1, 1, 2, 3))
locs.sf <- locs2sf(locs)
library(terra)
#> terra 1.7.79
ras <- rast(nrows = 2, ncols = 2, xmin = 0.5, xmax = 3.5, ymin = 0.5, ymax = 3.5,
 resolution = 1, vals = c(NA, 1, 1, NA, NA, 1, NA, 1, 1))
occmap(locs.sf, ras, pcol = "black", psize = 3)


point_in_cell(locs.sf, ras)
#> [1]  TRUE FALSE  TRUE FALSE

# adding column to original point data
locs.sf$inras <- point_in_cell(locs.sf, ras)
locs.sf
#> Simple feature collection with 4 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 1 xmax: 2 ymax: 3
#> Geodetic CRS:  WGS 84
#>      geometry inras
#> 1 POINT (1 1)  TRUE
#> 2 POINT (2 1) FALSE
#> 3 POINT (1 2)  TRUE
#> 4 POINT (2 3) FALSE