geodist_min: Minimal pairwise distances between two input matrices

geodist_minR Documentation

Minimal pairwise distances between two input matrices

Description

Convert two rectangular objects containing lon-lat coordinates into an index vector of the elements in the second matrix corresponding to the minimal distance to each element of the first matrix.

Usage

geodist_min(x, y, measure = "cheap", quiet = FALSE)

Arguments

x

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

y

Second rectangular object to be search for minimal distance to each row in the first object.

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

A integer index vector indexing elements of 'y' corresponding to minimal distances to each element of 'x'. The length of this vector is equal to the number of rows in 'x'.

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 codesf::st_dist() function.

Examples

n <- 50
# Default "cheap" distance measure is only accurate for short distances:
x <- cbind (runif (n, -0.1, 0.1), runif (n, -0.1, 0.1))
y <- cbind (runif (2 * n, -0.1, 0.1), runif (2 * n, -0.1, 0.1))
colnames (x) <- colnames (y) <- c ("x", "y")
index <- geodist_min (x, y, measure = "Haversine")
# 'index' is a vector of 50 values indexing elements of `y` corresponding to
# minimal distances to each element of `x`. It is exactly the same as the
# value returned by these lines:
d0 <- geodist (x, y, measure = "Haversine")
index0 <- apply (d0, 1, which.min)
identical (index, index0)

hypertidy/geodist documentation built on Jan. 17, 2024, 11:46 p.m.