I am trying to crop and mask raster data and convert the raster data to a vector and to an sf object with terra. However, I always get the following error message: Error in UseMethod ("st_as_sf")
not applicable method for 'st_as_sf'
applied to object of class "SpatVector"
. I would appreciate any help or advice to overcome this hurdle.
My code looks as follows
packs <- list("sf", "tidyverse", "terra")
lapply(packs, require, character.only=TRUE)
pop_count <- rast(raster.path)
adm_ner_sf <- st_read(shp.path)
pop_afg_sf <- pop_count %>%
crop(vect(adm_afg_sf)) %>%
mask(vect(adm_afg_sf)) %>%
as.points() %>%
st_as_sf() %>%
rename(pop_count = gpw_v4_population_count_rev11_2010_2pt5_min)
I am using SEDAC population density raster data (Formal class SpatRaster), which I am trying to limit to the shape of Afghanistan, by using an Afghanistan shape file (adm_afg_sf).
Any feedback would be greatly appeciated.