I have some difficulty if I want to also get the coordinates inside the buffer corresponding for each value in my result of the extract
. In my example:
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)
Obviously, because I have a list that represents each buffer and the famous solution:
ee <- t(data.frame(result))
rownames(ee) <- NULL
data.frame(xy, ee)
It doesn't work because I can recovery only xy coordinates and not each xy of the pixel values inside the buffer too.
What's the solution for I have an output data frame with the coordinates of xy and xy of buffer too for each value extracted like:
## 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.