# \donttest{
library(sf)
library(terra)
# read, project and subset 10 polygons
nc <- suppressWarnings(st_cast(st_read(system.file("shape/nc.shp",
package="sf")), "POLYGON"))
nc <- st_transform(nc, st_crs("ESRI:102008"))
nc.sub <- nc[sample(1:nrow(nc),10),]
# create 1000m reference raster, rasterize subset polygons
ref <- rast(ext(nc), resolution=1000)
rnc <- mask(rasterize(vect(nc.sub), field="CNTY_ID",
ref, background=9999), vect(nc))
crs(rnc) <- "ESRI:102008"
# Calculate distance to class 1 in rnc raster, plot results
ids <- nc.sub$CNTY_ID
rd <- rasterDistance(rnc, y=ids)
plot(rd)
plot( st_geometry(nc.sub), add=TRUE)
# }
Run the code above in your browser using DataLab