Learn R Programming

terra (version 1.9-25)

cells: Get cell numbers

Description

Get the cell numbers covered by a SpatVector or SpatExtent. Or that match values in a vector; or all non NA values.

Usage

# S4 method for SpatRaster,missing
cells(x, y)

# S4 method for SpatRaster,numeric cells(x, y, pairs=FALSE)

# S4 method for SpatRaster,SpatVector cells(x, y, method="simple", weights=FALSE, exact=FALSE, touches=is.lines(y), small=TRUE)

# S4 method for SpatRaster,SpatExtent cells(x, y)

Arguments

Value

numeric vector or matrix

Examples

Run this code
r <- rast(ncols=10, nrows=10)
values(r) <- 1:ncell(r)
r[c(1:25, 31:100)] <- NA
r <- ifel(r > 28, r + 10, r)

# all cell numbers of cells that are not NA
cells(r)

# cell numbers that match values
x <- cells(r, c(28,38))
x$lyr.1

# cells for points
m <- cbind(x=c(0,10,-30), y=c(40,-10,20))
cellFromXY(r, m)

v <- vect(m)
cells(r, v)
cells(r, v, method="bilinear")

# cells for polygons
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v)
cv <- cells(r, v) 

Run the code above in your browser using DataLab