Re: [R] Converting coordinates to actual distances
Paul Brewin wrote: Hello, I've been searching for a method of converting Lat/Lon decimal coordinates into actual distances between points, and taking into account the curvature of the earth. Is there such a package in R? I've looked at the GeoR package, but this does not seem to contain what I am looking for. Ideally the output would be a triangular matrix of distances. Hi Paul, Below is an implementation of the Haversine formula that I once had to use for calculating distances between telephone exchanges. It's in C, but could be translated to R fairly easily. I've included a degrees to radians conversion as well. Jim #define APOSTROPHE 39 #define EARTH_RADIUS 6367 typedef struct { char *basename; char *number[2]; double lat[2]; double lon[2]; char delimiter[4]; char *data02; char *data03; char *data07; char *data08; FILE *input; FILE *output; } PSTS_INFO; /* DegreesToRadians Converts a string of the form nnndnn'nn[NSEW] to a value in radians. East (E) and South (S) are arbitrarily negative. Note that this routine will generate garbage if not passed a string of the correct form, although it is unlikely to do too much damage. Return valuevalue of the string in radians -10 input not correct format */ double DegreesToRadians(char *dms) { int index = 0; double degrees; double minutes = 0; double seconds = 0; int mult; degrees = atof(dms); while(isdigit(dms[index])) index++; if(dms[index++] == 'd') { minutes = atof(dms[index]); minutes /= 60; while(isdigit(dms[index])) index++; if(dms[index++] == APOSTROPHE) { seconds = atof(dms[index]); seconds /= 360; while(isdigit(dms[index]) dms[index]) index++; if(dms[index++] == QUOTES) { mult = (dms[index] == 'S' || dms[index] == 'E') ? -1 : 1; degrees += minutes + seconds; return(mult * degrees * M_PI/180); } } } return(-10); } /* GreatCircleDistance (Formula and recommendations for calculation taken from: http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1 by Bob Chamberlain - [EMAIL PROTECTED]) Calculates 'great circle' distances using the Haversine formula, based upon a spherical earth of EARTH_RADIUS radius. This version extracts the two latitude/longitude pairs (as radians) from the PSTS_INFO structure. */ double GreatCircleDist(PSTS_INFO *psts_info) { double a,d,dlon,dlat; dlon = psts_info-lon[1] - psts_info-lon[0]; dlat = psts_info-lat[1] - psts_info-lat[0]; a = pow(sin(dlat / 2),2) + cos(psts_info-lat[0]) * cos(psts_info-lat[1]) * pow(sin(dlon / 2),2); /* The penultimate result may be calculated either way - the first is bulletproof, the second is a bit faster */ d = 2 * atan2(sqrt(a),sqrt(1 - a)); /* d = 2 * asin(sqrt(a));*/ return(d * EARTH_RADIUS); } __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Converting coordinates to actual distances
Hello, I've been searching for a method of converting Lat/Lon decimal coordinates into actual distances between points, and taking into account the curvature of the earth. Is there such a package in R? I've looked at the GeoR package, but this does not seem to contain what I am looking for. Ideally the output would be a triangular matrix of distances. Thanks in advance, Paul Brewin Paul E Brewin (PhD) Center for Research in Biological Systems University of California San Diego 9500 Gilman Drive MC 0505 La Jolla CA, 92093-0505 USA Ph: 858-822-0871 Fax: 858-822-3631 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Converting coordinates to actual distances
Paul Brewin wrote: Hello, I've been searching for a method of converting Lat/Lon decimal coordinates into actual distances between points, and taking into account the curvature of the earth. Is there such a package in R? I've looked at the GeoR package, but this does not seem to contain what I am looking for. Ideally the output would be a triangular matrix of distances. Thanks in advance, Paul Brewin Paul E Brewin (PhD) Center for Research in Biological Systems University of California San Diego 9500 Gilman Drive MC 0505 La Jolla CA, 92093-0505 USA Ph: 858-822-0871 Fax: 858-822-3631 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html See http://www.meridianworlddata.com/Distance-Calculation.asp. The calculations are trivial. -- Bob Wheeler --- http://www.bobwheeler.com/ ECHIP, Inc. --- Randomness comes in bunches. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Converting coordinates to actual distances
See http://finzi.psych.upenn.edu/R/Rhelp02a/archive/29117.html On 9/14/05, Paul Brewin [EMAIL PROTECTED] wrote: Hello, I've been searching for a method of converting Lat/Lon decimal coordinates into actual distances between points, and taking into account the curvature of the earth. Is there such a package in R? I've looked at the GeoR package, but this does not seem to contain what I am looking for. Ideally the output would be a triangular matrix of distances. Thanks in advance, Paul Brewin Paul E Brewin (PhD) Center for Research in Biological Systems University of California San Diego 9500 Gilman Drive MC 0505 La Jolla CA, 92093-0505 USA Ph: 858-822-0871 Fax: 858-822-3631 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Converting coordinates to actual distances
On Wed, 14 Sep 2005, Paul Brewin wrote: Hello, I've been searching for a method of converting Lat/Lon decimal coordinates into actual distances between points, and taking into account the curvature of the earth. Is there such a package in R? I've looked at the GeoR package, but this does not seem to contain what I am looking for. Ideally the output would be a triangular matrix of distances. Using C code in the sp package (which will be exposed at the R level shortly) and sp 0.8 and maptools 0.5: library(maptools) Loading required package: foreign Loading required package: sp xx - readShapePoly(system.file(shapes/sids.shp, package=maptools)[1]) ll - getSpPPolygonsLabptSlots(xx) # ll is a matrix of long-lat centroids of North Carolina county polygons str(ll) num [1:100, 1:2] -81.5 -81.1 -79.3 -79.8 -78.7 ... plot(ll) x - as.double(ll[,1]) y - as.double(ll[,2]) n - as.integer(length(x)) dists - vector(mode=double, length=n) lonlat - as.integer(1) res - matrix(as.double(NA), 100, 100) for (i in 1:100) res[i,] - .C(sp_dists, x, y, x[i], y[i], n, dists, + lonlat)[[6]] gives a full matrix measured in kilometers for the WGS-84 ellipsoid. Accessing the C function like this puts the responsibility for checking the argument modes on the user. If this seems scary, rdist.earth() in the fields package has an R version of this. But maybe you need the actual functions to use great circle distance instead of Euclidean, rather than just to generate a distance matrix? Hope this helps, Roger Bivand Thanks in advance, Paul Brewin Paul E Brewin (PhD) Center for Research in Biological Systems University of California San Diego 9500 Gilman Drive MC 0505 La Jolla CA, 92093-0505 USA Ph: 858-822-0871 Fax: 858-822-3631 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html