as_spatraster {tidyterra}R Documentation

Coerce a data frame to SpatRaster

Description

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).

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").

Value

A SpatRaster.

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

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

as_tbl

# From tibble
newrast <- as_spatraster(as_tbl, crs = "EPSG:3857")
newrast


[Package tidyterra version 0.6.1 Index]