Code
library(terra)
library(sf)library(terra)
library(sf)Climate data were obtained from the CHELSA-Bioclim v2.1 database at a spatial resolution of 1 km (Brun et al. 2022). We extracted mean annual temperature (BIO1, in °C) and mean annual precipitation (BIO12, in mm/year), both calculated as long-term averages over the 1981–2010 reference period.
if (!require(Rchelsa)) {
remotes::install_git("https://gitlabext.wsl.ch/karger/rchelsa.git")
library(Rchelsa)
}
clim <- lapply(
c("bio01", "bio12"),
function(rs) {
getChelsa(
rs,
extent = ext(c(110, 130, 0, 20)),
date = "1981-2010", dataset = "chelsa-bioclim"
)
}
) |>
rast()
names(clim) <- c("mean_temperature", "mean_precipitation")
if (!dir.exists("data/chelsa")) dir.create("data/chelsa")
writeRaster(clim, file = "data/chelsa/climate_variables.tif", overwrite = TRUE)"data/chelsa/climate_variables.tif" |>
rast() |>
plot()