Learn R Programming

tidyterra (version 0.1.0)

as_spatraster: 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 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(). See 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.

terra equivalent

terra::rast()

Details

[Questioning] 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").

See Also

pull_crs()

Coercing objects: as_coordinates(), as_tibble()

Examples

Run this code
# NOT RUN {
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

# }

Run the code above in your browser using DataLab