[R-sig-Geo] earth distance between points

Roger Bivand Roger.Bivand at nhh.no
Wed Apr 19 08:34:04 CEST 2006


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).

Roger

> 
> Currently I use this with a clunky dispatch method of my own for great 
> circle (sphere) versus Euclidean, where a and b are
> subset matrices with the first and last row removed respectively, i.e.
> 
> x <- matrix(c(159, 143, 168, -43, -42, -54), ncol =2)
> dist.gc(x[-1,], x[-nrow(x),])
> 
> ## great circle (sphere)
> dist.gc <- function(a, b) {
>         r <- cos(pi/180 * a[, 2]) * cos(pi/180 * b[, 2]) * cos(pi/180 *
>             (b[, 1] - a[, 1])) + sin(pi/180 * a[, 2]) * sin(pi/180 *
>             b[, 2])
>         6378.137 * acos(pmin(r, 1))
>     }
> 
> ## Euclidean
>  dist <- function(a, b) {
>             sqrt(rowSums((a - b)^2))
>     }
> 
> Is there plans for a more general distance function in sp, or should I 
> work towards incorporating these functions - they usually suffice
> for my purposes but the sphere is not always going to be enough.
> 
>   spDistsN1 seems quite specific to a particular need.
> 
> Cheers, Mike.
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
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: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list