tests/testthat/test-obtainCovariate.R

testthat::test_that('obtainCovariate can correctly obtain the covariate layer, and transform it to the desired projection', {

 skip_on_cran()
 library(R.utils)

  covname <- "tavg"
  countries <- c('Norway', 'Sweden')
  projection <- '+proj=utm +zone=59 +south'
  #path <- './tests/testthat'
  path <- './wc2.1_country'
  dir.create(path)

  if(dir.exists(path)) {

    ##If taking too long//servers down

  try(cov <- withTimeout(
    obtainCovariate(covname, res = '10', type = 'worldclim',
                    projection, path), timeout = 60, onTimeout = 'silent'))

  if ('cov' %in% ls()) {

  expect_equal(class(cov)[1], 'SpatRaster')
  expect_identical(fm_crs(cov)[2], fm_crs(projection)[2])


  }
  #Change CRS
  projection2 <- 'EPSG:4326'

  try(cov2 <-  withTimeout(obtainCovariate(covname, res = '10', type = 'worldclim',
                         projection2, path), timeout = 60, onTimeout = 'silent'))

  if ('cov2' %in% ls()) {

  expect_equal(class(cov2)[1], 'SpatRaster')
  expect_identical(fm_crs(cov2)[2], fm_crs(projection2)[2])
  }

  ##Try landcover

  try(cov3 <-  withTimeout(obtainCovariate('grassland', res = '10', type = 'landcover',
                                           projection2, path), timeout = 60, onTimeout = 'silent'))

  if ('cov3' %in% ls()) {

  expect_equal(class(cov3)[1], 'SpatRaster')
  expect_equal(names(cov3)[1], 'grassland')

  }

  unlink(path, recursive = TRUE)



  }

})

Try the intSDM package in your browser

Any scripts or data that you put into this service are public.

intSDM documentation built on Sept. 9, 2025, 5:59 p.m.