zonal_stats_ISEA3HEXAGON_grid: Get zonal stats binned in a hexagon grid

Description Usage Arguments Value Examples

View source: R/zonal_stats_ISEA3HEXAGON_grid.R

Description

Given a geometry (multi) polygon object, create a ISEA-3-HEXAGON grid at the given resolution covering the geometry, apply the zonal statistics function(s) to each feature of the grid feature collection, and return the grid with calculated properties. Optionally clipping the grid by the geometry object, and saving to file.

Usage

1
2
zonal_stats_ISEA3HEXAGON_grid(fun, polygon_object, raster_object,
  grid_res = 11, remove_na = T, clip = F, out_path = F)

Arguments

fun

An optional function or character vector, as described in exact_extract exactextractr

polygon_object

A sf::sf object

raster_object

A raster::raster object

grid_res

The zoom resolution of the ISEA-3-HEXAGON grid, see dggridR::dgconstruct

clip

Boolean, clip by the polygon? Note this can be slow

out_path

Path to write the grid using sf::st_write

Value

A sf object representing the ISEA-3-HEXAGON grid covering the geometry with zonal statistics as defined by fun

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
polygon_path <- system.file("ext_data", "boundary-vectors", "sweden.shp", package="vspt")
polygon_object <- sf::st_as_sf(sf::st_read(polygon_path))
raster_path <- system.file("ext_data/predicted-spp-occurence-rasters/proj_rcp45_2020_Pinus.mugo_ensemble.grd", package="vspt")
raster_object <- raster::raster(raster_path, band=6, crs="+proj=longlat +datum=WGS84")
raster_object
# Make stack with area
raster_object <- raster::stack(raster_object, raster::area(raster_object))
out_path <- file.path(system.file("ext_data/predicted-spp-occurence-vectors", package="vspt"), "SWE_pinus-mugo_rcp45-2020_ensemble.geojson")
# Define function to apply
area_weighted_mean <- function(values, coverage_frac){return(weighted.mean(values[,1], values[,2]*coverage_frac, na.rm=TRUE))}
fun = area_weighted_mean
out <- zonal_stats_ISEA3HEXAGON_grid(fun, polygon_object,raster_object,grid_res = 11, remove_na = T, clip=F, out_path=out_path)
names(out)
if(require(tmap)){
  tmap_mode("view")
  tm_shape(out) + tm_fill("zstat_Pinus.mugo_EMcaByROC_mergedAlgo_mergedRun_mergedData", palette = sf::sf.colors(3), alpha=0.7, colorNA=NULL)
  }else{plot(out)}

Vizzuality/species-distribution-modelling documentation built on Oct. 31, 2019, 1:10 a.m.