diff --git a/DESCRIPTION b/DESCRIPTION index 81c870d..10440c3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,7 +9,7 @@ Description: Builds landscape inputs for Canadian forest models from national disturbances. URL: https://github.com/PredictiveEcology/forestInputs BugReports: https://github.com/PredictiveEcology/forestInputs/issues -Version: 0.0.1.9001 +Version: 0.0.1.9002 Authors@R: c( person("Eliot J B", "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6914-8316")), @@ -34,7 +34,8 @@ Imports: utils Suggests: curl, - httr2, + googledrive, + raster, testthat (>= 3.0.0), withr Remotes: diff --git a/NEWS.md b/NEWS.md index 4c24db6..ddf8ecc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,13 @@ # forestInputs (development version) +* `prepInputsFireYear()` reads a fire year stored as text as the year. It gave factor codes 1, 2, ..., + so the map was entirely `NA` (#4). +* `prepInputsDisturbanceYear(polygon = )` no longer partially matches `polygonAgreement`, because + `polygonsBefore` shares the prefix; give the full name. +* Cached calls of `prepInputsFireYear()`, including the one inside `prepInputsStandAgeMap()`, run again + once after upgrading from LandR, because `Cache()` keys include the function's code. +* `inputsTable()`: the SCANFI stand age rows have `access = restricted` (their `url` is a shared Google + Drive folder); the public copies are in `mirror`. * `prepInputsDisturbanceYear()` takes regional settings, each defaulting to the current rule, so results do not change unless they are set (from Julie Turner's test in the southern interior BC mountains): `polygonsBefore` (before it an NFDB polygon counts without Pre-CanLaD; `1950` counts Pre-CanLaD alone), diff --git a/R/inputs.R b/R/inputs.R index a0563cf..da290ad 100644 --- a/R/inputs.R +++ b/R/inputs.R @@ -4,9 +4,12 @@ #' functions here read by default, with where each comes from. The columns follow the inputs #' table of hsaCEtool (`name`, `title`, `theme`, `url`, `path`, `target_file`, `archive`, #' `access`, `provider`, `terms`, `notes`), plus `year` (the year or years a row covers) and -#' `mirror` (a public copy of `url`, used when `url` cannot be reached). A `%d` in `url` and +#' `mirror` (a public copy of `url`; the SCANFI readers use it through [scanfiUrlRemap()]). A `%d` in `url` and #' `target_file` stands for the year, for inputs with one file per year. #' +#' `access` is `public` when anyone can download `url`, and `restricted` when `url` needs +#' permission (e.g., a shared Google Drive folder). +#' #' The function defaults read their URLs from this table, so a moved file is fixed here once. #' The table is `system.file("extdata", "inputs.csv", package = "forestInputs")`. #' diff --git a/R/scanfi-access.R b/R/scanfi-access.R index dc5a8d5..6228005 100644 --- a/R/scanfi-access.R +++ b/R/scanfi-access.R @@ -117,17 +117,3 @@ call. = FALSE) }) } - -## --------------------------------------------------------------------------- -## Messaging for SCANFI v3 access failures -## -## V3 is served directly by NRCan (no Google Drive layer), so a failure here is -## usually a network hiccup or an outage, not a permissions problem, and the -## guidance differs from .withSCANFIAccess() above. -## --------------------------------------------------------------------------- - -## Where else to get v3 if the https server is unreachable. -.scanfiV3FTPURL <- "ftp://ftp.maps.canada.ca/pub/nrcan_rncan/Forests_Foret/SCANFI/v3/" - -## Does this error look like the v3 https server refusing or dropping us? A bare status -## code is enough here because this is only ever applied to a known SCANFI v3 fetch. diff --git a/R/standAge.R b/R/standAge.R index b795f10..6772f50 100644 --- a/R/standAge.R +++ b/R/standAge.R @@ -332,7 +332,7 @@ prepInputsStandAgeMap <- function( #' #' @export #' -#' @examplesIf !isTRUE(as.logical(Sys.getenv("CI"))) +#' @examplesIf !isTRUE(as.logical(Sys.getenv("CI"))) && requireNamespace("withr", quietly = TRUE) #' ## NOTE: runs locally, skipped on CI -- downloads NFDB_poly.zip from #' ## cwfis.cfs.nrcan.gc.ca (same predicate as testthat::skip_on_ci()) #' withr::local_options(list( @@ -378,12 +378,6 @@ prepInputsFireYear <- function(..., rasterToMatch, fireField = "YEAR", earliestY if (!terra::same.crs(allFires, rasterToMatch)) { allFires <- terra::project(allFires, rasterToMatch) } - yr <- as.data.frame(allFires)[[fireField]] - if (!is.numeric(yr)) { - warning("Chosen fireField will be coerced to numeric") - allFires[[fireField]] <- as.numeric(as.factor(yr)) - } - ## fun = max to take the most recent fire year fireRas <- terra::rasterize(allFires, rasterToMatch, field = fireField, fun = max) v <- terra::values(fireRas, mat = FALSE) @@ -480,6 +474,13 @@ prepInputsFireYear <- function(..., rasterToMatch, fireField = "YEAR", earliestY ## the reason this isn't combined into one function is due to geometry issues in NFDB allFires <- allFires[terra::is.valid(allFires), ] ## drop invalid geometries + ## a year stored as text ("1995") is read as the year; as.factor() gave factor codes 1..k + yr <- as.data.frame(allFires)[[fireField]] + if (!is.numeric(yr)) { + warning("Chosen fireField will be coerced to numeric") + allFires[[fireField]] <- suppressWarnings(as.numeric(as.character(yr))) + } + ## rasterize(fun = max) keeps only the latest fire per pixel, which would hide an earlier fire ## from a caller whose data year is before it: drop the later fires first. if (is.finite(latestYear)) { @@ -504,7 +505,8 @@ prepInputsFireYear <- function(..., rasterToMatch, fireField = "YEAR", earliestY #' Replace stand age with time since last fire #' #' @param standAgeMap a raster layer stand age map -#' @param firePerimeters the earliest fire date to allow +#' @param firePerimeters a `SpatRaster` of fire years on the grid of `standAgeMap`, +#' e.g., from [prepInputsFireYear()] #' @template startTime #' #' @return a raster layer stand age map corrected for fires, with an attribute vector of pixel IDs diff --git a/inst/extdata/inputs.csv b/inst/extdata/inputs.csv index 09e982b..3dbf332 100644 --- a/inst/extdata/inputs.csv +++ b/inst/extdata/inputs.csv @@ -9,15 +9,15 @@ ntems_fire,NTEMS forest fire year 1985-2022,fire,1985-2022,https://opendata.nfis ntems_harvest,NTEMS forest harvest year 1985-2022,harvest,1985-2022,https://opendata.nfis.org/downloads/forest_change/CA_Forest_Harvest_1985-2022.zip,,CA_Forest_Harvest_1985-2022.tif,CA_Forest_Harvest_1985-2022.zip,,public,"Canadian Forest Service, NTEMS (NFIS)",,"0 none, else the year" ntems_fire_2020,NTEMS forest fire year 1985-2020,fire,1985-2020,https://opendata.nfis.org/downloads/forest_change/CA_Forest_Fire_1985-2020.zip,,,CA_Forest_Fire_1985-2020.zip,,public,"Canadian Forest Service, NTEMS (NFIS)",,used to move SCANFI v1 ages from 2020 ntems_harvest_2020,NTEMS forest harvest year 1985-2020,harvest,1985-2020,https://opendata.nfis.org/downloads/forest_change/CA_Forest_Harvest_1985-2020.zip,,,CA_Forest_Harvest_1985-2020.zip,,public,"Canadian Forest Service, NTEMS (NFIS)",,used to move SCANFI v1 ages from 2020 -scanfi_age_v1,SCANFI v1 stand age 2020,forest_age,2020,https://drive.google.com/file/d/1OdZ7Tznk53KceEyt9dFOBOkxDHEX5X0U,,,,,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators -scanfi_age_v2,SCANFI v2 stand age 1985,forest_age,1985,https://drive.google.com/file/d/1KoGbzQZB-wR8LkngAKTZGShDR0ILBK5r,,SCANFI_age_median_1985_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/1985/SCANFI_age_median_1985_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 1990,forest_age,1990,https://drive.google.com/file/d/1083izY1R-uBRdnnJL6KTYTPg3RFKCU6V,,SCANFI_age_median_1990_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/1990/SCANFI_age_median_1990_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 1995,forest_age,1995,https://drive.google.com/file/d/12aqGD4YQKrZXUrzSPIOz2DcVY00mQZAT,,SCANFI_age_median_1995_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/1995/SCANFI_age_median_1995_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 2000,forest_age,2000,https://drive.google.com/file/d/1v-9sx1a_-WqKfuULT80QZ1b0CAI8lLGV,,SCANFI_age_median_2000_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2000/SCANFI_age_median_2000_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 2005,forest_age,2005,https://drive.google.com/file/d/1HSWZ7aHW9GxfYTI3aAZRz5i2rD9-pymq,,SCANFI_age_median_2005_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2005/SCANFI_age_median_2005_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 2010,forest_age,2010,https://drive.google.com/file/d/1PIC0pvDUZFx7DfauJvB2WsJjgBgkCaXb,,SCANFI_age_median_2010_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2010/SCANFI_age_median_2010_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 2015,forest_age,2015,https://drive.google.com/file/d/1_ZGdjepqS3tGHKykAG5SCecic4Qn-Nx-,,SCANFI_age_median_2015_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2015/SCANFI_age_median_2015_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 2020,forest_age,2020,https://drive.google.com/file/d/1nXPS3bpFUESYieNfXO25OKlZJEgqtRnD,,SCANFI_age_median_2020_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2020/SCANFI_age_median_2020_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror -scanfi_age_v2,SCANFI v2 stand age 2025,forest_age,2025,https://drive.google.com/file/d/1mM2z-_sjt9HGv1JQIt2VZkhW_HrldhgT,,SCANFI_age_median_2025_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2025/SCANFI_age_median_2025_v2_20260119.tif,public,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v1,SCANFI v1 stand age 2020,forest_age,2020,https://drive.google.com/file/d/1OdZ7Tznk53KceEyt9dFOBOkxDHEX5X0U,,,,,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators +scanfi_age_v2,SCANFI v2 stand age 1985,forest_age,1985,https://drive.google.com/file/d/1KoGbzQZB-wR8LkngAKTZGShDR0ILBK5r,,SCANFI_age_median_1985_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/1985/SCANFI_age_median_1985_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 1990,forest_age,1990,https://drive.google.com/file/d/1083izY1R-uBRdnnJL6KTYTPg3RFKCU6V,,SCANFI_age_median_1990_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/1990/SCANFI_age_median_1990_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 1995,forest_age,1995,https://drive.google.com/file/d/12aqGD4YQKrZXUrzSPIOz2DcVY00mQZAT,,SCANFI_age_median_1995_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/1995/SCANFI_age_median_1995_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 2000,forest_age,2000,https://drive.google.com/file/d/1v-9sx1a_-WqKfuULT80QZ1b0CAI8lLGV,,SCANFI_age_median_2000_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2000/SCANFI_age_median_2000_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 2005,forest_age,2005,https://drive.google.com/file/d/1HSWZ7aHW9GxfYTI3aAZRz5i2rD9-pymq,,SCANFI_age_median_2005_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2005/SCANFI_age_median_2005_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 2010,forest_age,2010,https://drive.google.com/file/d/1PIC0pvDUZFx7DfauJvB2WsJjgBgkCaXb,,SCANFI_age_median_2010_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2010/SCANFI_age_median_2010_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 2015,forest_age,2015,https://drive.google.com/file/d/1_ZGdjepqS3tGHKykAG5SCecic4Qn-Nx-,,SCANFI_age_median_2015_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2015/SCANFI_age_median_2015_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 2020,forest_age,2020,https://drive.google.com/file/d/1nXPS3bpFUESYieNfXO25OKlZJEgqtRnD,,SCANFI_age_median_2020_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2020/SCANFI_age_median_2020_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror +scanfi_age_v2,SCANFI v2 stand age 2025,forest_age,2025,https://drive.google.com/file/d/1mM2z-_sjt9HGv1JQIt2VZkhW_HrldhgT,,SCANFI_age_median_2025_v2_20260119.tif,,https://object-arbutus.alliancecan.ca/529feda02d404836b0e91b1d360b4df0:predictiveecology/SCANFI_v2/2025/SCANFI_age_median_2025_v2_20260119.tif,restricted,"Natural Resources Canada, Canadian Forest Service (SCANFI)",,Google Drive folder shared with collaborators; public copy at mirror knn_age,kNN stand age 2001,forest_age,2001,https://ftp.maps.canada.ca/pub/nrcan_rncan/Forests_Foret/canada-forests-attributes_attributs-forests-canada/2001-attributes_attributs-2001/NFI_MODIS250m_2001_kNN_Structure_Stand_Age_v1.tif,,NFI_MODIS250m_2001_kNN_Structure_Stand_Age_v1.tif,,,public,"Natural Resources Canada, Canadian Forest Service (NFI kNN)",,250 m knn_age,kNN stand age 2011,forest_age,2011,https://ftp.maps.canada.ca/pub/nrcan_rncan/Forests_Foret/canada-forests-attributes_attributs-forests-canada/2011-attributes_attributs-2011/NFI_MODIS250m_2011_kNN_Structure_Stand_Age_v1.tif,,NFI_MODIS250m_2011_kNN_Structure_Stand_Age_v1.tif,,,public,"Natural Resources Canada, Canadian Forest Service (NFI kNN)",,250 m diff --git a/man/inputsTable.Rd b/man/inputsTable.Rd index 0d9542b..fffa52b 100644 --- a/man/inputsTable.Rd +++ b/man/inputsTable.Rd @@ -17,10 +17,13 @@ One row per input: the fire polygons, Landsat disturbance maps and stand age map functions here read by default, with where each comes from. The columns follow the inputs table of hsaCEtool (\code{name}, \code{title}, \code{theme}, \code{url}, \code{path}, \code{target_file}, \code{archive}, \code{access}, \code{provider}, \code{terms}, \code{notes}), plus \code{year} (the year or years a row covers) and -\code{mirror} (a public copy of \code{url}, used when \code{url} cannot be reached). A \verb{\%d} in \code{url} and +\code{mirror} (a public copy of \code{url}; the SCANFI readers use it through \code{\link[=scanfiUrlRemap]{scanfiUrlRemap()}}). A \verb{\%d} in \code{url} and \code{target_file} stands for the year, for inputs with one file per year. } \details{ +\code{access} is \code{public} when anyone can download \code{url}, and \code{restricted} when \code{url} needs +permission (e.g., a shared Google Drive folder). + The function defaults read their URLs from this table, so a moved file is fixed here once. The table is \code{system.file("extdata", "inputs.csv", package = "forestInputs")}. } diff --git a/man/prepInputsFireYear.Rd b/man/prepInputsFireYear.Rd index 7b7d8e6..bf6bc34 100644 --- a/man/prepInputsFireYear.Rd +++ b/man/prepInputsFireYear.Rd @@ -43,7 +43,7 @@ a \code{SpatRaster} layer of fire perimeters with fire year values. Create a raster of fire perimeters } \examples{ -\dontshow{if (!isTRUE(as.logical(Sys.getenv("CI")))) withAutoprint(\{ # examplesIf} +\dontshow{if (!isTRUE(as.logical(Sys.getenv("CI"))) && requireNamespace("withr", quietly = TRUE)) withAutoprint(\{ # examplesIf} ## NOTE: runs locally, skipped on CI -- downloads NFDB_poly.zip from ## cwfis.cfs.nrcan.gc.ca (same predicate as testthat::skip_on_ci()) withr::local_options(list( diff --git a/man/replaceAgeInFires.Rd b/man/replaceAgeInFires.Rd index 55c6d62..240de49 100644 --- a/man/replaceAgeInFires.Rd +++ b/man/replaceAgeInFires.Rd @@ -9,7 +9,8 @@ replaceAgeInFires(standAgeMap, firePerimeters, startTime) \arguments{ \item{standAgeMap}{a raster layer stand age map} -\item{firePerimeters}{the earliest fire date to allow} +\item{firePerimeters}{a \code{SpatRaster} of fire years on the grid of \code{standAgeMap}, +e.g., from \code{\link[=prepInputsFireYear]{prepInputsFireYear()}}} \item{startTime}{date of the last fire year to be considered (e.g., start of fire period counting backwards). If missing or earlier than 1950 (e.g., a simulation diff --git a/tests/testthat/test-prepInputsFireYear-grid.R b/tests/testthat/test-prepInputsFireYear-grid.R index 8ede51e..15b473d 100644 --- a/tests/testthat/test-prepInputsFireYear-grid.R +++ b/tests/testthat/test-prepInputsFireYear-grid.R @@ -139,3 +139,57 @@ test_that("prepInputsFireYear skips the pre-1972 archive when the records alread useCache = FALSE))) expect_equal(sort(stats::na.omit(unique(terra::values(fires, mat = FALSE)))), c(1960, 1985)) }) + +## Fire polygons zipped as a local "url", for the tests below +.fireZip <- function(fires) { + src <- withr::local_tempdir("fireSrc_", .local_envir = parent.frame()) + terra::writeVector(fires, file.path(src, "fires.shp")) + zipped <- withr::with_dir(src, utils::zip("fires.zip", list.files(src, "^fires\\."), flags = "-q")) + skip_if_not(identical(zipped, 0L), "no zip utility") + paste0("file://", file.path(src, "fires.zip")) +} +.twoFires <- function(crs = "EPSG:3978") { + fires <- terra::vect(c("POLYGON ((600 600, 1200 600, 1200 1200, 600 1200, 600 600))", + "POLYGON ((1300 1300, 1800 1300, 1800 1800, 1300 1800, 1300 1300))"), crs = "EPSG:3978") + fires$YEAR <- c(1995, 2010) + terra::project(fires, crs) +} + +## as.numeric(as.factor()) gave factor codes 1, 2 instead of the years, and the +## earliestYear filter then set every cell to NA +test_that("prepInputsFireYear reads a fire year stored as text as the year", { + skip_if_not_installed("terra") + rtm <- terra::rast(terra::ext(0, 2400, 0, 2400), resolution = 240, crs = "EPSG:3978", vals = 1) + fires <- .twoFires() + fires$YEAR <- as.character(fires$YEAR) + url <- .fireZip(fires) + expect_warning( + out <- suppressMessages(prepInputsFireYear(rasterToMatch = rtm, url = url, useCache = FALSE, + destinationPath = withr::local_tempdir())), + "coerced to numeric") + expect_setequal(stats::na.omit(unique(terra::values(out, mat = FALSE))), c(1995, 2010)) +}) + +test_that("prepInputsFireYear reprojects fire polygons in another CRS to rasterToMatch", { + skip_if_not_installed("terra") + rtm <- terra::rast(terra::ext(0, 2400, 0, 2400), resolution = 240, crs = "EPSG:3978", vals = 1) + url <- .fireZip(.twoFires(crs = "EPSG:4326")) + out <- suppressWarnings(suppressMessages( + prepInputsFireYear(rasterToMatch = rtm, url = url, useCache = FALSE, destinationPath = withr::local_tempdir()))) + expect_true(terra::compareGeom(out, rtm, stopOnError = FALSE)) + expect_equal(out[terra::cellFromXY(rtm, cbind(900, 900))][[1]], 1995) + expect_equal(out[terra::cellFromXY(rtm, cbind(1500, 1500))][[1]], 2010) +}) + +## The LandR version stopped with an error on a RasterLayer grid whenever there were fires +test_that("prepInputsFireYear takes a RasterLayer rasterToMatch and returns a SpatRaster", { + skip_if_not_installed("terra") + skip_if_not_installed("raster") + rtm <- terra::rast(terra::ext(0, 2400, 0, 2400), resolution = 240, crs = "EPSG:3978", vals = 1) + url <- .fireZip(.twoFires()) + out <- suppressWarnings(suppressMessages( + prepInputsFireYear(rasterToMatch = raster::raster(rtm), url = url, useCache = FALSE, + destinationPath = withr::local_tempdir()))) + expect_s4_class(out, "SpatRaster") + expect_equal(out[terra::cellFromXY(rtm, cbind(900, 900))][[1]], 1995) +})