Skip to contents

Internal function - not developed to be used outside of spatsoc functions

Usage

calc_distance(geometry_a, geometry_b, x_a, y_a, x_b, y_b, crs, use_dist)

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.

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