If y is missing this method computes the distance, for all cells that are NA in SpatRaster x to the nearest cell that is not NA (or other values, see arguments "target" and "exclude").
If y is a numeric value, the cells with that value are ignored. That is, distance to or from these cells is not computed (only if grid=FALSE).
If y is a SpatVector, the distance to that SpatVector is computed for all cells, optionally after rasterization.
The distance is always expressed in meter if the coordinate reference system is longitude/latitude, and in map units otherwise. Map units are typically meter, but inspect crs(x) if in doubt.
Results are more precise, sometimes much more precise, when using longitude/latitude rather than a planar coordinate reference system, as these distort distance.
If x is a SpatVector :
If y is missing, a distance matrix between all objects in x is computed. A distance matrix object of class "dist" is returned.
If y is a SpatVector the geographic distance between all objects is computed (and a matrix is returned). If both sets have the same number of points, and pairwise=TRUE, the distance between each pair of objects is computed, and a vector is returned.
If x is a matrix :
x should consist of two columns, the first with "x" (or longitude) and the second with "y" coordinates (or latitude). If y is a also a matrix, the distance between each points in x and all points in y is computed, unless pairwise=TRUE
If y is missing, the distance between each points in x with all other points in x is computed, unless sequential=TRUE
## S4 method for signature 'SpatRaster,missing'distance(x, y, target=NA, exclude=NULL, unit="m", method="haversine", maxdist=NA, values=FALSE, filename="",...)## S4 method for signature 'SpatRaster,SpatVector'distance(x, y, unit="m", rasterize=FALSE, method="cosine", filename="",...)## S4 method for signature 'SpatVector,ANY'distance(x, y, sequential=FALSE, pairs=FALSE, symmetrical=TRUE, unit="m", method="geo")## S4 method for signature 'SpatVector,SpatVector'distance(x, y, pairwise=FALSE, unit="m", method="cosine")## S4 method for signature 'matrix,matrix'distance(x, y, lonlat, pairwise=FALSE, unit="m", method="geo")## S4 method for signature 'matrix,missing'distance(x, y, lonlat, sequential=FALSE, pairs=FALSE, symmetrical=TRUE, unit="m", method="geo")
Arguments
x: SpatRaster, SpatVector, or two-column matrix with coordinates (x,y) or (lon,lat)
y: missing, numeric, SpatVector, or two-column matrix
target: numeric. The value of the cells for which distances to cells that are not NA should be computed
exclude: numeric. The value of the cells that should not be considered for computing distances
unit: character. Can be either "m" or "km"
method: character. One of "geo", "cosine" or "haversine" (the latter cannot be used for distances to lines or polygons). With "geo" the most precise but slower method of Karney (2003) is used. The other two methods are faster but less precise
maxdist: numeric. Distance above this values are not set to NA
values: logical. If TRUE, the value of the nearest non-target cell is returned instead of the distance to that cell
rasterize: logical. If TRUE distance is computed from the cells covered by the geometries after rasterization. This can be much faster in some cases
filename: character. Output filename
...: additional arguments for writing files as in writeRaster
sequential: logical. If TRUE, the distance between sequential geometries is returned
pairwise: logical. If TRUE and if x and y have the same size (number of rows), the pairwise distances are returned instead of the distances between all elements
lonlat: logical. If TRUE the coordinates are interpreted as angular (longitude/latitude). If FALSE they are interpreted as planar
pairs: logical. If TRUE a "from", "to", "distance" matrix is returned
symmetrical: logical. If TRUE and pairs=TRUE, the distance between a pair is only included once. The distance between geometry 1 and 3 is included, but the (same) distance between 3 and 1 is not
Returns
SpatRaster, numeric, matrix, or a distance matrix (object of class "dist")
Note
A distance matrix can be coerced into a regular matrix with as.matrix
See Also
nearest, nearby
References
Karney, C.F.F., 2013. Algorithms for geodesics, J. Geodesy 87: 43-55. doi:10.1007/s00190-012-0578-z.