% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/internal.R
\name{calc_distance}
\alias{calc_distance}
\title{Calculate distance}
\usage{
calc_distance(geometry_a, geometry_b, x_a, y_a, x_b, y_b, crs, use_dist)
}
\arguments{
\item{geometry_a, geometry_b}{sfc (simple feature geometry list column)
from \code{\link[=get_geometry]{get_geometry()}}}

\item{x_a, x_b}{X coordinate column, numeric}

\item{y_a, y_b}{Y coordinate column, numeric}

\item{crs}{crs for x_a, y_a (and if provided, x_b, y_b) coordinates,
ignored for geometry_a and geometry_b arguments}

\item{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.
\itemize{
\item If the crs is longlat degrees (as determined by
\code{\link[sf:st_is_longlat]{sf::st_is_longlat()}}), the distance function is \code{\link[sf:geos_measures]{sf::st_distance()}} which
passes to \code{\link[s2:s2_is_collection]{s2::s2_distance()}} if \code{\link[sf:s2]{sf::sf_use_s2()}} is TRUE and
\code{\link[lwgeom:geod]{lwgeom::st_geod_distance()}} if \code{\link[sf:s2]{sf::sf_use_s2()}} is FALSE. The distance
returned has units set according to the crs.
\item If the crs is not longlat degrees (eg. NULL, NA_crs_, or projected), the
distance function used is \code{\link[stats:dist]{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.
}
\description{
\strong{Internal function} - not developed to be used outside of spatsoc functions
}
\details{
Calculate distance for one of the following combinations:
\itemize{
\item the distance matrix of points in geometry_a
\item the distance matrix of points in x_a, y_a
\item the pairwise distance between points in geometry_a and geometry_b
\item the pairwise distance between points in x_a, y_a and x_b, y_b
}

Requirements:
\itemize{
\item matching length between a and b objects if b provided
}
}
\examples{
# Load data.table
library(data.table)
\dontshow{data.table::setDTthreads(1)}

# 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)]
}
\keyword{internal}
