# earthDist: distance between lat-long coordinates In OSMscale: Add a Scale Bar to 'OpenStreetMap' Plots

## Description

Great-circle distance between points at lat-long coordinates. (The shortest distance over the earth's surface). The distance of all the entries is computed relative to the `i`th one.

## Usage

 `1` ```earthDist(lat, long, data, r = 6371, i = 1L) ```

## Arguments

 `lat, long` Latitude (North/South) and longitude (East/West) coordinates in decimal degrees `data` Optional: data.frame with the columns `lat` and `long` `r` radius of the earth. Could be given in miles. DEFAULT: 6371 (km) `i` Integer: Index element against which all coordinate pairs are computed. DEFAULT: 1

## Value

Vector with distance(s) in km (or units of `r`, if `r` is changed)

## Author(s)

Berry Boessenkool, berry-b@gmx.de, Aug 2016 + Jan 2017. Angle formula from Diercke Weltatlas 1996, Page 245

`degree` for pre-formatting, http://www.movable-type.co.uk/scripts/latlong.html
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24``` ```d <- read.table(header=TRUE, sep=",", text=" lat, long 52.514687, 13.350012 # Berlin 51.503162, -0.131082 # London 35.685024, 139.753365") # Tokio earthDist(lat, long, d) # from Berlin to L and T: 928 and 8922 km earthDist(lat, long, d, i=2) # from London to B and T: 928 and 9562 km # slightly different with other formulas: # install.packages("geosphere") # geosphere::distHaversine(as.matrix(d[1,2:1]), as.matrix(d[2,2:1])) / 1000 # compare with UTM distance set.seed(42) d <- data.frame(lat=runif(100, 47,54), long=runif(100, 6, 15)) d2 <- projectPoints(d\$lat, d\$long) d_utm <- berryFunctions::distance(d2\$x[-1],d2\$y[-1], d2\$x,d2\$y)/1000 d_earth <- earthDist(lat,long, d)[-1] plot(d_utm, d_earth) # distances in km hist(d_utm-d_earth) # UTM distance slightly larger than earth distance plot(d_earth, d_utm-d_earth) # correlates with distance berryFunctions::colPoints(d2\$x[-1], d2\$y[-1], d_utm-d_earth, add=FALSE) points(d2\$x,d2\$y, pch=3, cex=2, lwd=2) ```