[R-sig-Geo] distances between coordinates: alternatives?

Mario A. Pardo biomario at gmail.com
Fri Aug 31 21:05:44 CEST 2012


Hi,

I need to calculate the great circle distances between consecutive 
coordinates.

Let's say:

lon <- c(-115.8667, -115.8667, -115.8583, -115.8500, -115.8500, 
-115.8500, -115.8444, -115.8389, -115.8333, -115.8333, -115.8333)
lat <- c( 29.76670,  29.76670,  29.74170,  29.71670,  29.71670, 
29.71670,  29.68337,  29.65003,  29.61670,  29.61670,  29.60000)

The output vector that I need is:

 > distances

          0
     2.8990
     2.8963
          0
          0
     3.7496
     3.7493
     3.7496
          0
     1.8590

In which each value corresponds to the distance (in kilometers) between 
one coordinate and the previous. Obviously this vector is one value shorter.

I know that the function "rdist.earth" gives this result within a square 
matrix hidden as a diagonal, but it turns out that the position of that 
diagonal differs depending on the length of the coordinate vectors. 
That's a huge problem within a loop, because I'm dealing with thousands 
of sets of coordinates of different lengths and I can't extract the 
values iteratively. I just moved to R from Matlab, in which this was 
very straight forward with the "m_lldist" function.

Do you know another function to compute this?

Thanks in advance,

  
--
mario



More information about the R-sig-Geo mailing list