【问题标题】:How can I extract data from a raster stack based on a list of lat long?如何根据经纬度列表从栅格堆栈中提取数据?
【发布时间】:2016-10-11 23:42:07
【问题描述】:

我有一个包含 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)

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


从 cmets 测试解决方案:

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)

【问题讨论】:

  • 你能提供一个输入数据的例子吗?

标签: r raster


【解决方案1】:

您可以在raster 库中使用extract 函数。首先,您读取数据框并选择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

【讨论】:

  • 能否请您检查一下我测试过的解决方案。我收到一个错误:UseMethod("extract_") 中的错误:没有适用于 'extract_' 的方法应用于“c('RasterStack', 'Raster', 'RasterStackBrick', 'BasicRaster')”类的对象跨度>
  • 您确定要从raster stack 中提取lon, lat 坐标吗?你还在报错吗?
【解决方案2】:

我通常不使用这种类型的数据,但是这个怎么样:

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

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    相关资源
    最近更新 更多