Skip to contents

as_spatvector() turns an existing object into a SpatVector. It wraps the terra::vect() S4 method for the data.frame signature.

Usage

as_spatvector(x, ...)

# S3 method for class 'data.frame'
as_spatvector(x, ..., geom = c("lon", "lat"), crs = "")

# S3 method for class 'sf'
as_spatvector(x, ...)

# S3 method for class 'sfc'
as_spatvector(x, ...)

# S3 method for class 'SpatVector'
as_spatvector(x, ...)

Arguments

x

A tibble, data frame or sf object of class sf or sfc.

...

Additional arguments passed on to terra::vect().

geom

Character vector naming the fields that contain the geometry data. Use two names for point coordinates (x and y), or one name for a column with WKT geometries.

crs

A CRS in several formats (PROJ.4, WKT, EPSG code, etc.) or a spatial object from sf or terra that includes the target coordinate reference system. See pull_crs() and Details.

Value

A SpatVector.

Details

This function differs from terra::vect() in the following ways:

  • Rows with geometry values NA or "" are removed before conversion.

  • If x is a grouped data frame (see dplyr::group_by()), the grouping variables are transferred and a grouped SpatVector is created (see group_by.SpatVector()).

  • If no crs is provided and the tibble has been created with the method as_tibble.SpatVector(), the crs is inferred from attr(x, "crs").

  • It handles the conversion of EMPTY geometries between sf and terra.

terra equivalent

terra::vect()

See also

pull_crs() for retrieving CRS and the corresponding utils sf::st_crs() and terra::crs().

Coercing objects: as_coordinates(), as_sf(), as_spatraster(), as_tibble.Spat, fortify.Spat, tidy.Spat

Examples

library(terra)

v <- vect(matrix(1:80, ncol = 2), crs = "EPSG:3857")

v$cat <- sample(LETTERS[1:4], size = nrow(v), replace = TRUE)

v
#> class       : SpatVector
#> geometry    : points
#> dimensions  : 40, 1  (geometries, attributes)
#> extent      : 1, 40, 41, 80  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
#> names       :   cat
#> type        : <chr>
#> values      :     A
#>                   C
#>                   D
#>               ...

# Create tibble
as_tbl <- as_tibble(v, geom = "WKT")

as_tbl
#> # A tibble: 40 × 2
#>    cat   geometry     
#>    <chr> <chr>        
#>  1 A     POINT (1 41) 
#>  2 C     POINT (2 42) 
#>  3 D     POINT (3 43) 
#>  4 C     POINT (4 44) 
#>  5 A     POINT (5 45) 
#>  6 B     POINT (6 46) 
#>  7 C     POINT (7 47) 
#>  8 C     POINT (8 48) 
#>  9 C     POINT (9 49) 
#> 10 D     POINT (10 50)
#> # ℹ 30 more rows

# From tibble
newvect <- as_spatvector(as_tbl, geom = "geometry", crs = "EPSG:3857")
newvect
#> class       : SpatVector
#> geometry    : points
#> dimensions  : 40, 1  (geometries, attributes)
#> extent      : 1, 40, 41, 80  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
#> names       :   cat
#> type        : <chr>
#> values      :     A
#>                   C
#>                   D
#>               ...