spDistsN1.Rd
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)
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 |
segments | logical; if |
diagonal | logical; if |
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).
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.
http://www.abecedarical.com/javascript/script_greatcircle.html
#> [1] 0.00000 55.79918#> [1] 0.00000 55.80342dg <- spDistsN1(ll, ll[1,]) dg#> [1] 0 1dg[2]/km[2]#> [1] 0.01792141#> Min. 1st Qu. Median Mean 3rd Qu. Max. #> 0 1210 2279 2282 3403 4425#> [,1] [,2] [,3] #> [1,] 0.000000 1.414214 2.828427 #> [2,] 1.414214 0.000000 1.414214 #> [3,] 2.828427 1.414214 0.000000spDists(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.000000spDists(p1, p1, diagonal = TRUE)#> [1] 0 0 0#> Error in spDists(p1, p1, segments = TRUE) : missing(y) is not TRUEspDists(p1, segments = TRUE)#> [1] 1.414214 1.414214#> [,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#> Error in spDists(p1, p2, diagonal = TRUE) : #> nrow(x) == nrow(y) is not TRUE#> Error in spDists(p1, p2, segments = TRUE) : missing(y) is not TRUE#> Warning: Discarded datum Unknown based on WGS84 ellipsoid in CRS definition#> Warning: Discarded datum Unknown based on WGS84 ellipsoid in CRS definitionis.projected(p1)#> [1] FALSEis.projected(p2)#> [1] FALSEspDists(p1)#> [,1] [,2] [,3] #> [1,] 0.0000 156.8744 313.7019 #> [2,] 156.8744 0.0000 156.8276 #> [3,] 313.7019 156.8276 0.0000spDists(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.0000spDists(p1, p1, diagonal = TRUE)#> [1] 0 0 0spDists(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#> Error in spDists(p1, p2, diagonal = TRUE) : #> nrow(x) == nrow(y) is not TRUE#> [1] 458.6486 248.3940 110.5772spDists(p1, segments = TRUE)#> [1] 156.8744 156.8276spDists(p1[0],p2[0],diagonal=TRUE)#> numeric(0)spDists(p1[0])#> <0 x 0 matrix>#> 1 2 3 #> 1 0.000000 1.732051 3.464102 #> 2 1.732051 0.000000 1.732051 #> 3 3.464102 1.732051 0.000000spDists(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#> Error in spDists(p1, p1, diagonal = TRUE) : ncol(x) == 2 is not TRUE#> Error in spDists(p1, p1, segments = TRUE) : missing(y) is not TRUE#> Error in spDists(p1, segments = TRUE) : ncol(x) == 2 is not TRUE#> [,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#> Error in spDists(p1, p2, diagonal = TRUE) : ncol(x) == 2 is not TRUE#> Error in spDists(p1, p2, segments = TRUE) : missing(y) is not TRUE