Internal function - not developed to be used outside of spatsoc functions
Arguments
- geometry_a, geometry_b
sfc (simple feature geometry list column) from
get_geometry()- x_a, x_b
X coordinate column, numeric
- y_a, y_b
Y coordinate column, numeric
- crs
crs for x_a, y_a (and if provided, x_b, y_b) coordinates, ignored for geometry_a and geometry_b arguments
- use_dist
boolean predetermine if distance calculated via dist
Value
The underlying distance function used depends on the crs of the coordinates or geometry provided.
If the crs is longlat degrees (as determined by
sf::st_is_longlat()), the distance function issf::st_distance()which passes tos2::s2_distance()ifsf::sf_use_s2()is TRUE andlwgeom::st_geod_distance()ifsf::sf_use_s2()is FALSE. The distance returned has units set according to the crs.If the crs is not longlat degrees (eg. NULL, NA_crs_, or projected), the distance function used is
stats::dist()(or Euclidean distance for pairwise distances), maintaining expected behaviour from previous versions. The distance returned does not have units set.
Note: in both cases, if the coordinates are NA then the result will be NA.
Details
Calculate distance for one of the following combinations:
the distance matrix of points in geometry_a
the distance matrix of points in x_a, y_a
the pairwise distance between points in geometry_a and geometry_b
the pairwise distance between points in x_a, y_a and x_b, y_b
Requirements:
matching length between a and b objects if b provided
Examples
# Load data.table
library(data.table)
# Example points
example <- data.table(
X = c(0, 5, 5, 0, 0, NA_real_, 0, NA_real_),
Y = c(0, 0, 5, 5, 0, 0, NA_real_, NA_real_)
)
# E, N, W, S
example[, spatsoc:::calc_distance(x_a = X, y_a = Y, crs = 4326, use_dist = FALSE)]
#> Units: [m]
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#> [1,] 0.0 555975.5 785768.5 555975.5 0.0 NA NA NA
#> [2,] 555975.5 0.0 555975.5 785768.5 555975.5 NA NA NA
#> [3,] 785768.5 555975.5 0.0 553858.5 785768.5 NA NA NA
#> [4,] 555975.5 785768.5 553858.5 0.0 555975.5 NA NA NA
#> [5,] 0.0 555975.5 785768.5 555975.5 0.0 NA NA NA
#> [6,] NA NA NA NA NA NA NA NA
#> [7,] NA NA NA NA NA NA NA NA
#> [8,] NA NA NA NA NA NA NA NA
