Distance calculations
bng_distance.Rd
Compute Euclidean distances between BNG references and distance-based neighbours lists.
Usage
bng_dwithin(bng_ref, d, ...)
bng_distance(bng_ref1, bng_ref2, by_element = FALSE, edge_to_edge = FALSE)
Arguments
- bng_ref
object of class
BNGReference
.- d
numeric. Distance expressed in metres.
- ...
additional parameters. Not currently used.
- bng_ref1, bng_ref2
object of
BNGReference
- by_element
logical. If
TRUE
, return a vector with distance between each pair of BNG references. An error is raised if theBNGReference
objects are not the same length. Default isFALSE
, to return a dense matrix with all pairwise distances.- edge_to_edge
Logical. Should the distances be measured between the edges of the grid references? Default is
FALSE
to use the centroid.
Value
list containing an unordered vector of BNGReference
objects
around a given grid square within an absolute distance d
.
If by_element
is FALSE
bng_distance
returns a
dense numeric matrix of dimension length(x) by length(y); otherwise it
returns a numeric vector the same length as x
and y
with an
error raised if the lengths of x
and y
are unequal. Distances
involving invalid references are NA
.
Details
bng_dwithin
returns all grids squares for which any part of the
boundary is within the distance d
of any part of bng_ref
's
boundary.
Examples
bng_dwithin(as_bng_reference("SU1234"), 1000)
#> [[1]]
#> <BNGReference[9] with Resolution=1km>
#> [1] <SU 11 33> <SU 12 33> <SU 13 33> <SU 11 34> <SU 12 34> <SU 13 34> <SU 11 35>
#> [8] <SU 12 35> <SU 13 35>
#>
bng_dwithin(as_bng_reference("SU1234"), 1001)
#> [[1]]
#> <BNGReference[21] with Resolution=1km>
#> [1] <SU 11 32> <SU 12 32> <SU 13 32> <SU 10 33> <SU 11 33> <SU 12 33>
#> [7] <SU 13 33> <SU 14 33> <SU 10 34> <SU 11 34> <SU 12 34> <SU 13 34>
#> [13] <SU 14 34> <SU 10 35> <SU 11 35> <SU 12 35> <SU 13 35> <SU 14 35>
#> [19] <SU 11 36> <SU 12 36> <SU 13 36>
#>
ref1 <- as_bng_reference("SE1433")
bng_distance(ref1, as_bng_reference("SE1631"))
#> [,1]
#> [1,] 2828.427
bng_distance(ref1, as_bng_reference("SENW"))
#> [,1]
#> [1,] 42807.71
ref2 <- as_bng_reference(c("SE1533", "SE1631", "SE"))
#> Warning: Varying resolutions detected.
bng_distance(ref1, ref2)
#> [,1] [,2] [,3]
#> [1,] 1000 2828.427 39147.16
bng_distance(ref2)
#> [,1] [,2] [,3]
#> [1,] 0.000 2236.068 38242.65
#> [2,] 2236.068 0.000 38268.79
#> [3,] 38242.646 38268.786 0.00
ref3 <- as_bng_reference(c("SE1433", "SE1244"))
bng_distance(ref3, ref2)
#> [,1] [,2] [,3]
#> [1,] 1000.00 2828.427 39147.16
#> [2,] 11401.75 13601.471 37901.19
bng_distance(ref3, ref2[1:2], by_element = TRUE)
#> [1] 1000.00 13601.47
bng_distance(ref1, ref2, edge_to_edge = TRUE)
#> [,1] [,2] [,3]
#> [1,] 0 1414.214 0