Ricardo Bandin
2008-Nov-17 01:40 UTC
[R] How to calculate the linear distance between 2 points
Deemed colleagues I would appreciate your help with a sentence to calculate the linear distance between 2 geographical points (coordinates in UTM). In advance thnks for your attention, -- Ricardo Bandin Llanos rbandin@udec.cl Estudiante - Magíster Cs. m. Pesquerías Universidad de Concepción, Región del Bio-Bio, Chile Celular: (0056-41) 97949957 [[alternative HTML version deleted]]
jim holtman
2008-Nov-17 01:53 UTC
[R] How to calculate the linear distance between 2 points
Here are a couple of hits I got from searching the archives. Any of them help: http://finzi.psych.upenn.edu/R/library/spectralGP/html/rdist.earth.html http://finzi.psych.upenn.edu/R/library/RFOC/html/GreatDist.html On Sun, Nov 16, 2008 at 8:40 PM, Ricardo Bandin <rbandin at gmail.com> wrote:> Deemed colleagues > > I would appreciate your help with a sentence to calculate the linear > distance between 2 geographical points (coordinates in UTM). > > In advance thnks for your attention, > > -- > Ricardo Bandin Llanos > rbandin at udec.cl > Estudiante - Mag?ster Cs. m. Pesquer?as > Universidad de Concepci?n, Regi?n del Bio-Bio, Chile > Celular: (0056-41) 97949957 > > [[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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Jon Loehrke
2008-Nov-17 13:18 UTC
[R] How to calculate the linear distance between 2 points
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
b<-6357 #Polar radius km
e<-sqrt(1-b^2/a^2) #eccentricity of the ellipsoid that is Earth
# conversion
TAG_LAT_RAD<-lat1*pi/180
TAG_LONG_RAD<-long1*pi/180
RECAP_LAT_RAD<-lat2*pi/180
RECAP_LONG_RAD<-long2*pi/180
dLat<-RECAP_LAT_RAD-TAG_LAT_RAD
dLong<-RECAP_LONG_RAD-TAG_LONG_RAD
#calculates dist traveled using the Haversine function from release
and recapture positions in radians
hav<-(sin((dLat)/2))^2+
(cos(TAG_LAT_RAD)*cos(RECAP_LAT_RAD)*(sin((dLong)/2))^2)
dist_travel_rad<-2*atan2(sqrt(hav),sqrt(1-hav)) #ie half the versed
sine...the haversine formula
#distance traveled conversion, ie multplying by R with autocorrelating
R by latitude
DIST_TRAVEL_KM<-dist_travel_rad*(a*(1-e^2))/(1-
e^2*(sin(mean(c(TAG_LAT_RAD, RECAP_LAT_RAD)))^2)^(3/2))# define new
vector
# calculation of angular displacement
y<-sin(dLong)*cos(RECAP_LAT_RAD)
x<-cos(TAG_LAT_RAD)*sin(RECAP_LAT_RAD)-
sin(TAG_LAT_RAD)*cos(RECAP_LAT_RAD)*cos(dLong)
PHI_RAD<-2*pi-atan2(y,x)
PHI_DEG<-((PHI_RAD*180.0/pi)+360.0)%%360.0
#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
Graduate Research Assistant
Department of Fisheries Oceanography
School for Marine Science and Technology
University of Massachusetts
200 Mill Road, Suite 325
Fairhaven, MA 02719
jloehrke@umassd.edu
T 508-910-6393
F 508-910-6396
[[alternative HTML version deleted]]