daisy
daisy

Reputation: 93

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

I am trying to download worldclim gridded climate data to a temporary file and then open it as a raster. I do not want to save the files to my computer. I can do this with zipfiles that contain only one gridded climate dataset but I can not seem to get this to work when there are many.

Thanks in advance.

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)

Upvotes: 1

Views: 1087

Answers (2)

Robert Hijmans
Robert Hijmans

Reputation: 47146

A direct way to get these data is:

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

Upvotes: 1

Remko Duursma
Remko Duursma

Reputation: 2821

Here is a solution; unzip the downloaded temporary file to a temporary directory.

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)

Upvotes: 2

Related Questions