## ----sdm15a------------------------------------------------------------------- library(predicts) # get the predictors filename f1 <- system.file("ex/bio.tif", package="predicts") f2 <- system.file("ex/biome.tif", package="predicts") r <- rast(c(f1, f2)) # select 5000 random points # set seed to assure that the examples will always # have the same random sample. set.seed(1963) bg <- spatSample(r, 5000, "random", na.rm=TRUE, as.points=TRUE) ## ----sdm15, fig.width=9, fig.height=6----------------------------------------- plot(r, 1) points(bg, cex=0.5) ## ----------------------------------------------------------------------------- e <- ext(-80, -53, -39, -22) bg2 <- spatSample(r, 500, "random", na.rm=TRUE, as.points=TRUE, ext=e) plot(r, 1) lines(e, col="red", lwd=2) points(bg2, cex=0.5) ## ----sdm16a------------------------------------------------------------------- acfile <- file.path(system.file(package="predicts"), "ex/acaule.rds") ac <- readRDS(acfile) ## ----sdm17-------------------------------------------------------------------- # circles with a radius of 50 km x <- buffer(ac, 50000) pol <- aggregate(x) ## ----sdm19A------------------------------------------------------------------- # sample randomly from all circles set.seed(999) samp1 <- spatSample(pol, 500, "random") ## ----sdm19B------------------------------------------------------------------- pcells <- cells(r, samp1) # remote duplicates pcells <- unique(pcells[,2]) # back to coordinates xy <- xyFromCell(r, pcells) ## ----sdm20, fig.width=9, fig.height=6----------------------------------------- plot(pol, axes=TRUE) points(samp1, pch="+", cex=.5) points(xy, cex=0.75, pch=20, col='blue') ## ----sdm21a------------------------------------------------------------------- spxy <- vect(xy, crs="+proj=longlat +datum=WGS84") xyInside <- intersect(spxy, x) ## ----sdm21b, fig.width=9, fig.height=6---------------------------------------- # make a new, empty, smaller raster m <- crop(rast(r[[1]]), ext(x)+1) # extract cell numbers for the circles v <- cells(m, x) # get unique cell numbers from which you could sample v <- unique(v[,"cell"]) head(v) # to display the results m[v] <- 1 plot(m) lines(x)