mask_stars: Mask a stars object with a polygon

View source: R/stars.R

mask_starsR Documentation

Mask a stars object with a polygon

Description

You can use st_crop(...) but it runs slowly for large rasters

Usage

mask_stars(x, y, invert = FALSE, filename = NA)

Arguments

x

stars object

y

sf polygon object. Pixels exterior of this as assigned NA

invert

logical, if TRUE values inside the polygon are set to NA

filename

character or NA, if a filename write to the file

Value

a masked stars object, areas outside (or possibly inside) are set to NA depending upon the value of invert. Default is outside is NA

See Also

https://github.com/r-spatial/stars/issues/622

Examples

## Not run: 
  library(stars)
  library(sf)
  x = read_stars(system.file("tif/L7_ETMs.tif", package = "stars")) 
  m = cbind(c(290000, 295000, 292500, 290000),
            c(9113000, 9115000, 9118000, 9113000))
  p = st_sfc( st_polygon(list(m)) ) |> 
    st_set_crs(31985)
  m = mask_stars(x, p)
  plot_p = function() plot(p, add = TRUE, border = 'orange')
  z = c(x,m, along = 'band')
  plot(z, hook = plot_p)

## End(Not run)

BigelowLab/twinkle documentation built on Jan. 26, 2025, 6:34 a.m.