I am using R.
I have a polygon v that covers multiple raster cells in raster z.
How could I calculate what proportion of the polygon is covered by each category in the raster?
I read a solution on Unexpected behaviour of extract function in the raster package which suggested rasterizing the polygon, polygonizing the raster, and then using terra::extract. This may work for the reproducible example here, but I'm looking for a solution that will scale to work with 2500 polygons and a raster with 100,000 rows and 100,000 columns.
I have assumed that the above solution would not work well with such a large dataset.
library(terra)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
v <- v[1,]
z <- rast(v, resolution=.09, names="test")
values(z) <- 1:ncell(z)
levels(z) <- 0:ncell(z)
plot(z)
plot(v, add = T)
exactextractr
package? Its fast and gives you the area of partial grid overlaps. I'm using it on a 60,000x60,000 raster (with two layers) and its not too painful, your pain may vary depending on the complexity of your polygons and the power of your computer (but this is trivially parallelizable over polygons anyway). Doesn't seem to yet work withterra
so tryraster
?raster
butterra
is supported and often much faster.exact_extract
doesn't work with the vector polygon classes fromterra
, it can extract from the raster classes ofterra
buty
has to be ansf
orsp
class polygonal object. I don't know whyterra
had to define yet another class of spatial data features but oh well..