由于使用的是光栅堆栈,因此所有单元格都应该具有相同的面积。在这种情况下,我认为根本不需要使用多边形。(注意,我对示例数据做了一些调整。)
library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
s <- stack(r, r + 50, r - 50, r + 100)
# Create a new raster stack with results of a logical test
s2 <- s < 300
# Create a raster indicating which cells of the new stack
# have values that are all TRUE
r2 <- sum(s2) == length(unstack(s2))
# Multiply by the area of a single cell
r3 <- r2 * area(r2)[1]
# Sum the area for all raster values
sum(values(r3), na.rm = TRUE)
## 124800
如果您想使用多边形,并且光栅不太大,请将堆栈转换为
SpatialPolygonsDataFrame
应该很快。这里有一个类似的方法,可以得到相同的结果:
# Create a new raster stack with results of a logical test
s2 <- s < 300
# Convert to sp object
spdf <- as(s2, "SpatialPolygonsDataFrame")
# Index to the rows/features where the values in s2 were all TRUE
spdf2 <- spdf[which(rowSums(spdf@data) == length(unstack(s))), ]
rgeos::gArea(spdf2)
## 124800