IMMM-SFA / gamut

An R package to identify multi-sector teleconnection complexity
https://immm-sfa.github.io/gamut/
Other
0 stars 3 forks source link

Change raster processing functionality #64

Closed crvernon closed 4 years ago

crvernon commented 4 years ago

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')
}
KristianNelson commented 4 years ago

Raster processing speed increase dramatically after switching to single watershed analysis.