Liangzhong
Liangzhong

Reputation: 31

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

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

Upvotes: 0

Views: 632

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47061

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)

Upvotes: 0

Related Questions