st_as_sf {stars} | R Documentation |
Convert stars object into an sf object
Description
Convert stars object into an sf object
Usage
## S3 method for class 'stars'
st_as_sfc(x, ..., as_points, which = seq_len(prod(dim(x)[1:2])))
## S3 method for class 'stars'
st_as_sf(
x,
...,
as_points = FALSE,
merge = FALSE,
na.rm = TRUE,
use_integer = is.logical(x[[1]]) || is.integer(x[[1]]),
long = FALSE,
connect8 = FALSE
)
## S3 method for class 'stars_proxy'
st_as_sf(x, ..., downsample = 0)
Arguments
x |
object of class |
... |
ignored |
as_points |
logical; should cells be converted to points or to polygons? See details. |
which |
linear index of cells to keep (this argument is not recommended to be used) |
merge |
logical; if |
na.rm |
logical; should missing valued cells be removed, or also be converted to features? |
use_integer |
(relevant only if |
long |
logical; if |
connect8 |
logical; if |
downsample |
see st_as_stars |
Details
If merge
is TRUE
, only the first attribute is converted into an sf
object. If na.rm
is FALSE
, areas with NA
values are also written out as polygons. Note that the resulting polygons are typically invalid, and use st_make_valid to create valid polygons out of them.
Examples
tif = system.file("tif/L7_ETMs.tif", package = "stars")
x = read_stars(tif)
x = x[,1:100,1:100,6] # subset of a band with lower values in it
x[[1]][x[[1]] < 30] = NA # set lower values to NA
x[[1]] = x[[1]] < 100 # make the rest binary
x
(p = st_as_sf(x)) # removes NA areas
(p = st_as_sf(x[,,,1], merge = TRUE)) # glues polygons together
all(st_is_valid(p)) # not all valid, see details
plot(p, axes = TRUE)
(p = st_as_sf(x, na.rm = FALSE, merge = TRUE)) # includes polygons with NA values
plot(p, axes = TRUE)