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.

spDistsN1(pts, pt, longlat = FALSE)
spDists(x, y = x, longlat = FALSE, segments = FALSE, diagonal = FALSE)

Arguments

pts

A matrix of 2D points, first column x/longitude, second column y/latitude, or a SpatialPoints or SpatialPointsDataFrame object

pt

A single 2D point, first value x/longitude, second value y/latitude, or a SpatialPoints or SpatialPointsDataFrame object with one point only

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 coordinates method

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 coordinates method

longlat

logical; if FALSE, Euclidean distance, if TRUE Great Circle (WGS84 ellipsoid) distance; if x is a Spatial object, longlat should not be specified but will be derived from is.projected(x)

segments

logical; if TRUE, y must be missing; the vector of distances between consecutive points in x is returned.

diagonal

logical; if TRUE, y must be given and have the same number of points as x; the vector with distances between points with identical index is returned.

Value

spDistsN1 returns a numeric vector of distances in the metric of the points if longlat=FALSE, or in kilometers if longlat=TRUE.

spDists returns a full matrix of distances in the metric of the points if longlat=FALSE, or in kilometers if longlat=TRUE; it uses spDistsN1 in case points are two-dimensional. In case of spDists(x,x), it will compute all n x n distances, not the sufficient n x (n-1).

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.

References

http://www.abecedarical.com/javascript/script_greatcircle.html

See also

Examples

ll <- matrix(c(5, 6, 60, 60), ncol=2) km <- spDistsN1(ll, ll[1,], longlat=TRUE) zapsmall(km)
#> [1] 0.00000 55.79918
utm32 <- matrix(c(276.9799, 332.7052, 6658.1572, 6655.2055), ncol=2) spDistsN1(utm32, utm32[1,])
#> [1] 0.00000 55.80342
dg <- spDistsN1(ll, ll[1,]) dg
#> [1] 0 1
dg[2]/km[2]
#> [1] 0.01792141
data(meuse) coordinates(meuse) <- c("x", "y") res <- spDistsN1(meuse, meuse[1,]) summary(res)
#> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0 1210 2279 2282 3403 4425
p1 = SpatialPoints(cbind(1:3, 1:3)) spDists(p1)
#> [,1] [,2] [,3] #> [1,] 0.000000 1.414214 2.828427 #> [2,] 1.414214 0.000000 1.414214 #> [3,] 2.828427 1.414214 0.000000
spDists(p1, p1)
#> [,1] [,2] [,3] #> [1,] 0.000000 1.414214 2.828427 #> [2,] 1.414214 0.000000 1.414214 #> [3,] 2.828427 1.414214 0.000000
spDists(p1, p1, diagonal = TRUE)
#> [1] 0 0 0
try(spDists(p1, p1, segments = TRUE))
#> Error in spDists(p1, p1, segments = TRUE) : missing(y) is not TRUE
spDists(p1, segments = TRUE)
#> [1] 1.414214 1.414214
p2 = SpatialPoints(cbind(5:2, 2:5)) spDists(p1, p2)
#> [,1] [,2] [,3] [,4] #> [1,] 4.123106 3.605551 3.605551 4.123106 #> [2,] 3.000000 2.236068 2.236068 3.000000 #> [3,] 2.236068 1.000000 1.000000 2.236068
try(spDists(p1, p2, diagonal = TRUE)) # fails
#> Error in spDists(p1, p2, diagonal = TRUE) : #> nrow(x) == nrow(y) is not TRUE
try(spDists(p1, p2, segments = TRUE)) # fails
#> Error in spDists(p1, p2, segments = TRUE) : missing(y) is not TRUE
# longlat points: proj4string(p1) = "+proj=longlat +ellps=WGS84"
#> Warning: Discarded datum Unknown based on WGS84 ellipsoid in CRS definition
proj4string(p2) = "+proj=longlat +ellps=WGS84"
#> Warning: Discarded datum Unknown based on WGS84 ellipsoid in CRS definition
#> [1] FALSE
#> [1] FALSE
spDists(p1)
#> [,1] [,2] [,3] #> [1,] 0.0000 156.8744 313.7019 #> [2,] 156.8744 0.0000 156.8276 #> [3,] 313.7019 156.8276 0.0000
spDists(p1, p1)
#> [,1] [,2] [,3] #> [1,] 0.0000 156.8744 313.7019 #> [2,] 156.8744 0.0000 156.8276 #> [3,] 313.7019 156.8276 0.0000
spDists(p1, p1, diagonal = TRUE)
#> [1] 0 0 0
spDists(p1, p2)
#> [,1] [,2] [,3] [,4] #> [1,] 458.6486 400.3590 399.3794 456.0565 #> [2,] 333.7564 248.3940 247.5177 331.7324 #> [3,] 248.3940 111.1679 110.5772 247.4690
try(spDists(p1, p2, diagonal = TRUE)) # fails
#> Error in spDists(p1, p2, diagonal = TRUE) : #> nrow(x) == nrow(y) is not TRUE
spDists(p1, p2[1:length(p1),], diagonal = TRUE)
#> [1] 458.6486 248.3940 110.5772
spDists(p1, segments = TRUE)
#> [1] 156.8744 156.8276
spDists(p1[0],p2[0],diagonal=TRUE)
#> numeric(0)
spDists(p1[0])
#> <0 x 0 matrix>
p1 = SpatialPoints(cbind(1:3, 1:3, 1:3)) spDists(p1)
#> 1 2 3 #> 1 0.000000 1.732051 3.464102 #> 2 1.732051 0.000000 1.732051 #> 3 3.464102 1.732051 0.000000
spDists(p1, p1)
#> [,1] [,2] [,3] #> [1,] 0.000000 1.732051 3.464102 #> [2,] 1.732051 0.000000 1.732051 #> [3,] 3.464102 1.732051 0.000000
try(spDists(p1, p1, diagonal = TRUE))
#> Error in spDists(p1, p1, diagonal = TRUE) : ncol(x) == 2 is not TRUE
try(spDists(p1, p1, segments = TRUE))
#> Error in spDists(p1, p1, segments = TRUE) : missing(y) is not TRUE
try(spDists(p1, segments = TRUE))
#> Error in spDists(p1, segments = TRUE) : ncol(x) == 2 is not TRUE
p2 = SpatialPoints(cbind(5:2, 2:5, 3:6)) spDists(p1, p2)
#> [,1] [,2] [,3] [,4] #> [1,] 4.582576 4.690416 5.385165 6.480741 #> [2,] 3.162278 3.000000 3.741657 5.000000 #> [3,] 2.236068 1.414214 2.236068 3.741657
try(spDists(p1, p2, diagonal = TRUE)) # fails
#> Error in spDists(p1, p2, diagonal = TRUE) : ncol(x) == 2 is not TRUE
try(spDists(p1, p2, segments = TRUE)) # fails
#> Error in spDists(p1, p2, segments = TRUE) : missing(y) is not TRUE