R:使用解压缩来提取下载到临时文件的光栅

R: Use unzip to extract raster that is downloaded to a temporary file

我正在尝试将 worldclim 网格气候数据下载到临时文件,然后将其作为栅格打开。我不想将文件保存到我的计算机。我可以使用仅包含一个网格化气候数据集的 zipfiles 来做到这一点,但是当有很多时我似乎无法让它工作。

提前致谢。

temp <- tempfile()
#download worldclim climate data
download.file("http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/tmean_10m_esri.zip",temp, mode="wb")
unzip(temp,list=TRUE)#list files
#unzip and make raster, may need this as seperate steps
meanT<- raster(unzip(paste0(temp,"/tmean/tmean_9")))
unlink(temp)

这是一个解决方案;将下载的临时文件解压到临时目录。

temp <- tempfile()
tempd <- tempdir()

#download worldclim climate data
download.file("http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/tmean_10m_esri.zip",temp, mode="wb")

unzip(temp, exdir=tempd)
tmean_raster <- raster(file.path(tempd, "tmean/tmean_9"))
unlink(tempd)

获取这些数据的直接方法是:

library(raster)
wc <- getData('worldclim', var='tmean', res=10)