Skip to contents

wc_to_ascii() facilitates the conversion of one or more WorldClim GeoTIFF files to ASCII raster format. Optionally, rasters can be cropped and/or aggregated using a provided polygon of class SpatVector.

Usage

wc_to_ascii(
  file,
  dir = dirname(file[1]),
  shape = NULL,
  aggregate = NULL,
  overwrite = TRUE,
  na_flag = -9999,
  ...
)

Arguments

file

A character vector of file paths to the WorldClim GeoTIFF files to be converted. The files must have a .tif extension.

dir

A character vector specifying the output directory for the converted ASCII files (default: dirname(file[1])).

shape

(optional) A SpatVector object representing the polygon to crop the raster data. The function will crop the raster data to the extent of this polygon (default: NULL).

aggregate

(optional) An integer value specifying the aggregation factor. The function will aggregate the raster data by this factor. See aggregate() for more details (default: NULL).

overwrite

(optional) A logical flag indicating whether to overwrite existing files in the output directory (default: TRUE).

na_flag

(optional) An integer value specifying the NoData value for the output ASCII files (default: -9999).

...

Additional arguments passed to writeRaster() for writing the ASCII files.

Value

A character vector containing the file paths of the converted ASCII files.

See also

Other WorldClim functions: get_wc_url()

Examples

if (FALSE) { # \dontrun{
  library(curl)
  library(fs)
  library(magrittr)
  library(readr)
  library(rvest)
  library(stringr)
  library(zip)

  # Download the WorldClim data from the website

  url <-
    get_wc_url("hcd") |>
    rvest::read_html() |>
    rvest::html_elements("a") |>
    rvest::html_attr("href") |>
    stringr::str_subset("geodata") |>
    magrittr::extract(1)

  zip_file <- basename(url)

  curl::curl_download(url, path(tempdir(), zip_file))

  path(tempdir(), zip_file) |>
    zip::unzip(exdir = tempdir())

  tif_file <-
    dir_ls(tempdir(), regexp = "\\.tif$") |>
    magrittr::extract(1)

  # Run the function

  asc_file <- tif_file |> wc_to_ascii()

  # Check the output

  asc_file |> read_lines(n_max = 6)
} # }