R/cadastral_helpers.R

Defines functions unstash_cadastral stash_cadastral

Documented in stash_cadastral unstash_cadastral

#' Store USFWS cadastral data prior to updating \code{fwspp} package
#'
#' Regular updates of the \code{fwspp} package can be annoying because
#'  prepping the USFWS cadastral data consumes 10-15 minutes. Use this
#'  function to stash previously installed cadastral data prior to
#'  updating or reinstalling the \code{fwspp} package and then run
#'  \code{\link{unstash_cadastral}} to restore the cadastral to its
#'  rightful place. It's best not to load the \code{fwspp} namespace
#'  when stashing the cadastral data. See example.
#'
#' @return character string of stashed cadastral files; pass this string
#'  to \code{\link{unstash_cadastral}} after re-installing the package
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # In new session
#' cad <- fwspp::stash_cadastral()
#'
#' # Re-install fwspp package
#' devtools::install_github("USFWS/fwspp")
#'
#' # Unstash the cadastral data
#' library(fwspp)
#' unstash_cadastral(cad)
#' }
stash_cadastral <- function() {

  gdb <- try(system.file("extdata/FWSCadastral.gdb", package = "fwspp",
                         mustWork = TRUE), silent = TRUE)
  approved <-  try(system.file("extdata/fws_approved.rds", package = "fwspp",
                               mustWork = TRUE), silent = TRUE)
  interest <-  try(system.file("extdata/fws_interest.rds", package = "fwspp",
                               mustWork = TRUE), silent = TRUE)
  stash_list <- list(gdb, approved, interest)

  if (any(sapply(stash_list, is_error)))
    stop("At least one cadastral files is missing. Run `install_fws_cadastral()`",
         " after the re-install.")

  stash <- tempfile(c("GDB", "APPROVED", "INTEREST"),
                    tmpdir = ".", fileext = ".cadastral")

  for (i in seq_along(stash_list)) {
    file.rename(stash_list[[i]], stash[i])
  }
  stash
}


#' Restore USFWS cadastral data after updating \code{fwspp} package
#'
#' Regular updates of the \code{fwspp} package can be annoying because
#'  prepping the USFWS cadastral data consumes 10-15 minutes. Use this
#'  function to unstash USFWS cadastral data stashed with
#'  \code{\link{unstash_cadastral}} prior to updating or reinstalling
#'  the \code{fwspp} package. See example.
#'
#' @param stash character vector of temporary file paths (length == 3)
#'  generated by \code{\link{stash_cadastral}}
#'
#' @return \code{NULL}. Stashed cadastral files are restored.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # In new session
#' cad <- fwspp::stash_cadastral()
#'
#' # Re-install fwspp package
#' devtools::install_github("USFWS/fwspp")
#'
#' # Unstash the cadastral data
#' library(fwspp)
#' unstash_cadastral(cad)
#' }
unstash_cadastral <- function(stash) {
  if (length(stash) != 3) stop("Input path name vector of incorrect length.")
  if (!all(sapply(stash, file.exists)))
    stop("Not all input files were found. Check paths or ",
         "run `install_fws_cadastral()`.")

  fwspp_dir <- file.path(.Library, "fwspp/extdata")
  if (!dir.exists(fwspp_dir))
    stop("The `fwspp` package is not installed in the default library.")

  unstash <- c(file.path(fwspp_dir, "FWSCadastral.gdb"),
               file.path(fwspp_dir, "fws_approved.rds"),
               file.path(fwspp_dir, "fws_interest.rds"))

  for (i in seq_along(stash)) {
    file.rename(stash[i], unstash[i])
  }
  invisible()
}
adamdsmith/fwspp documentation built on Oct. 16, 2023, 3:43 a.m.