pretty_map_from_file_raster: Pretty raster maps from file

View source: R/pretty_map.R

pretty_map_from_file_rasterR Documentation

Pretty raster maps from file

Description

This function creates pretty raster maps from a list of source files. For each file, the function reads the raster into R (via read), processes the raster (via process, if specified) and produces or saves a plot (via pretty_map and associated arguments). The function can be implemented in parallel via cl and varlist.

Usage

pretty_map_from_file_raster(
  x,
  read = raster::raster,
  add_rasters = list(),
  process = NULL,
  png_param = NULL,
  cl = NULL,
  varlist = NULL,
  ...
)

Arguments

x

A list of full file paths to rasters for plotting.

read

A function to read files. The default is raster.

add_rasters

A named list, passed to pretty_map, to customise the appearance of each raster. This applied to each raster.

process

(optional) A function to process rasters, such as function(x) raster::mask(x, layer) where layer refers to a spatial mask. This applied to each raster.

png_param

(optional) A named list of arguments, passed to png, to save plots to file. The ‘filename’ argument should be the directory in which plots are saved. Plots are then saved as "1.png", "2.png" and so on.

cl, varlist

Parallelisation arguments. cl is cluster object created by makeCluster to read/plot/save files in parallel. If cl is supplied, varlist may also be required. This is a character vector of objects to export. varlist is passed to the varlist of clusterExport. Exported objects must be located in the global environment.

...

Additional arguments passed to pretty_map.

Value

The function returns or saves a plot for each file.

Author(s)

Edward Lavender

See Also

This is a wrapper for pretty_map.

Examples

#### Generate and save some example raster files
r1 <- raster::raster(matrix(runif(100, 0, 1), ncol = 10, nrow = 10))
r2 <- raster::raster(matrix(runif(100, 10, 20), ncol = 10, nrow = 10))
root <- paste0(tempdir(), "/egs/")
dir.create(root)
raster::writeRaster(r1, paste0(root, "r1.tif"))
raster::writeRaster(r1, paste0(root, "r2.tif"))

#### List source files for plotting
files <- list.files(root, full.names = TRUE)

#### Example (1): Implement function with default options
pp <- graphics::par(mfrow = c(1, 2))
pretty_map_from_file_raster(files)
par(pp)

#### Example (2): Customise the rasters/plot via add_rasters and ...
pp <- graphics::par(mfrow = c(1, 2))
pretty_map_from_file_raster(files,
                            add_rasters = list(col = grDevices::topo.colors(100)),
                            xlab = "x", ylab = "y")
graphics::par(pp)

#### Example (3): Process rasters using the process argument
# E.g. to mask areas of the rasters
pp <- graphics::par(mfrow = c(1, 2))
mk <- r1
mk[1, ] <- NA
pretty_map_from_file_raster(files, process = function(x) raster::mask(x, mask = mk))
graphics::par(pp)

#### Example (4): Save plots to file via png_param
pp <- graphics::par(mfrow = c(1, 2))
pretty_map_from_file_raster(files, png_param = list(filename = root))
graphics::par(pp)
list.files(root, "*.png")

#### Example (5): Read, plot and save files in parallel via cl and varlist
## Not run: 
pretty_map_from_file_raster(files,
                            png_param = list(filename = root),
                            cl = parallel::makeCluster(2L),
                            varlist = "files")

## End(Not run)


edwardlavender/prettyGraphics documentation built on Jan. 19, 2025, 2:47 p.m.