# Re: [R] How to calculate the linear distance between 2 points

From: Jon Loehrke <jloehrke_at_umassd.edu>
Date: Mon, 17 Nov 2008 08:18:13 -0500

I have used both the trigonometric identity (a^2 + b^2) = c^2, the haversine and the vincenty equations (http://www.movable-type.co.uk/scripts/latlong.html ), it really depends on how many calculations you need to make, how big of an error you can tolerate, and how far apart your measurements are.

Here is the haversine implementation, let me know if you would like the vincenty version.

haversine<-function(lat1, long1, lat2, long2){

# adapted from Chris Veness JavaScript (c) 2002-2007
# http://www.movable-type.co.uk/
# haversine.r
# calculates the distance traveled or the angle of displacement with
the Haversine function

#coefficients

```a<-6378 #Equatorial radius km
e<-sqrt(1-b^2/a^2) #eccentricity of the ellipsoid that is Earth

```

# conversion

#calculates dist traveled using the Haversine function from release

hav<-(sin((dLat)/2))^2+
#distance traveled conversion, ie multplying by R with autocorrelating
R by latitude

# calculation of angular displacement

#output

S<-list()
S\$Dist_km<-DIST_TRAVEL_KM # distance traveled in Km S\$PHI_DEG<-PHI_DEG # Angular displacement from North in deg. S
}

Jon Loehrke
Department of Fisheries Oceanography
School for Marine Science and Technology University of Massachusetts
Fairhaven, MA 02719
jloehrke_at_umassd.edu
T 508-910-6393
F 508-910-6396

[[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 17 Nov 2008 - 13:23:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 17 Nov 2008 - 14:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.