Skip to contents

Convert class between sf/stars-terra

Usage

dep_switch(input)

Arguments

input

Spat* in terra or sf object.

Value

Data converted to the other package class (if sf, terra; if terra, sf)

Author

Insang Song

Examples

library(sf)
library(stars)
library(terra)
options(sf_use_s2 = FALSE)

## generate a random raster
ras_rand <- terra::rast(nrow = 30, ncol = 30)
terra::values(ras_rand) <- runif(900)
stars_rand <- dep_switch(ras_rand)
stars_rand
#> stars object with 2 dimensions and 1 attribute
#> attribute(s):
#>               Min.  1st Qu.    Median      Mean   3rd Qu.      Max.
#> lyr.1  0.002977239 0.267624 0.4937182 0.5006182 0.7345819 0.9977391
#> dimension(s):
#>   from to offset delta refsys x/y
#> x    1 30   -180    12 WGS 84 [x]
#> y    1 30     90    -6 WGS 84 [y]
# should return stars object

vec_rand <- terra::spatSample(ras_rand, size = 10L, as.points = TRUE)
sf_rand <- dep_switch(vec_rand)
sf_rand
#> Simple feature collection with 10 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -162 ymin: -69 xmax: 150 ymax: 45
#> Geodetic CRS:  WGS 84
#>        lyr.1         geometry
#> 1  0.6226255   POINT (-162 3)
#> 2  0.6969408   POINT (-66 -3)
#> 3  0.5969548    POINT (30 33)
#> 4  0.1762442    POINT (138 3)
#> 5  0.2826658   POINT (150 39)
#> 6  0.5825950 POINT (-126 -69)
#> 7  0.9565204  POINT (-126 15)
#> 8  0.3923108   POINT (102 45)
#> 9  0.1054764     POINT (-6 3)
#> 10 0.8319061    POINT (-18 9)
# should return sf object