% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/direction_to_centroid.R
\name{direction_to_centroid}
\alias{direction_to_centroid}
\title{Direction to group centroid}
\usage{
direction_to_centroid(
  DT = NULL,
  coords = NULL,
  crs = NULL,
  geometry = "geometry"
)
}
\arguments{
\item{DT}{input data.table}

\item{coords}{character vector of X coordinate and Y coordinate column names.
Note: the order is assumed X followed by Y column names}

\item{crs}{numeric or character defining the coordinate reference
system to be passed to \link[sf:st_crs]{sf::st_crs}. For example, either
\code{crs = "EPSG:32736"} or \code{crs = 32736}. Used only if coords are provided,
see details under Interface}

\item{geometry}{simple feature geometry list column name, generated by
\code{\link[=get_geometry]{get_geometry()}}. Default 'geometry', see details under Interface}
}
\value{
\code{direction_to_centroid} returns the input \code{DT} appended with a
\code{direction_centroid} column indicating the direction to the group centroid
in radians. A value of NaN is returned when the coordinates of the focal
individual equal the coordinates of the centroid.

A message is returned when \code{direction_centroid} column already exist in the
input \code{DT}, because they will be overwritten.

Missing values in coordinates / geometry are ignored and NA is returned.

See details for appending outputs using modify-by-reference in the
\href{https://docs.ropensci.org/spatsoc/articles/faq.html}{FAQ}.
}
\description{
\code{direction_to_centroid} calculates the direction of each relocation to the
centroid of the spatiotemporal group identified by \code{\link[=group_pts]{group_pts()}}. The
function expects a \code{data.table} with relocation data appended with a \code{group}
column from \code{\link[=group_pts]{group_pts()}} and centroid columns from \code{\link[=centroid_group]{centroid_group()}}.
Relocation data should be in two columns representing the X and Y
coordinates, or in a geometry column prepared by the helper function
\code{\link[=get_geometry]{get_geometry()}}.
}
\details{
The \code{DT} must be a \code{data.table}. If your data is a \code{data.frame}, you can
convert it by reference using \code{\link[data.table:setDT]{data.table::setDT()}} or by reassigning using
\code{\link[data.table:data.table]{data.table::data.table()}}.

This function expects a \code{group} column present generated with the
\code{\link[=group_pts]{group_pts()}} function and centroid coordinates generated with the
\code{\link[=centroid_group]{centroid_group()}} function. The \code{group} argument expects the name of the
column in \code{DT} which correspond to the group column.

See below under "Interface" for details on providing coordinates and under
"Direction function" for details on the underlying direction function used.
}
\section{Interface}{

Two interfaces are available for providing coordinates:
\enumerate{
\item Provide \code{coords} and \code{crs}. The \code{coords} argument expects the names of
the X and Y coordinate columns. The \code{crs} argument expects a character
string or numeric defining the coordinate reference system to be passed to
\link[sf:st_crs]{sf::st_crs}. For example, for UTM zone 36S (EPSG 32736), the crs argument
is \code{crs = "EPSG:32736"} or \code{crs = 32736}. See \url{https://spatialreference.org}
for a list of EPSG codes.
\item (New!) Provide \code{geometry}. The \code{geometry} argument allows the user to
supply a \code{geometry} column that represents the coordinates as a simple
feature geometry list column. This interface expects the user to prepare
their input DT with \code{\link[=get_geometry]{get_geometry()}}. To use this interface, leave the
\code{coords} and \code{crs} arguments \code{NULL}, and the default argument for \code{geometry}
('geometry') will be used directly.
}
}

\section{Direction function}{


The underlying distance function used depends on the crs of the coordinates
or geometry provided.
\itemize{
\item If the crs is provided and longlat degrees (as determined by
\code{\link[sf:st_is_longlat]{sf::st_is_longlat()}}), the distance function is
\code{\link[lwgeom:st_geod_azimuth]{lwgeom::st_geod_azimuth()}}.
\item If the crs is provided and not longlat degrees (eg. a projected UTM),
the coordinates or geometry are transformed to \code{sf::st_crs(4326)} before the
distance is measured using \code{\link[lwgeom:st_geod_azimuth]{lwgeom::st_geod_azimuth()}}.
\item If the crs is NULL or NA_crs_, the distance function cannot be used
and an error is returned.
}
}

\examples{
# Load data.table
library(data.table)
\dontshow{data.table::setDTthreads(1)}
# Read example data
DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))

# Cast the character column to POSIXct
DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]
\dontshow{
# (Subset example data to reduce runtime on CRAN)
if (isFALSE(rlang::is_interactive())) {
  DT <- DT[as.Date(datetime) == '2017-01-17']
}
}
# Temporal grouping
group_times(DT, datetime = 'datetime', threshold = '20 minutes')

# Spatial grouping with timegroup
group_pts(DT, threshold = 5, id = 'ID',
          coords = c('X', 'Y'), timegroup = 'timegroup')

# Calculate group centroid
centroid_group(DT, coords = c('X', 'Y'), group = 'group')

# Calculate direction to group centroid
direction_to_centroid(DT, coords = c('X', 'Y'), crs = 32736)

# Or, using the new geometry interface
get_geometry(DT, coords = c('X', 'Y'), crs = 32736)
group_pts(DT, threshold = 5, id = 'ID', timegroup = 'timegroup')
centroid_group(DT)
direction_to_centroid(DT)
}
\references{
See example of using direction to group centroid:
\itemize{
\item \doi{doi:10.1016/j.cub.2017.08.004}
}
}
\seealso{
\link{centroid_group}, \link{group_pts}, \code{\link[lwgeom:st_geod_azimuth]{lwgeom::st_geod_azimuth()}}

Other Direction functions: 
\code{\link{direction_group}()},
\code{\link{direction_polarization}()},
\code{\link{direction_step}()},
\code{\link{direction_to_leader}()},
\code{\link{edge_alignment}()},
\code{\link{edge_delay}()},
\code{\link{edge_direction}()},
\code{\link{edge_zones}()},
\code{\link{leader_direction_group}()},
\code{\link{leader_edge_delay}()}

Other Centroid functions: 
\code{\link{centroid_dyad}()},
\code{\link{centroid_fusion}()},
\code{\link{centroid_group}()},
\code{\link{distance_to_centroid}()}
}
\concept{Centroid functions}
\concept{Direction functions}
