wc_to_ascii() facilitates the conversion of one or more
WorldClim
GeoTIFF files to the Esri ASCII Grid 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,
box = NULL,
dateline_fix = TRUE,
extreme_outlier_fix = TRUE,
aggregate = NULL,
overwrite = TRUE,
dx = -45,
na_flag = -99,
...
)Arguments
- file
A
charactervector of file paths to the WorldClim GeoTIFF files to be converted. The files must have a.tifextension.- dir
A
charactervector specifying the output directory for the converted ASCII files (default:dirname(file[1])).- shape
(optional) A
SpatVectorobject representing the polygon to crop the raster data. The function will crop the raster data to the extent of this polygon (default:NULL).- box
(optional) A
numericvector of length 4 specifying the bounding box for cropping the raster data in the formatc(xmin, ymin, xmax, ymax)(default:NULL).- dateline_fix
(optional) A
logicalflag indicating whether to apply a dateline fix to the raster data. This is particularly useful when working with rasters and vectors that span the dateline (e.g. the Russian territory). Seeshift_and_cropto learn more (default:TRUE).- extreme_outlier_fix
(optional) A
logicalflag indicating whether to replace extreme outliers withNA. Extreme outliers are defined as values more than 10 times the interquartile range (IQR) below the first or above the third quartile. The quartiles and IQR are calculated using the unique (deduplicated) values of the data, and the resulting thresholds are applied to the full dataset. This helps remove abnormal values in raster data.- aggregate
(optional) An
integervalue specifying the aggregation factor. The function will aggregate the raster data by this factor. Seeaggregate()for more details (default:NULL).- overwrite
(optional) A
logicalflag indicating whether to overwrite existing files in the output directory (default:TRUE).- dx
(optional) A
numericvalue specifying the horizontal distance in degrees to shift the raster data. This is only relevant ifdateline_fixis set toTRUE(default:-45).- na_flag
(optional) An
integervalue specifying the NoData value for the output ASCII files. See the Details section to learn more (default:-99).- ...
Additional arguments passed to
writeRaster()for writing the ASCII files.
Value
A character vector containing the file paths
of the converted ASCII files.
Details
na_flag parameter
According to the Esri ASCII raster format documentation, the default value
for NODATA_VALUE (the NA flag) is -9999. However, using four digits of
precision significantly inflates file size. For WorldClim data, two
significant digits (-99) are sufficient, since the only variables with
negative values are temperatures, and the lowest temperature ever recorded on
Earth is above that.
See also
Other WorldClim functions:
get_wc_url(),
worldclim_extract_variable(),
worldclim_random()
Examples
if (FALSE) { # \dontrun{
library(curl)
library(fs)
library(magrittr)
library(readr)
library(rvest)
library(stringr)
library(zip)
# Download the WorldClim Data
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)
} # }
