【发布时间】:2019-09-05 12:22:16
【问题描述】:
我想获取随机坐标(pts)邻域(例如buffer=6米)的值(像素值)、坐标(x和y)和属性(status) ),使用光栅包中的提取功能。我尝试在data.frame 中组织结果,但没有成功。
library(raster)
#create some GeoTIFF rasters
r <- raster(ncol=10, nrow=10)
s <- stack(lapply(1:8, function(i) setValues(r, runif(ncell(r)))))
f1 <- file.path(tempdir(), "sl1.tif")
f2 <- file.path(tempdir(), "sl2.tif")
writeRaster(s[[1:4]], f1, overwrite=TRUE)
writeRaster(s[[5:8]], f2, overwrite=TRUE)
# 10 random points in the rasters
set.seed(5)
pts <- sampleRandom(s[[1]], 10, xy=TRUE)[,1:2]
status<-c(rep(c("A","B"),5))
pts<-as.data.frame(cbind(pts,status))
i<-c(1,2)
pts[ , i]<-apply(pts[ , i], 2,
function(x) as.numeric(as.character(x)))
#read all rasters
f <- c(f1, f2)
ras <- lapply(f, brick)
# extract raster values in 10 random coordinates and 6 meters around and organize the results
RES<-NULL
for(i in 1:length(ras)){
value <- raster::extract(ras[[i]],pts[,1:2], buffer=6)
RES<-rbind(RES,cbind(pts,coordinates(value),value)) #create a data frame of the results
}
RES
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 10, 4
我当然有不同的行数!!我想创建一个带有随机坐标的最终数据帧输出(pts 中的 xy)、邻域点的 xy(6m 缓冲区周围的 x2 和 y2 像素坐标)、状态(pts 状态的重复,我认为邻域与pts父坐标的状态相同),每个层的值如下:
x y x2 y2 status sl1.1 sl1.2 sl1.3 sl1.4 ...
1 -162 45 -165 48 A 0.47991386 0.04220410 0.79925156 0.04536868 0.47991386 ...
...
【问题讨论】: