
Convert stars object into an sf object
# S3 method for stars
st_as_sfc(x, ..., as_points, which = seq_len(prod(dim(x)[1:2])))# S3 method for 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 stars_proxy
st_as_sf(x, ..., downsample = 0)
object of class stars
ignored
logical; should cells be converted to points or to polygons? See details.
linear index of cells to keep (this argument is not recommended to be used)
logical; if TRUE
, cells with identical values are merged (using GDAL_Polygonize
or GDAL_FPolygonize
); if FALSE
, a polygon for each raster cell is returned; see details
logical; should missing valued cells be removed, or also be converted to features?
(relevant only if merge
is TRUE
): if TRUE
, before polygonizing values are rounded to 32-bits signed integer values (GDALPolygonize), otherwise they are converted to 32-bit floating point values (GDALFPolygonize).
logical; if TRUE
, return a long table form sf
, with geometries and other dimensions recycled
logical; if TRUE
, use 8 connectedness. Otherwise the 4 connectedness algorithm will be applied.
see st_as_stars
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.
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)
Run the code above in your browser using DataLab