get_xyspace: Extract xy-space values

View source: R/functions_netCDF.R

get_xyspaceR Documentation

Extract xy-space values

Description

Extract xy-space values

Usage

get_xyspace(
  x,
  crs,
  res,
  xy_names = c("lon", "lat"),
  tol = sqrt(.Machine[["double.eps"]])
)

Arguments

x

An object that describes a gridded or discrete xy-space. Regular, rectangular grids are the only currently supported grid. This can be

  • an object identifying a netCDF file, i.e., a character string as file name or an object of class ncdf4 derived from ncdf4::nc_open

  • a raster::RasterLayer object,

  • a terra::SpatRaster object,

  • a stars::stars object,

  • a list, such as the one produced by get_xyspace.

  • an object with coordinate values for all gridcell centers that can be passed to as_points;

crs

An object which is a crs or from which one can be derived. x can be numeric as a EPSG number; a character string as a wkt; a character string as a proj4 (not recommended because outdated); or of a class including raster::Raster, sp::Spatial, sp::CRS, or a sf or sfc class.

res

A numeric vector of length two. The (absolute values of) cell size/resolution/delta of the x and y dimensions in units of the crs.

xy_names

A vector with two character strings. The names of the x and y spatial dimensions of a netCDF file.

tol

A numeric value. The tolerance applied to determine if a grid is regular and to calculate grid resolution.

Value

A list with three elements: two vectors one each containing all unique values of the x coordinate and the y coordinates of (i) all gricell centers, if gridded, or (ii) all sites, if discrete; and a vector res with the (regular) x and y resolutions, if gridded (NA, if discrete).

Notes

The argument crs is only used if grid is a data.frame, in which case it is passed to as_points. Otherwise, it is ignored and can be missing.

The argument res is only used in two cases:

  • if grid is a list, such as the one produced by get_xyspace, but does not contain a named element res; or

  • if grid is an object with coordinate values for all gridcell centers.

Otherwise, it is ignored and can be missing.

Examples

# grid as terra object
r <- terra::rast(
  xmin = 0, xmax = 120,
  ymin = 0, ymax = 45,
  crs = "OGC:CRS84",
  resolution = c(1, 1)
)
get_xyspace(r)

# grid as data frame with coordinate values
rdf <- terra::crds(r)
get_xyspace(rdf, crs = "OGC:CRS84", res = c(1, 1))

# a list with vectors for all x values and all y values (and resolution)
rl <- list(
  x = sort(unique(rdf[, 1])),
  y = sort(unique(rdf[, 2]))
)
get_xyspace(c(rl, list(res = c(1, 1))))
get_xyspace(rl, res = c(1, 1))


DrylandEcology/rSW2st documentation built on Jan. 10, 2024, 6:22 p.m.