Dear R,
I have a bunch of geographic locations specified by latlong coordinates. What's an easy way to calculate geographic distance between any two points? OR, perhaps there is a function for calculating a distance matrix for K sites? Sincerely, Scott Chamberlain [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/rhelp PLEASE do read the posting guide http://www.Rproject.org/postingguide.html and provide commented, minimal, selfcontained, reproducible code. 
I found something here http://www.biostat.umn.edu/~sudiptob/Software/distonearth.R
#The following program computes the distance on the surface of the earth between two points point1 and point2. Both the points are of the form (Longitude, Latitude) geodetic.distance < function(point1, point2) { R < 6371 p1rad < point1 * pi/180 p2rad < point2 * pi/180 d < sin(p1rad[2])*sin(p2rad[2])+cos(p1rad[2])*cos(p2rad[2])*cos(abs(p1rad[1]p2rad[1])) d < acos(d) R*d } Dirk 
In reply to this post by Scott Chamberlain3
Hi Scott,
have a look at the 'earth.dist'function in the package 'fossil'. hth. Am 11.04.2011 17:37, schrieb Scott Chamberlain: > Dear R, > > > I have a bunch of geographic locations specified by latlong coordinates. What's an easy way to calculate geographic distance between any two points? OR, perhaps there is a function for calculating a distance matrix for K sites? > > > Sincerely, > Scott Chamberlain > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/rhelp > PLEASE do read the posting guide http://www.Rproject.org/postingguide.html > and provide commented, minimal, selfcontained, reproducible code.  Eik Vettorazzi Institut für Medizinische Biometrie und Epidemiologie Universitätsklinikum HamburgEppendorf Martinistr. 52 20246 Hamburg T ++49/40/741058243 F ++49/40/741057790 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/rhelp PLEASE do read the posting guide http://www.Rproject.org/postingguide.html and provide commented, minimal, selfcontained, reproducible code. 
> I have a bunch of geographic locations specified by latlong
> coordinates. What's an easy way to calculate geographic distance > between any two points? OR, perhaps there is a function for > calculating a distance matrix for K sites? A comparison of some geographic distance calculations is provided at http://pinedakrch.com/2010/11/23/greatcircledistancecalculationsinr/ , along with code for calculating the Vincenty inverse formula, which relies on the WGS84 ellipsoid approximations. The author compares the results to fields::rdist.earth, which seems to rely on a spherical model of the earth. It would be interesting to compare it to other distance functions as well. I found that the function provided at the above URL did not handle the case of coincident points. Adding the following line after the while loop fixed this. if (iterLimit==100) return(0) # formula began with nearly or exactly coincident points Enjoy the days, cur  Curt Seeliger, Data Ranger Raytheon Information Services  Contractor to ORD [hidden email] 541/7544638 [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/rhelp PLEASE do read the posting guide http://www.Rproject.org/postingguide.html and provide commented, minimal, selfcontained, reproducible code. 
Thanks very much for the help!
Scott On Monday, April 11, 2011 at 12:54 PM, [hidden email] wrote: > I have a bunch of geographic locations specified by latlong > > coordinates. What's an easy way to calculate geographic distance > > between any two points? OR, perhaps there is a function for > > calculating a distance matrix for K sites? > > A comparison of some geographic distance calculations is provided at > http://pinedakrch.com/2010/11/23/greatcircledistancecalculationsinr/ > , along with code for calculating the Vincenty inverse formula, which > relies on the WGS84 ellipsoid approximations. > > The author compares the results to fields::rdist.earth, which seems to > rely on a spherical model of the earth. It would be interesting to > compare it to other distance functions as well. > > I found that the function provided at the above URL did not handle the > case of coincident points. Adding the following line after the while loop > fixed this. > if (iterLimit==100) return(0) # formula began with nearly or > exactly coincident points > > Enjoy the days, > cur >  > Curt Seeliger, Data Ranger > Raytheon Information Services  Contractor to ORD > [hidden email] > 541/7544638 > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/rhelp > PLEASE do read the posting guide http://www.Rproject.org/postingguide.html > and provide commented, minimal, selfcontained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/rhelp PLEASE do read the posting guide http://www.Rproject.org/postingguide.html and provide commented, minimal, selfcontained, reproducible code. 
> A comparison of some geographic distance calculations is provided at
> http://pinedakrch.com/2010/11/23/greatcircledistancecalculationsinr/ > , along with code for calculating the Vincenty inverse formula, which > relies on the WGS84 ellipsoid approximations. You know, Scott, I should have included some test results of that method. Comparing the distances with Arc 9 indicates that the accuracy varies with location and whether there is a longitudinal difference in the two points. Comparing calculation results for points shifted 0 secs to 10 degrees North, West and Northwest from a 'base' point, the relative errors (defined as (Arc9.distance  Vincenty.distance)/Arc9.distance) range up to 0.08 in AK, AZ, CA, MT, NE, NM, UT, WA and WY, and range only up to 0.009 otherwise. In the special case of zero longitudinal offset (NorthSouth distances only), the relative error ranges to 0.006 in those states and to 2E7 otherwise. Let us know if you can do better, cur  Curt Seeliger, Data Ranger Raytheon Information Services  Contractor to ORD [hidden email] 541/7544638 [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/rhelp PLEASE do read the posting guide http://www.Rproject.org/postingguide.html and provide commented, minimal, selfcontained, reproducible code. 
Hi Curt,
Thanks for the help. According to that blogpost you sent, I am using the function gcd.hf using the Haversine formula. I wrapped it up in a function called CalcDists so that I can get a distance matrix between N sites. I don't know much about calculating distances, so does this seem like a good way to go (using Haversine that is)? # Convert degrees to radians deg2rad < function(deg) return(deg*pi/180) # Calculates the geodesic distance between two points specified by # radian latitude/longitude using the Haversine formula gcd.hf < function(long1, lat1, long2, lat2) { R < 6371 # Earth mean radius [km] delta.long < (long2  long1) delta.lat < (lat2  lat1) a < sin(delta.lat/2)^2 + cos(lat1) * cos(lat2) * sin(delta.long/2)^2 c < 2 * asin(min(1,sqrt(a))) d = R * c return(d) # Distance in km } # Fxn to calculate matrix of distances between each two sites CalcDists < function(latlongs) { name < list(rownames(latlongs), rownames(latlongs)) n < nrow(latlongs) z < matrix(0, n, n, dimnames = name) for (i in 1:n) { for (j in 1:n) z[i, j] < gcd.hf(long1 = latlongs[i, 1], lat1 = latlongs[i, 2], long2 = latlongs[j, 1], lat2 = latlongs[j,2]) } z < as.dist(z) return(z) } Scott On Monday, April 11, 2011 at 5:00 PM, [hidden email] wrote: > > A comparison of some geographic distance calculations is provided at > > http://pinedakrch.com/2010/11/23/greatcircledistancecalculationsinr/ > > , along with code for calculating the Vincenty inverse formula, which > > relies on the WGS84 ellipsoid approximations. > > You know, Scott, I should have included some test results of that method. Comparing the distances with Arc 9 indicates that the accuracy varies with location and whether there is a longitudinal difference in the two points. Comparing calculation results for points shifted 0 secs to 10 degrees North, West and Northwest from a 'base' point, the relative errors (defined as (Arc9.distance  Vincenty.distance)/Arc9.distance) range up to 0.08 in AK, AZ, CA, MT, NE, NM, UT, WA and WY, and range only up to 0.009 otherwise. In the special case of zero longitudinal offset (NorthSouth distances only), the relative error ranges to 0.006 in those states and to 2E7 otherwise. > > Let us know if you can do better, > cur > >  > Curt Seeliger, Data Ranger > Raytheon Information Services  Contractor to ORD > [hidden email] > 541/7544638 > [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/rhelp PLEASE do read the posting guide http://www.Rproject.org/postingguide.html and provide commented, minimal, selfcontained, reproducible code. 
In reply to this post by Scott Chamberlain3
Please allow me to present you the ultimate distance calculation algorithm by Charles Karney, accurate to one nanometre:
http://geographiclib.sourceforge.net/cgibin/Geod

Powered by Nabble  Edit this page 