R raster: How to get the extent of raster with value?

403 Views Asked by At

There is a raster file with three bands. The spatial range of the data includes a large number of valueless areas. How to obtain the extent of the value area?enter image description here

1

There are 1 best solutions below

0
Robert Hijmans On

You can use terra::trim to remove all the outer rows and columns that only have NA values.

Example data

library(terra)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)

x <- extend(r, ext(c(0,10,40,60)))
plot(x)

Solution

y <- trim(x)
plot(y)