terra (version 1.0-10)

distance: Geographic distance

Description

If x is a SpatRaster:

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. If argument grid=TRUE, the distance is computed using a path that goes through the centers of the 8 neighboring cells.

If y is a SpatVector, the distance to that SpatVector is computed for all cells. For lines and polygons this is done after rasterization (for now).

If x is a SpatVector:

If y is missing, a distance matrix between all object in x is computed. An 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.

Usage

# S4 method for SpatRaster,missing
distance(x, y, grid=FALSE, filename="", overwrite=FALSE, wopt=list(), ...)

# S4 method for SpatRaster,SpatVector distance(x, y, filename="", overwrite=FALSE, wopt=list(), ...)

# S4 method for SpatVector,ANY distance(x, y, sequential=FALSE, pairs=FALSE, symmetrical=TRUE, ...)

# S4 method for SpatVector,SpatVector distance(x, y, pairwise=FALSE, ...)

# S4 method for matrix,matrix distance(x, y, lonlat, pairwise=FALSE, ...)

# S4 method for matrix,ANY distance(x, y, lonlat, sequential=FALSE, ...)

Arguments

x

SpatRaster, SpatVector, or two-column matrix (x,y) or (lon,lat)

y

missing or SpatVector, or two-column matrix

grid

logical. If TRUE, distance is computed using a path that goes through the centers of the 8 neighboring cells

filename

character. Output filename. Optional

overwrite

logical. If TRUE, filename is overwritten

wopt

list. Options for writing files as in writeRaster

...

additional arguments. None implemented

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

Value

SpatRaster or numeric or matrix or distance matrix (object of class "dist")

Examples

Run this code
# NOT RUN {
#lonlat
r <- rast(ncol=36,nrow=18, crs="+proj=longlat +datum=WGS84")
v <- rep(NA, ncell(r))
v[500] <- 1
values(r) <- v
d <- distance(r) 
plot(d / 100000)

#planar
r <- rast(ncol=36,nrow=18, crs="+proj=utm +zone=1 +datum=WGS84")
v <- rep(NA, ncell(r))
v[500] <- 1
values(r) <- v
d <- distance(r) 

p1 <- vect(rbind(c(0,0), c(90,30), c(-90,-30)), crs="+proj=longlat +datum=WGS84")
dp <- distance(r, p1) 

d <- distance(p1)
d
as.matrix(d)

p2 <- vect(rbind(c(30,-30), c(25,40), c(-9,-3)), crs="+proj=longlat +datum=WGS84")
dd <- distance(p1, p2)
dd
pd <- distance(p1, p2, pairwise=TRUE)
pd
pd == diag(dd)


# polygons, lines
crs <- "+proj=utm +zone=1"
p1 <- vect("POLYGON ((0 0, 8 0, 8 9, 0 9, 0 0))", crs=crs)
p2 <- vect("POLYGON ((5 6, 15 6, 15 15, 5 15, 5 6))", crs=crs)
p3 <- vect("POLYGON ((2 12, 3 12, 3 13, 2 13, 2 12))", crs=crs)
p <- c(p1, p2, p3)
L1 <- vect("LINESTRING(1 11, 4 6, 10 6)", crs=crs)
L2 <- vect("LINESTRING(8 14, 12 10)", crs=crs)
L3 <- vect("LINESTRING(1 8, 12 14)", crs=crs)
lns <- c(L1, L2, L3)
pts <- vect(cbind(c(7,10,10), c(3,5,6)), crs=crs)

distance(p1,p3)
distance(p)
distance(p,pts)
distance(p,lns)
distance(pts,lns)
# }

Run the code above in your browser using DataLab