Skip to contents

as_coordinates() can be used to obtain the position of each cell on the SpatRaster matrix.

Usage

as_coordinates(x, as.raster = FALSE)

Arguments

x

A SpatRaster object

as.raster

If TRUE, the result is a SpatRaster object with three layers indicating the position of each cell (cell number, row and column).

Value

A tibble or a SpatRaster (if as.raster = TRUE) with the same number of rows (or cells) than the number of cells in x.

When as.raster = TRUE the resulting SpatRaster has the same crs, extension and resolution than x

Examples


library(terra)
#> terra 1.6.41

f <- system.file("extdata/cyl_temp.tif", package = "tidyterra")

r <- rast(f)

as_coordinates(r)
#> # A tibble: 10,324 × 3
#>    cellindex rowindex colindex
#>        <int>    <dbl>    <dbl>
#>  1         1        1        1
#>  2         2        1        2
#>  3         3        1        3
#>  4         4        1        4
#>  5         5        1        5
#>  6         6        1        6
#>  7         7        1        7
#>  8         8        1        8
#>  9         9        1        9
#> 10        10        1       10
#> # … with 10,314 more rows
as_coordinates(r, as.raster = TRUE)
#> class       : SpatRaster 
#> dimensions  : 89, 116, 3  (nrow, ncol, nlyr)
#> resolution  : 3856.617, 3856.617  (x, y)
#> extent      : 2893583, 3340950, 2019451, 2362690  (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
#> source(s)   : memory
#> names       : cellindex, rowindex, colindex 
#> min values  :         1,        1,        1 
#> max values  :     10324,       89,      116 

as_coordinates(r, as.raster = TRUE) %>% plot()