geodist {geodist}R Documentation

geodist.

Description

Convert one or two rectangular objects containing lon-lat coordinates into vector or matrix of geodesic distances.

Usage

geodist(x, y, paired = FALSE, sequential = FALSE, pad = FALSE,
  measure = "cheap")

Arguments

x

Rectangular object (matrix, data.frame, tibble, whatever) containing longitude and latitude coordinates.

y

Optional second object which, if passed, results in distances calculated between each object in x and each in y.

paired

If TRUE, calculate paired distances between each entry in x and y, returning a single vector.

sequential

If TRUE, calculate (vector of) distances sequentially along x (when no y is 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 input with n rows, otherwise return n - 1 values.

measure

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

Value

If only x passed and sequential = FALSE, a square symmetric matrix containing distances between all items in x; If only x passed and sequential = TRUE, a vector of sequential distances between rows of x; otherwise if y is passed, a matrix of nrow(x) rows and nrow(y) columns.

Note

measure = "cheap" denotes the mapbox cheap ruler https://github.com/mapbox/cheap-ruler-cpp; measure = "geodetic" denotes the very accurate geodetic methods given in Kearney (2013) "Algorithms for geodesics" J Geod 87:43-55, and as provided by the codesf::st_dist() function.

Examples

n <- 50
x <- cbind (-10 + 20 * runif (n), -10 + 20 * runif (n))
y <- cbind (-10 + 20 * runif (2 * n), -10 + 20 * runif (2 * n))
colnames (x) <- colnames (y) <- c ("x", "y")
d0 <- geodist (x) # A 50-by-50 matrix
d1 <- geodist (x, y) # A 50-by-100 matrix
d2 <- geodist (x, sequential = TRUE) # Vector of length 49
d2 <- geodist (x, sequential = TRUE, pad = TRUE) # Vector of length 50
d0_2 <- geodist (x, measure = "geodesic") # nanometre-accurate version of d0

[Package geodist version 0.0.3 Index]