我有一个带有100+文件的光栅堆栈。我想从每个文件中提取特定长的位置的值。这给了我一个Lat长组合的值列表。
plist <- list.files(pattern = "\\.tif$", include.dirs = TRUE)
pstack <- stack(plist)
#levelplot(pstack)
for (i in 1:length(plist))
t[i]=extract(pstack[[i]], 35,-90)当我在一个单独的文件/dataframe中拥有长时间的位置时,我如何对数千个位置这样做。在最后一个列表中,我也希望保留一个位置ID:
Lat Long LocID
35 -90 001
35 -95 221
30 -95.4 226
31.5 - 90 776我的最后一个目标是获得这类数据:
Lat Long LocID value
35 -90 001 0.5
35 -95 221 1.4
30 -95.4 226 2.5
31.5 - 90 776 4.5虽然如果不可能保存LocID,那也没关系。
其中一个文件:conus.tif?dl=0
从评论中测试解决方案:
latlong<-structure(list(lon = c(-71.506667, -71.506667, -71.506667, -71.215278,
-71.215278, -71.215278, -71.215278, -71.215278, -71.215278, -71.215278
), lat = c(42.8575, 42.8575, 42.8575, 42.568056, 42.568056, 42.568056,
42.568056, 42.568056, 42.568056, 42.568056)), .Names = c("lon",
"lat"), row.names = c(NA, 10L), class = "data.frame")ext<-extract(pstack,latlong)给出
UseMethod中的错误(“extract_”):对于“c”类对象(‘RasterStack’、'Raster‘、'RasterStackBrick’、‘BasicRaster’)的对象,没有适用的extract_方法
更新#2:
这个错误是因为它与另一个包冲突。这样做是可行的:
raster::extract(pstack,latlong)发布于 2016-10-12 19:10:19
您可以在extract库中使用raster函数。首先在数据帧中读取,然后选择lon、lat列。假设你有dataframe dat和pstack光栅堆栈
loc <- dat[,c("long", "lat")]
ext <- extract(pstack, loc)
new_d <- cbind(dat, ext) # bind the extracted values back to the previous dataframe发布于 2016-10-12 00:41:06
我通常不处理这类数据,但这样如何:
library(sp)
library(raster)
library(rgdal)
# coordinate data
coords <- read.table(text = 'Lat Long LocID
35 -90 001
35 -95 221
30 -95.4 226
31.5 -90 776', header = T)
# list of all files
plist <- c('~/Downloads/new_conus.tif', '~/Downloads/new_conus copy.tif')
# image stack
data.images <- stack(plist)
# make a master data frame containing all necessary data
data.master <- data.frame(file = rep(plist, each = nrow(coords)), file.id = rep(1:length(plist), each = nrow(coords)), coords)此时,我们有一个主数据框架,如下所示:
file file.id Lat Long LocID
1 ~/Downloads/new_conus.tif 1 35.0 -90.0 1
2 ~/Downloads/new_conus.tif 1 35.0 -95.0 221
3 ~/Downloads/new_conus.tif 1 30.0 -95.4 226
4 ~/Downloads/new_conus.tif 1 31.5 -90.0 776
5 ~/Downloads/new_conus copy.tif 2 35.0 -90.0 1
6 ~/Downloads/new_conus copy.tif 2 35.0 -95.0 221
7 ~/Downloads/new_conus copy.tif 2 30.0 -95.4 226
8 ~/Downloads/new_conus copy.tif 2 31.5 -90.0 776现在,我们只提取与数据帧每一行中的数据对应的值:
# extract values for each row in the master data frame
data.master$value <- NA
for (i in 1:nrow(data.master)) {
data.master$value[i] <- with(data.master, extract(data.images[[file.id[i]]], Lat[i], Long[i]))
}
file file.id Lat Long LocID value
1 ~/Downloads/new_conus.tif 1 35.0 -90.0 1 255
2 ~/Downloads/new_conus.tif 1 35.0 -95.0 221 255
3 ~/Downloads/new_conus.tif 1 30.0 -95.4 226 259
4 ~/Downloads/new_conus.tif 1 31.5 -90.0 776 249
5 ~/Downloads/new_conus copy.tif 2 35.0 -90.0 1 255
6 ~/Downloads/new_conus copy.tif 2 35.0 -95.0 221 255
7 ~/Downloads/new_conus copy.tif 2 30.0 -95.4 226 259
8 ~/Downloads/new_conus copy.tif 2 31.5 -90.0 776 249https://stackoverflow.com/questions/39988415
复制相似问题