Learn R Programming

terra (version 1.5-12)

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, origin, omit=NULL, chunk=FALSE, filename="", overwrite=FALSE, ...)

Arguments

x

SpatRaster

origin

value(s) of the cells from which the distance is calculated. If origin=NULL all cells that are not NA are origins

omit

value(s) of the cells that cannot be traversed (optional)

chunk

logical. If TRUE the data are processed in chunks. This may be necessary for large datasets, but could lead to errors in the case of complex areas that need to be omitted if they are spread over different chunks (meandering rivers, for instance).

filename

character. output filename (optional)

overwrite

logical. If TRUE, filename is overwritten

...

additional arguments as for writeRaster

Value

SpatRaster

Details

If the SpatRaster to be processed is big, it will be processed in chunks. This may lead to errors in the case of complex objects spread over different chunks (meandering rivers, for instance).

See Also

See distance for "as the crow flies" distance

Examples

Run this code
# NOT RUN {
#world lon/lat raster
r <- rast(ncol=10,nrow=10, vals=1)
r[48] <- 2
r[66:68] <- 3
d <- gridDistance(r,origin=2,omit=3) 
plot(d)

#UTM small area
crs(r) <- "+proj=utm +zone=15 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
d <- gridDistance(r,origin=2,omit=3) 
plot(d)

# }

Run the code above in your browser using DataLab