R/routeTools.R

Defines functions intersection

Documented in intersection

#' intersection
#'
#' Find coordinates of intersection of two radials
#'
#' @param p1 First point, as c(lat,lon) pair or fix ID
#' @param b1 Bearing from first point in degrees
#' @param p2 First point, as c(lat,lon) pair or fix ID
#' @param b2 Bearing from second point in degrees
#'
#' @return lat, lon of point of intersection
#' @export
#' @importFrom geosphere gcIntersectBearing
intersection <- function(p1, b1, p2, b2) {
  data(fixes)
  if(is.character(p1) && length(p1) == 1) {
    lat1 <- as.numeric(fixes[p1, ]$lat)
    lon1 <- as.numeric(fixes[p1, ]$lon)
  } else if(is.numeric(p1) && length(p1) == 2) {
    lat1 <- as.numeric(p1[1])
    lon1 <- as.numeric(p1[2])
  } else {
    stop("p1 must be either c(lat, lon) of type numeric, or a fix ID")
  }
  if(is.character(p2) && length(p2) == 1) {
    lat2 <- as.numeric(fixes[p2, ]$lat)
    lon2 <- as.numeric(fixes[p2, ]$lon)
  } else if(is.numeric(p2) && length(p2) == 2) {
    lat2 <- as.numeric(p2[1])
    lon2 <- as.numeric(p2[2])
  } else {
    stop("p1 must be either c(lat, lon) of type numeric, or a fix ID")
  }

  gcIntersectBearing(c(lon1, lat1), b1,  c(lon2, lat2) , b2)[c(2,1)]
}
erikor/airtraffic documentation built on Nov. 4, 2019, 11:56 a.m.