# How to calculate the area percentage of polygons overlapping with cells of raster using terra, R

I have a raster with 1 km x 1km cells and some Points, then create 600 m buffers for each point. Now I would like to calculate the area percentage of the buffers for each cell. Finally get a raster with cell values of those area percentage. For example, if the cells without buffer overlapped, the value for the cell is 0, and the maximum is 1. First, I convert my raster into 1km x 1km polygons, then create 650 m buffers for points.

Grids <- as.polygons(Har, dissolve=FALSE, values=FALSE)

poi_b <- buffer(vect(poi), width = 650) %>% project(crs(Grids))

• When asking a R question, please provide a minimal, reproducible, self-contained example, that is, create a few points and a raster with code, create the circles... Also, do you want to know the fraction of the cells covered, or the average (?) of the fraction of the overlapping circles covered by the cells? Commented Dec 19, 2023 at 17:34
• So sorry, I have added the description. Commented Dec 20, 2023 at 15:46

## 1 Answer

The exactextractr library returns the fractional intersection of the raster cells and can derive certain area weighted statistics.

Here is a quick worked example

Add libraries and create example data

``````library(terra)
library(sf)
library(exactextractr)

p <- st_read(system.file("shape/nc.shp", package="sf"))
r <- rast(ext(p), crs = crs(p), nrow=100, ncol=100)
r[] <- sample(1:20, ncell(r), replace=TRUE)
plot(r)
plot(st_geometry(p), add=TRUE)
``````

Extract data and look at head of first polygon in returned list. The structure of the returned object is a list, containing a data.frame, for each polygon.

``````e <- exact_extract(r, p)
head(e[[1]])
``````

Here we apply a function, using `lapply`, to the list that returns the median of pixels with >= 50% overlap with the given polygon

``````unlist(lapply(e, \(x) {
median(x[which(x\$coverage_fraction >= 0.50),]\$value)
}))
``````
• Thank you so much. But I am not familiar with the function of exact_extract and its results. For my question, I need to calculate the buffer area percentage overlap with the 1 km2 cells and get a raster data. Please fell free to check my example firgure. Commented Dec 20, 2023 at 16:10
• @WeiLiao it's unclear. Should the raster layer have the percentage overlap of all buffers or should you get one raster layer for each buffer? If you want one layer, dissolve the buffer features and get the percentage overlap with Jeffrey's recommended function. Also, check function documentation for getting the coordinates/ID for each cell for converting it back to raster Commented Dec 20, 2023 at 17:28
• The former. Please fell free to check my example figure. Commented Dec 21, 2023 at 1:14