render_region_plot: Render a regional raster plot with optional custom boundaries

View source: R/render_region_plot.R

render_region_plotR Documentation

Render a regional raster plot with optional custom boundaries

Description

Produces a map image for a selected region by cropping/masking a NetCDF raster, handling longitude wrap (0–360 -> -180–180), CRS alignment, and setting sane plot limits/aspect. Designed for use in the CM SAF R Toolbox.

Usage

render_region_plot(
  infile,
  outfile = NULL,
  fileExtension = ".png",
  visualizeVariables,
  visualizeDataMax,
  lon_bounds,
  lat_bounds,
  lon_loc_vec,
  lat_loc_vec,
  name_loc_vec,
  division,
  selectedRegion,
  region_data,
  timestep,
  num_tick,
  num_rmin,
  num_rmax,
  location,
  text1,
  text2,
  text3,
  PAL,
  palettes,
  num_brk,
  reverse,
  textsize,
  bordercolor,
  plot_grid,
  grid_col,
  image_def,
  ihsf,
  nc = NULL
)

Arguments

infile

Character path to the NetCDF input file.

outfile

Optional path for the output image file. If NULL, a temp file is used.

fileExtension

Output file extension, one of ".png", ".jpg", ".pdf".

visualizeVariables

List with at least $vn (variable name) and $date.time.

visualizeDataMax

Numeric; used for colorbar tick calculation.

lon_bounds, lat_bounds

Numeric vectors of visible longitude/latitude bounds (unused here but part of API).

lon_loc_vec, lat_loc_vec, name_loc_vec

Optional location markers (lon/lat + labels).

division

Character name of the attribute/level used to select the region (e.g. "COUNTRY" or a shapefile field).

selectedRegion

Character code/value of the selected region (e.g. ISO3).

region_data

Spatial or sf object with user-provided regions (when division != "COUNTRY").

timestep

Selected timestep (should match an entry of visualizeVariables$date.time).

num_tick, num_rmin, num_rmax

Numeric settings for legend ticks and z range.

location

Logical; draw location markers if TRUE.

text1, text2, text3

Character strings for title, footer, and legend label.

PAL, palettes, num_brk, reverse

Color palette settings passed to getColors().

textsize

Numeric base text size used in plotting.

bordercolor

Color for region borders and markers.

plot_grid, grid_col

Currently unused plotting options (kept for API compatibility).

image_def, ihsf

Image sizing settings from the Toolbox.

nc

Optional opened NetCDF handle; if provided, infile <- nc$filename.

Details

The function:

  1. Loads the raster brick for the requested variable.

  2. Normalizes longitudes to -180 to 180 if input is 0–360 (via raster::rotate).

  3. Validates/aligns the region geometry (sf -> Spatial, EPSG:4326, transform to raster CRS).

  4. Crops/masks the raster by the region and draws the image with geographic aspect.

The package lwgeom is used optionally (via requireNamespace) to fix invalid geometries; a st_buffer(., 0) fallback is applied if lwgeom is not available.

Value

A named list with src (file path), contentType, width, height.


cmsafvis documentation built on Nov. 5, 2025, 7:18 p.m.