An alternative interface to the main geodist function that directly accepts inputs as individual vectors of coordinates, rather than the matrix or `data.frame` inputs of the main function. This interface is provided for cases where computational efficiency is important, and will generally provide faster results than the main function.
geodist_vec(
x1,
y1,
x2,
y2,
paired = FALSE,
sequential = FALSE,
pad = FALSE,
measure = "cheap",
quiet = FALSE
)Numeric vector of longitude coordinates
Numeric vector of latitude coordinates
Optional second numeric vector of longitude coordinates
Optional second numeric vector of latitude coordinates
If TRUE, calculate paired distances between each entry
in (x1, y1) and (x2, y2), returning a single vector.
If TRUE, calculate (vector of) distances
sequentially along (x1, y1) (when no (x2, y2) are passed),
otherwise calculate matrix of pairwise distances between all points.
If sequential = TRUE values are padded with initial
NA to return n values for inputs of length n, otherwise
return n - 1 values.
One of "haversine" "vincenty", "geodesic", or "cheap" specifying desired method of geodesic distance calculation; see Notes.
If FALSE, check whether max of calculated distances
is greater than accuracy threshold and warn.
If only (x1, y1) are passed and sequential = FALSE, a
square symmetric matrix containing distances between all items in (x1,
y1); If only (x1, y1) are passed and sequential = TRUE, a
vector of sequential distances between matching elements of (x1, y1);
otherwise if (x2, y2) are passed, a matrix of lenght(x1) ==
length(y1) rows and length(x2) == length(y2) columns.
measure = "cheap" denotes the mapbox cheap ruler
https://github.com/mapbox/cheap-ruler-cpp; measure = "geodesic"
denotes the very accurate geodesic methods given in Karney (2013)
"Algorithms for geodesics" J Geod 87:43-55, and as provided by the
`st_distance()` function from the sf package.
n <- 50
# Default "cheap" distance measure is only accurate for short distances:
x1 <- -1 + 2 * runif (n, -0.1, 0.1)
y1 <- -1 + 2 * runif (n, -0.1, 0.1)
d0 <- geodist_vec (x1, y1) # A 50-by-50 matrix
d2 <- geodist_vec (x1, y1, sequential = TRUE) # Vector of length 49
d2 <- geodist_vec (x1, y1, sequential = TRUE, pad = TRUE) # length 50
x2 <- -10 + 20 * runif (2 * n, -0.1, 0.1)
y2 <- -10 + 20 * runif (2 * n, -0.1, 0.1)
d1 <- geodist_vec (x1, y1, x2, y2) # A 50-by-100 matrix
#> Maximum distance is > 100km. The 'cheap' measure is inaccurate over such
#> large distances, you'd likely be better using a different 'measure',
#> one of 'haversine', 'vincenty', or 'geodesic'.