gridDistance {terra} | R Documentation |
The function calculates the distance to cells of a SpatRaster when the path has to go through the centers of the eight neighboring raster cells.
The distance is in meters if the coordinate reference system (CRS) of the SpatRaster is longitude/latitude (+proj=longlat
) and in the units of the CRS (typically meters) in other cases.
Distances are computed by summing local distances between cells, which are connected with their neighbors in 8 directions.
## S4 method for signature 'SpatRaster'
gridDistance(x, target=0, scale=1000, maxiter=50, filename="", ...)
x |
SpatRaster |
target |
numeric. value of the target cells (where to compute distance to) |
scale |
numeric. Scale factor for longitude/latitude data (1 = m, 1000 = km) |
maxiter |
numeric. The maximum number of iterations. Increase this number if you get the warning that |
filename |
character. output filename (optional) |
... |
additional arguments as for |
SpatRaster
see terra:::.oldGridDistance for the previous version of this method (will be removed in a future version)
See distance
for "as the crow flies" distance
# global lon/lat raster
r <- rast(ncol=10,nrow=10, vals=1)
r[48] <- 0
r[66:68] <- NA
d <- gridDistance(r)
plot(d)
# planar
crs(r) <- "+proj=utm +zone=15 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
d <- gridDistance(r)
plot(d)
# distance to cells that are not NA
rr <- classify(r, cbind(1, NA))
dd <- gridDistance(rr, NA)