Learn R Programming

terra (version 1.5-34)

gridDistance: Distance on a grid

Description

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.

Usage

# S4 method for SpatRaster
gridDistance(x, target=0, scale=1000, maxiter=50, filename="", ...)

Arguments

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 costDistance did not converge

filename

character. output filename (optional)

...

additional arguments as for writeRaster

Value

SpatRaster

See Also

See distance for "as the crow flies" distance

Examples

Run this code
# NOT RUN {
# 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