也使用缓冲区坐标的 xy 和 xy 提取栅格值
Extracting value of raster with xy and xy of buffer coordinates too
如果我还想获得 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
...
# 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
如果我还想获得 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
...
# 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