| 1 |
\name{spDistsN1} |
| 2 |
\alias{spDistsN1} |
| 3 |
\alias{spDists} |
| 4 |
%- Also NEED an '\alias' for EACH other topic documented here. |
| 5 |
\title{Euclidean or Great Circle distance between points} |
| 6 |
\description{ |
| 7 |
The function returns a vector of distances between a matrix of 2D points, first column longitude, second column latitude, and a single 2D point, using Euclidean or Great Circle distance (WGS84 ellipsoid) methods. |
| 8 |
} |
| 9 |
\usage{ |
| 10 |
spDistsN1(pts, pt, longlat = FALSE) |
| 11 |
spDists(x, y = x, longlat = FALSE, segments = FALSE, diagonal = FALSE) |
| 12 |
} |
| 13 |
%- maybe also 'usage' for other objects documented here. |
| 14 |
\arguments{ |
| 15 |
\item{pts}{A matrix of 2D points, first column x/longitude, second column y/latitude, or a SpatialPoints or SpatialPointsDataFrame object} |
| 16 |
\item{pt}{A single 2D point, first value x/longitude, second value y/latitude, or a SpatialPoints or SpatialPointsDataFrame object with one point only} |
| 17 |
\item{x}{A matrix of n-D points with row denoting points, first column x/longitude, second column y/latitude, or a Spatial object that has a \link{coordinates} method} |
| 18 |
\item{y}{A matrix of n-D points with row denoting points, first column x/longitude, second column y/latitude, or a Spatial object that has a \link{coordinates} method} |
| 19 |
\item{longlat}{logical; |
| 20 |
if FALSE, Euclidean distance, |
| 21 |
if TRUE Great Circle (WGS84 ellipsoid) distance; |
| 22 |
if \code{x} is a Spatial object, longlat should not be specified but will be derived |
| 23 |
from \link{is.projected}\code{(x)} } |
| 24 |
\item{segments}{logical; if \code{TRUE}, \code{y} must be missing; the vector of distances |
| 25 |
between consecutive points in \code{x} is returned. } |
| 26 |
\item{diagonal}{logical; if \code{TRUE}, \code{y} must be given and have the same number of |
| 27 |
points as \code{x}; the vector with distances between points with identical index is returned. } |
| 28 |
} |
| 29 |
|
| 30 |
\value{ |
| 31 |
\code{spDistsN1} returns a numeric vector of distances in the metric of the points if longlat=FALSE, or in kilometers if longlat=TRUE. |
| 32 |
|
| 33 |
\code{spDists} returns a full matrix of distances in the metric of the points if longlat=FALSE, or in kilometers if longlat=TRUE; it uses \code{spDistsN1} in case points are two-dimensional. In case of \code{spDists(x,x)}, it will compute |
| 34 |
all n x n distances, not the sufficient n x (n-1). |
| 35 |
} |
| 36 |
|
| 37 |
\note{The function can also be used to find a local kilometer equivalent to a plot scaled in decimal degrees in order to draw a scale bar. } |
| 38 |
\references{\code{http://www.abecedarical.com/javascript/script_greatcircle.html}} |
| 39 |
\author{Roger Bivand, Edzer Pebesma} |
| 40 |
|
| 41 |
|
| 42 |
\seealso{\code{\link{is.projected}}} |
| 43 |
\examples{ |
| 44 |
ll <- matrix(c(5, 6, 60, 60), ncol=2) |
| 45 |
km <- spDistsN1(ll, ll[1,], longlat=TRUE) |
| 46 |
zapsmall(km) |
| 47 |
utm32 <- matrix(c(276.9799, 332.7052, 6658.1572, 6655.2055), ncol=2) |
| 48 |
spDistsN1(utm32, utm32[1,]) |
| 49 |
dg <- spDistsN1(ll, ll[1,]) |
| 50 |
dg |
| 51 |
dg[2]/km[2] |
| 52 |
data(meuse) |
| 53 |
coordinates(meuse) <- c("x", "y") |
| 54 |
res <- spDistsN1(meuse, meuse[1,]) |
| 55 |
summary(res) |
| 56 |
|
| 57 |
p1 = SpatialPoints(cbind(1:3, 1:3)) |
| 58 |
spDists(p1) |
| 59 |
spDists(p1, p1) |
| 60 |
spDists(p1, p1, diagonal = TRUE) |
| 61 |
try(spDists(p1, p1, segments = TRUE)) |
| 62 |
spDists(p1, segments = TRUE) |
| 63 |
p2 = SpatialPoints(cbind(5:2, 2:5)) |
| 64 |
spDists(p1, p2) |
| 65 |
try(spDists(p1, p2, diagonal = TRUE)) # fails |
| 66 |
try(spDists(p1, p2, segments = TRUE)) # fails |
| 67 |
|
| 68 |
# longlat points: |
| 69 |
proj4string(p1) = "+proj=longlat +ellps=WGS84" |
| 70 |
proj4string(p2) = "+proj=longlat +ellps=WGS84" |
| 71 |
is.projected(p1) |
| 72 |
is.projected(p2) |
| 73 |
spDists(p1) |
| 74 |
spDists(p1, p1) |
| 75 |
spDists(p1, p1, diagonal = TRUE) |
| 76 |
spDists(p1, p2) |
| 77 |
try(spDists(p1, p2, diagonal = TRUE)) # fails |
| 78 |
spDists(p1, p2[1:length(p1),], diagonal = TRUE) |
| 79 |
spDists(p1, segments = TRUE) |
| 80 |
spDists(p1[0],p2[0],diagonal=TRUE) |
| 81 |
spDists(p1[0]) |
| 82 |
|
| 83 |
p1 = SpatialPoints(cbind(1:3, 1:3, 1:3)) |
| 84 |
spDists(p1) |
| 85 |
spDists(p1, p1) |
| 86 |
try(spDists(p1, p1, diagonal = TRUE)) |
| 87 |
try(spDists(p1, p1, segments = TRUE)) |
| 88 |
try(spDists(p1, segments = TRUE)) |
| 89 |
p2 = SpatialPoints(cbind(5:2, 2:5, 3:6)) |
| 90 |
spDists(p1, p2) |
| 91 |
try(spDists(p1, p2, diagonal = TRUE)) # fails |
| 92 |
try(spDists(p1, p2, segments = TRUE)) # fails |
| 93 |
|
| 94 |
} |
| 95 |
\keyword{spatial} |