R/mask.R

Defines functions vol_mask

Documented in vol_mask

# ============================ #
# The opaque sheet             #
# ============================ #

#' Mark grid points that fall inside a polygon
#'
#' @title Mark grid points that fall inside a polygon
#' @description Builds the logical mask the renderer draws as an opaque sheet at
#'   zero depth, by testing every grid point against a polygon set. Land, for a
#'   marine field.
#' @param lon Numeric vector of longitudes.
#' @param lat Numeric vector of latitudes.
#' @param polygons An `sf` or `sfc` object holding the polygons.
#' @param planar Logical. When `TRUE`, the containment test draws straight edges
#'   between the vertices instead of great circles.
#' @return A logical matrix of `length(lat)` rows by `length(lon)` columns,
#'   `TRUE` where the point falls inside the polygon set.
#' @details The test runs in the plane by default, and the default is not
#'   cosmetic. \pkg{sf} treats longitude and latitude as coordinates on a sphere
#'   unless told otherwise, so an edge between two vertices is a great circle;
#'   the convention almost every coastline product and plotting library assumes
#'   is a straight edge in the longitude and latitude plane. On a grid of a
#'   twenty-fourth of a degree the two conventions disagree on a small but
#'   non-empty set of cells near the coast, so the choice has to be made
#'   explicitly rather than inherited.
#'
#'   The polygons are unioned by the caller when that is worth doing once for
#'   many calls. A point exactly on an edge counts as inside, which
#'   [sf::st_intersects()] gives and a strict containment test does not; on a
#'   grid whose points are irrational multiples of the polygon vertices this
#'   distinction reaches no cell.
#'
#'   This function needs \pkg{sf}, which is a suggested package and not a
#'   dependency, because a field that needs no opaque sheet needs no geometry
#'   engine.
#' @references Pebesma, E. (2018). Simple features for R: standardized support
#'   for spatial vector data. *The R Journal*, 10(1), 439-446.
#'   \doi{10.32614/RJ-2018-009}
#' @examples
#' if (requireNamespace("sf", quietly = TRUE)) {
#'   land <- readRDS(system.file("extdata", "med_land.rds", package = "volumr"))
#'   m <- vol_mask(seq(-6, 36, by = 0.5), seq(30, 46, by = 0.5), land)
#'   dim(m)
#'   mean(m)
#' }
#' @seealso [vol_field()], which carries the mask, [vol_payload()], which writes
#'   it into the payload as an image, [vol_widget()], whose `showLand` option
#'   draws or hides it.
#' @export
vol_mask <- function(lon, lat, polygons, planar = TRUE) {
    require_package("sf")
    old <- sf::sf_use_s2()
    on.exit(suppressMessages(sf::sf_use_s2(old)), add = TRUE)
    suppressMessages(sf::sf_use_s2(!planar))
    g <- if (inherits(polygons, "sf")) sf::st_geometry(polygons) else polygons
    pts <- sf::st_as_sf(expand.grid(x = lon, y = lat), coords = c("x", "y"),
                        crs = sf::st_crs(g))
    hit <- lengths(suppressMessages(sf::st_intersects(pts, g))) > 0L
    matrix(hit, nrow = length(lat), ncol = length(lon), byrow = TRUE)
}

Try the volumr package in your browser

Any scripts or data that you put into this service are public.

volumr documentation built on Dec. 2, 2026, 5:07 p.m.