如何根据lat long列表从栅格堆栈中提取数据?

时间:2016-10-11 23:42:08

标签: r raster

我有一个包含100多个文件的光栅堆栈。我想从特定纬度位置的每个文件中提取值。这给了我一个Lat-Long组合的值列表。

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)

当我在单独的文件/数据框中具有lat-long位置时,如何在数千个位置执行此操作。我想在最终列表中保留一个位置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,那也很好。

其中一个文件:https://www.dropbox.com/s/ank4uxjbjk3chaz/new_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")

分机&LT; -extract(pstack的,经纬度)

给出

Error in UseMethod("extract_") : 
  no applicable method for 'extract_' applied to an object of class "c('RasterStack', 'Raster', 'RasterStackBrick', 'BasicRaster')"

更新#2:

该错误是因为它与另一个包冲突。这有效:

raster::extract(pstack,latlong)

2 个答案:

答案 0 :(得分:2)

您可以在extract库中使用raster功能。首先,您在数据框中读取并选择lonlat列。假设您有dataframe datpstack

的栅格堆栈
loc <- dat[,c("long", "lat")]
ext <- extract(pstack, loc)
new_d <- cbind(dat, ext) # bind the extracted values back to the previous dataframe

答案 1 :(得分:1)

我通常不会使用此类数据,但是如何:

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   249