Raster processing for attaining zonal statistics is slow using the mask and extract method currently employed to get a variety count. Instead implement exactextractr method similar to:
library(raster)
library(sf)
library(exactextractr)
library(dplyr)
test_zonal <- function(city, polygon_file, raster_file){
# load raster
cdl <- raster::raster(raster_file)
# get crs from raster
raster_crs <- raster::crs(cdl)
# load watershed polygons
wshed <- sf::read_sf(polygon_file)
# get the basin polygon; transform to crs of raster; dissolve geometries of mulitpart into one feature
wshed_subset <- wshed[wshed$City_Name == city, ] %>%
sf::st_transform(crs = raster_crs) %>%
sf::st_union()
# get the unique number of raster cell values intersecting the polygon data
wshed_subset$n_cdl <- exactextractr::exact_extract(cdl, wshed_subset, 'variety')
}
Raster processing for attaining zonal statistics is slow using the
mask
andextract
method currently employed to get a variety count. Instead implementexactextractr
method similar to: