2016-10-11 33 views
2

我有一個100 +文件的光柵堆棧。我想從每個文件中提取特定經緯度位置的值。這給了我一個Lat-Long組合值的列表。如何根據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


測試從評論的解決方案:

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") 

轉< -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) 
+0

光柵堆棧你能提供的輸入數據的例子嗎? – jdobres

+0

我已添加。 – maximusdooku

回答

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 
1

您可以在raster函數庫中使用extract函數。首先閱讀您的數據框並選擇lon,lat列。比方說,你有dataframedatpstack

loc <- dat[,c("long", "lat")] 
ext <- extract(pstack, loc) 
new_d <- cbind(dat, ext) # bind the extracted values back to the previous dataframe 
+0

您可以檢查我測試過的解決方案嗎?我得到一個錯誤:使用方法錯誤(「extract_」): 對'c'('RasterStack','Raster','RasterStackBrick','BasicRaster')對象應用'extract_'沒有適用的方法「 – maximusdooku

+0

你確定你是從'柵格堆棧'中提取'lon,lat'座標嗎?你還在犯錯嗎? –