1

I want to download temperature and precipitation data from WorldClim for a huge list of coordinates. I am working in R, and have a solution using getData() from raster(), but it's going to take forever for the giant list of coordinates I have. I posted a similar question over on stackoverflow in hopes I could streamline the looping. However, I think even if I can streamline the looping, this might take forever.

Does anyone have a suggestion for a more efficient way of doing this?

I've Googled a whole bunch, and can't find any solutions in R. I do have a reproducible working example:

# Download and merge 0.5 minute MAT/MAP data from WorldClim for a list of lon/lat coordinates
# This is based on https://emilypiche.github.io/BIO381/raster.html

# Make a dataframe with coordinates
coords <- data.frame(Lon = c(-83.63, 149.12), Lat=c(10.39,-35.31))

# Load package
library(raster)

# Make an empty dataframe for dumping data into
coords3 <- data.frame(Lon=integer(), Lat=integer(), MAT_10=integer(), MAP_MM=integer())

# Get WorldClim data for all the coordinates, and dump into coords 3
for(i in seq_along(coords$Lon)) {
  r <- getData("worldclim", var="bio", res=0.5, lon=coords[i,1], lat=coords[i,2]) # Download the tile containing the lat/lon
  r <- r[[c(1,12)]] # Reduce the layers in the RasterStack to just the variables we want to look at (MAT*10 and MAP_mm)
  names(r) <- c("MAT_10", "MAP_mm") # Rename the columns to something intelligible
  points <- SpatialPoints(na.omit(coords[i,1:2]), proj4string = r@crs) #give lon,lat to SpatialPoints
  values <- extract(r,points)
  coords2 <- cbind.data.frame(coords[i,1:2],values)
  coords3 <- rbind(coords3, coords2)
}

# Convert MAT*10 from WorldClim into MAT in Celcius
coords3$MAT_C <- coords3$MAT_10/10
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.