Skip to contents

Get the great circle distance between WGS84 lat/long points

Usage

get_gcdist(pt1 = NULL, pt2 = NULL, units = c("m", "km", "rads"), simple = TRUE)

Arguments

pt1

`sf` object with point geometry, `sfc_POINT` object, `sfg` point, data frame or matrix.

pt2

`sf` object with point geometry, `sfc_POINT` object, `sfg` point, data frame or matrix.

units

whether to return the great circle distance in meters, kilometers, or radians.

simple

whether to return a numeric vector of distances or a `data.frame` containing start and end coordinates as well as distance.

Value

Numeric vector of point to point distances, or data frame of origin and destination coordinates accompanied by their distances.

Note

This functionality also exists in R packages sp, sf, geosphere and fields. H3's version appears to return slightly shorter distances than most other implementations, but is included here for completeness.

Examples

# distance between Brisbane and Melbourne
bne <- c(153.028, -27.468)
mlb <- c(144.963, -37.814)
get_gcdist(bne, mlb, 'km')
#> Assuming positions 1 and 2 contain x, y coordinates in EPSG:4326
#> Assuming positions 1 and 2 contain x, y coordinates in EPSG:4326
#> [1] 1331.349