flightpathr

Tools to analyze aircraft and flight path data.
git clone https://git.eamoncaddigan.net/flightpathr.git
Log | Files | Refs | README | LICENSE

createPath.R (1781B)


      1 #' Create a flightpath object from coordinate info.
      2 #'
      3 #' @param longitude Required; numeric vector giving aircraft longitude in
      4 #'   degrees.
      5 #' @param latitude Required; numeric vector giving aircraft latitude in degrees.
      6 #' @param altitude Optional; numeric vector giving aircraft altitude (AGL) in
      7 #'   feet.
      8 #' @return A flightpath object encapsulating these parameters (with default
      9 #'   values substituded as necessary).
     10 #'
     11 #' @details \code{longitude} and \code{latitude} must be the same length.
     12 #'   \code{altitude} must also have a length equal to these parameters or be
     13 #'   scalar. If \code{altitude} is \code{NA}, operations that compare values to
     14 #'   flightpaths will be affected.
     15 #'
     16 #' @export
     17 createPath <- function(longitude, latitude, altitude = NA) {
     18   if (!is.numeric(longitude)) stop("\"longitude\" must be a numeric vector")
     19   nCoord <- length(longitude)
     20 
     21   # Check latitude type and length
     22   if (!is.numeric(latitude)) stop("\"latitude\" must be a numeric vector")
     23   if (length(latitude) != nCoord) stop("\"latitude\" and \"longitude\" length mismatch")
     24 
     25   # If altitude is NA or scalar, coerce its length. Otherwise, raise an error
     26   if (isTRUE(all(is.na(altitude)))) {
     27     altitude <- rep(NA, nCoord)
     28   } else if (length(altitude) == 1) {
     29     altitude <- rep(altitude, nCoord)
     30   } else {
     31     if (!is.numeric(altitude)) stop("\"altitude\" must be NA or a numeric vector")
     32     if (length(altitude) != nCoord) stop("\"altitude\" has incorrect length")
     33   }
     34 
     35   flightpath <- list(longitude = longitude,
     36                      latitude = latitude,
     37                      altitude = altitude)
     38   class(flightpath) <- "flightpath"
     39 
     40   return(flightpath)
     41 }
     42 
     43 #' Check if an object is a \code{flightpath}
     44 #' @export
     45 is.flightpath <- function(x) inherits(x, "flightpath")