| 1 |
spDistsN1 <- function(pts, pt, longlat=FALSE) { |
| 2 |
if (inherits(pts, "SpatialPoints")) |
| 3 |
pts <- coordinates(pts) |
| 4 |
if (!is.matrix(pts)) stop("pts must be a matrix") |
| 5 |
if (ncol(pts) != 2) stop("pts must have two columns") |
| 6 |
if (!is.numeric(pts)) stop("pts must be numeric") |
| 7 |
if (inherits(pt, "SpatialPoints")) |
| 8 |
pt <- coordinates(pt) |
| 9 |
if (!is.numeric(pt)) stop("pt must be numeric") |
| 10 |
if (length(pt) != 2) stop("pt must have length two") |
| 11 |
storage.mode(pts) <- "double" |
| 12 |
storage.mode(pt) <- "double" |
| 13 |
x <- pts[,1] |
| 14 |
y <- pts[,2] |
| 15 |
xx <- pt[1] |
| 16 |
yy <- pt[2] |
| 17 |
n <- as.integer(length(x)) |
| 18 |
dists <- vector(mode="double", length=n) |
| 19 |
lonlat <- as.integer(longlat) |
| 20 |
res <- .C("sp_dists", x, y, xx, yy, n, dists, lonlat, |
| 21 |
PACKAGE = "sp")[[6]] |
| 22 |
if (any(!is.finite(res))) { |
| 23 |
nAn <- which(!is.finite(res)) |
| 24 |
dx <- abs(x[nAn] - xx) |
| 25 |
dy <- abs(y[nAn] - yy) |
| 26 |
if (all((c(dx, dy) < .Machine$double.eps ^ 0.5))) |
| 27 |
res[nAn] <- 0 |
| 28 |
else |
| 29 |
stop(paste("non-finite distances in spDistsN1")) |
| 30 |
} |
| 31 |
res |
| 32 |
} |
| 33 |
|
| 34 |
spDists <- function(x, y = x, longlat = FALSE, segments = FALSE, diagonal = FALSE) { |
| 35 |
if (segments) |
| 36 |
stopifnot(missing(y)) |
| 37 |
if (diagonal) |
| 38 |
stopifnot(! missing(y)) |
| 39 |
missing.y = missing(y) # assigning y later on changes missing(y) |
| 40 |
if (is(x, "Spatial")) { |
| 41 |
if (! missing(y)) |
| 42 |
stopifnot(identicalCRS(x, y)) |
| 43 |
ll = !is.na(is.projected(x)) && !is.projected(x) |
| 44 |
if (!missing(longlat) && longlat != ll) |
| 45 |
warning(paste("spDists: argument longlat conflicts with CRS(x); using the value", longlat)) |
| 46 |
else |
| 47 |
longlat = ll |
| 48 |
x = coordinates(x) |
| 49 |
y = coordinates(y) |
| 50 |
} |
| 51 |
stopifnot(ncol(x) == ncol(y)) |
| 52 |
if (segments) { |
| 53 |
stopifnot(ncol(x) == 2) |
| 54 |
n = nrow(x) |
| 55 |
if (n == 1) return(0) |
| 56 |
res <- .C("sp_lengths", as.double(x[,1]), as.double(x[,2]), |
| 57 |
as.integer(n), vector(mode = "double", length=(n-1)), |
| 58 |
as.integer(longlat), PACKAGE = "sp")[[4]] |
| 59 |
if (any(!is.finite(res))) |
| 60 |
stop("non-finite segment lengths") |
| 61 |
res |
| 62 |
} else if (diagonal) { |
| 63 |
stopifnot(ncol(x) == 2) |
| 64 |
stopifnot(nrow(x) == nrow(y)) |
| 65 |
res = .C("sp_dists_NN", as.double(x[,1]), as.double(x[,2]), |
| 66 |
as.double(y[,1]), as.double(y[,2]), as.integer(nrow(x)), |
| 67 |
vector(mode = "double", length = nrow(x)), |
| 68 |
as.integer(longlat), PACKAGE = "sp")[[6]] |
| 69 |
if (any(!is.finite(res))) |
| 70 |
stop("non-finite lengths") |
| 71 |
res |
| 72 |
} else if (ncol(x) != 2) { |
| 73 |
if (longlat) |
| 74 |
stop("cannot compute spherical distances for longlat data in more than 2 dimensions") |
| 75 |
if (missing.y) |
| 76 |
as.matrix(dist(x)) |
| 77 |
else |
| 78 |
sqrt(Reduce("+", Map(function(i) outer(x[,i], y[,i], "-") ^ 2, 1:ncol(x)))) |
| 79 |
} else { |
| 80 |
spDiN1 = function(x, y, ll) spDistsN1(y, x, ll) |
| 81 |
if (nrow(x) < nrow(y)) |
| 82 |
matrix(t(apply(x, 1, spDiN1, y = y, ll = longlat)), nrow(x), nrow(y)) |
| 83 |
else |
| 84 |
matrix(apply(y, 1, spDiN1, y = x, ll = longlat), nrow(x), nrow(y)) |
| 85 |
} |
| 86 |
} |