[R-sig-Geo] earth distance between points
Michael Sumner
mdsumner at utas.edu.au
Wed Apr 19 02:57:11 CEST 2006
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.
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.
More information about the R-sig-Geo
mailing list