R 在缓冲区内有条件地从光栅提取
我有一个光栅,它存储的值从1到10 如何提取缓冲区中每个值的像素数并求和?因此,在指定大小的缓冲区内,我有值为1、2等的像素总数R 在缓冲区内有条件地从光栅提取,r,gis,R,Gis,我有一个光栅,它存储的值从1到10 如何提取缓冲区中每个值的像素数并求和?因此,在指定大小的缓冲区内,我有值为1、2等的像素总数 los <- function(x, cent, buff) { xmin <- x@extent@xmin xmax <- x@extent@xmax ymin <- x@extent@ymin ymax <- x@extent@ymax cent2 <- filter(cent, (L
los <- function(x, cent, buff) {
xmin <- x@extent@xmin
xmax <- x@extent@xmax
ymin <- x@extent@ymin
ymax <- x@extent@ymax
cent2 <- filter(cent, (Long<=xmax & Long>=xmin), (Lat<=ymax &
Lat>=ymin))
coordinates(cent) <- ~Long+Lat
buffer <- extract(x, cent, buffer=buff, fun=???,
df=TRUE)
return(buffer)
}
提问时,请提供一些示例数据
library(raster)
r <- raster(ncol=36, nrow=18, vals=sample(5, 36*18, replace=TRUE))
xy <- cbind(-50, seq(-80, 80, by=20))[1:3,]
或
您可以不使用过滤器,而使用美分
maxvalue <- maxValue(r)
e <- extract(r, xy[1:3,], buffer=1000000, fun=function(x) tabulate(x, maxvalue))
matrix(e, ncol=maxvalue, byrow=T)
maxvalue <- maxValue(r)
e <- extract(r, xy[1:3,], buffer=1000000)
t(sapply(e, function(x) tabulate(x, maxvalue)))
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 3 5 5 6
#[2,] 4 2 0 0 0
#[3,] 1 2 0 1 0