R/add_track_length.R

Defines functions add_track_length_Odometer add_track_length_GPS

Documented in add_track_length_GPS add_track_length_Odometer

#' Add track length based on GPS
#'
#' The function adds a column with track length in meters based on track start and stop GPS coordinates.
#' @references
#' \itemize{
#' \item{geosphere R-package}
#' }
#' @param stations (required) Dataframe (tibble) with all stations and the attributes
#' (columns) "Lon_start_DD", "Lat_start_DD","Lon_stop_DD", and "Lat_stop_DD".
#' @return This function returns the input tibble with an added column called
#' "Track_dist_m_GPS".
#' @export
add_track_length_GPS <- function(stations){
  stations <- stations %>%
    dplyr::mutate(Track_dist_m_GPS = geosphere::distGeo(
      as.matrix(.[c("Lon_start_DD", "Lat_start_DD")]),
      as.matrix(.[c("Lon_stop_DD", "Lat_stop_DD")])
    ))
  return(stations)
}

#' Add track length based on the odometer
#'
#' The function adds a column with track length in meters based on odometer ticks (one tick is two meters).
#' @references
#' \itemize{
#' \item{geosphere R-package}
#' }
#' @param stations (required) Dataframe (tibble) with all stations and the attributes
#' (columns) "Lon_start_DD", "Lat_start_DD","Lon_stop_DD", and "Lat_stop_DD".
#' @return This function returns the input tibble with an added column called
#' "Track_dist_m_Odometer".
#' @export
add_track_length_Odometer <- function(stations, tick_length_m = 2){
  stations <- stations %>%
    dplyr::mutate(Track_dist_m_Odometer = Odometer_count * tick_length_m)
  return(stations)
}
dswdejonge/TripleD documentation built on June 18, 2020, 12:24 p.m.