[R-sig-Geo] earth distance between points

Michael Sumner mdsumner at utas.edu.au
Wed Apr 19 09:00:26 CEST 2006


Thanks Roger.

trackDistance <-
function (track, longlat = FALSE)
{
    if (!is.matrix(track))
        stop("track must be two-column matrix")
    if (ncol(track) != 2)
        stop("track must be two-column matrix")
    n1 <- nrow(track) - 1
    if (n1 < 2)
        stop("less than two points")
    res <- numeric(n1)
    for (i in seq(along = res))
      res[i] <- spDistsN1(track[i,,drop = FALSE],
                          track[(i + 1),,drop = FALSE ], longlat = longlat)
    res
}


ll <- matrix(c(5, 6, 60, 60), ncol=2)
      km <- spDistsN1(ll, ll[1,], longlat=TRUE)
      zapsmall(km)

ll.trk <- matrix(c(5, 6, 8, 8,  60, 60, 60,  60), ncol=2)
km.trk <- trackDistance(ll.trk, longlat = TRUE)
zapsmall(km.trk)


On Wed, 19 Apr 2006, Michael Sumner wrote:


> > Further to my SpatialPoints TimeOrderedRecords interest:  sp has a 
> > function spDistsN1 to calculate distances
> > between 1 and many other locations - I would like to calculate distances 
> > between succesive locations.
>   

Have a look inside the existing function and *apply or loop over the marix 
rows for .C("sp_dists", ...). There is a similar approach to azimuths in 
the latest maptools (trackAzimuth()). Yes, the current solution is 
specific, and was an attempt to centralise GreatCircle compiled code. 
However, so far R packages cannot be compiled against each others' shared 
libraries, so inter-package calls need to be at the R level, or force code 
copying (which isn't a big problem, but is not good practice for 
maintenance).

Roge




More information about the R-sig-Geo mailing list