Skip to contents

as_spatraster() turns an existing data frame or tibble into a SpatRaster. This is a wrapper of terra::rast() S4 method for signature data.frame.

Usage

as_spatraster(x, ..., xycols = 1:2, crs = "", digits = 6)

Arguments

x

A tibble or data frame.

...

additional arguments passed on to terra::rast().

xycols

A vector of integers of length 2 determining the position of the columns that hold the x and y coordinates.

crs

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

digits

integer to set the precision for detecting whether points are on a regular grid (a low number of digits is a low precision).

Value

A SpatRaster.

Details

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

terra equivalent

terra::rast() (see S4 method for signature data.frame).

See also

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

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

Examples

library(terra)

r <- rast(matrix(1:90, ncol = 3), crs = "EPSG:3857")

r
#> class       : SpatRaster 
#> dimensions  : 30, 3, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 3, 0, 30  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) 
#> source(s)   : memory
#> name        : lyr.1 
#> min value   :     1 
#> max value   :    90 

# Create tibble
as_tbl <- as_tibble(r, xy = TRUE)

as_tbl
#> # A tibble: 90 × 3
#>        x     y lyr.1
#>    <dbl> <dbl> <int>
#>  1   0.5  29.5     1
#>  2   1.5  29.5    31
#>  3   2.5  29.5    61
#>  4   0.5  28.5     2
#>  5   1.5  28.5    32
#>  6   2.5  28.5    62
#>  7   0.5  27.5     3
#>  8   1.5  27.5    33
#>  9   2.5  27.5    63
#> 10   0.5  26.5     4
#> # ℹ 80 more rows

# From tibble
newrast <- as_spatraster(as_tbl, crs = "EPSG:3857")
newrast
#> class       : SpatRaster 
#> dimensions  : 30, 3, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 1  (x, y)
#> extent      : 0, 3, 0, 30  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) 
#> source(s)   : memory
#> name        : lyr.1 
#> min value   :     1 
#> max value   :    90