也使用xy和缓冲区坐标xy提取栅格的值

时间:2020-04-06 13:27:53

标签: r raster r-raster

如果我还想获取extract结果中每个值所对应的缓冲区内的坐标,就会遇到一些困难。在我的示例中:

library(raster)

#Simulation of raster and some coordinates
r <- raster(ncol=36, nrow=18)
r[] <- 1:ncell(r)
xy <- cbind(-50, seq(-80, 80, by=20))

#Get coordinates of raster cells
v <- extract(x = r, 
                 y = xy, 
                 buffer=100,
                 df=TRUE)
cbind(v,coordinates(r))

Error in cbind(v, coordinates(r)) : 
  number of rows of matrices must match (see arg 2)

显然,因为我有一个代表每个缓冲区的列表和著名的解决方案:

ee <- t(data.frame(result))
rownames(ee) <- NULL
data.frame(xy, ee)

这是行不通的,因为我只能恢复xy坐标,而不能恢复缓冲区内每个像素值的xy。

对于每个提取的值,我都有一个输出数据帧,其中包含xy和xy的坐标,这是什么解决方案?


##         cells layer x   y   x_buffer y_buffer
##  [1,]   626   626   -45 -85 -44     -84
...

1 个答案:

答案 0 :(得分:0)

# get xy from buffer cells
cell <- extract(r, xy, buffer=100, cellnumbers=T)
xy_b <- xyFromCell(r, do.call(rbind, cell)[,1])
res<-NULL
res <- do.call(rbind, cell)
RES<-cbind(res,xy,xy_b)
colnames(RES)<-c("cells","layer","x","y","x_buffer","y_buffer")
head(RES)
     cells layer    x y x_buffer y_buffer
[1,]  4909  4909 -150 0   -149.4      0.9
[2,]  4937  4937  -50 0    -48.6      0.9
[3,]  4964  4964   50 0     48.6      0.9
[4,]  4992  4992  150 0    149.4      0.9