Learn R Programming

tidyterra (version 1.3.0)

as_spatraster: Coerce a data frame to SpatRaster

Description

as_spatraster() converts a data frame or tibble into a SpatRaster. It wraps the terra::rast() S4 method for signature data.frame.

Usage

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

Value

A SpatRaster.

Arguments

x

A tibble or data frame.

...

Additional arguments passed 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 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.

digits

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

<a href="https://CRAN.R-project.org/package=terra"><span class="pkg">terra</span></a> equivalent

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

Details

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

See Also

is_regular_grid() for checking whether coordinates form a regular grid. pull_crs() retrieves CRS information, as do sf::st_crs() and terra::crs().

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

Examples

Run this code
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