
Last chance! 50% off unlimited learning
Sale ends in
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.
The shortest distance to the cells with the target
value is computed for all cells that are not NA
. Cells that are NA
cannot be traversed and are ignored, unless the target itself is NA
, in which case the distance to the nearest cell that is not NA
is computed for all cells that are NA
.
# S4 method for SpatRaster
gridDistance(x, target=0, scale=1000, maxiter=50, filename="", ...)
SpatRaster
SpatRaster
numeric. value of the target cells (where to compute distance to)
numeric. Scale factor for longitude/latitude data (1 = m, 1000 = km)
numeric. The maximum number of iterations. Increase this number if you get the warning that costDistance did not converge
character. output filename (optional)
additional arguments as for writeRaster
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)
Run the code above in your browser using DataLab