if (FALSE) {
# you can run these examples if you have the 'terra' package installed
require(terra)
# get an example raster map:
rst <- terra::rast(system.file("ex/elev.tif", package = "terra"))
rst <- terra::aggregate(rst, 10)
plot(rst)
# generate some random presence points within it:
set.seed(8)
presences <- terra::spatSample(as.polygons(ext(rst)), 10)
plot(presences, add = TRUE)
# use 'ptsrast2obspred' on this points + raster data:
# without removing duplicates (the default):
obspred <- ptsrast2obspred(pts = crds(presences), rst = rst)
obspred
nrow(obspred) # you get as many 'obs' as pixels + additional points per pixel
sum(obspred$obs) # as many presences as points that overlay 'pred'
# with removal of duplicates:
obspred_rmdup <- ptsrast2obspred(pts = crds(presences), rst = rst[[1]],
rm.dup = TRUE) # you get as many 'obs' as pixels
obspred_rmdup
nrow(obspred_rmdup) # you get as many 'obs' as pixels
sum(obspred_rmdup$obs) # as many presences as pixels that contain (one or more) points
}
Run the code above in your browser using DataLab