geodist_vec: geodist_vec

geodist_vecR Documentation

geodist_vec

Description

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.

Usage

geodist_vec(
  x1,
  y1,
  x2,
  y2,
  paired = FALSE,
  sequential = FALSE,
  pad = FALSE,
  measure = "cheap",
  quiet = FALSE
)

Arguments

x1

Numeric vector of longitude coordinates

y1

Numeric vector of latitude coordinates

x2

Optional second numeric vector of longitude coordinates

y2

Optional second numeric vector of latitude coordinates

paired

If TRUE, calculate paired distances between each entry in (x1, y1) and (x2, y2), returning a single vector.

sequential

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.

pad

If sequential = TRUE values are padded with initial NA to return n values for inputs of length n, otherwise return n - 1 values.

measure

One of "haversine" "vincenty", "geodesic", or "cheap" specifying desired method of geodesic distance calculation; see Notes.

quiet

If FALSE, check whether max of calculated distances is greater than accuracy threshold and warn.

Value

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.

Note

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_dist()' function from the sf package.

Examples

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

geodist documentation built on July 4, 2024, 1:11 a.m.