flightpathr

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

interpolateTrajectory.R (1339B)


      1 #' Interpolate a trajectory (in time)
      2 #' 
      3 #' @param trajectory A \code{flighttrajectory} object.
      4 #' @param timestamp The new timestamp along which the data should be 
      5 #'   interpolated.
      6 #' @return A new \code{flighttrajectory} with the given \code{timestamp}
      7 #'   
      8 #' @details This just performs linear interpolation for all of the values in the
      9 #'   trajectory. A better approach would make use of the bearing and velocity
     10 #'   information to smoothly interpolate the coordinates.
     11 #'
     12 #' @export
     13 interpolateTrajectory <- function(trajectory, timestamp) {
     14   # We'll interpolate all of the features (except the timestamp, which is
     15   # specified).
     16   trajectoryFeatures <- names(trajectory)
     17   trajectoryFeatures <- trajectoryFeatures[trajectoryFeatures != "timestamp"]
     18   
     19   # Create a new trajectory with 
     20   newTrajectory <- list()
     21   for (trajectoryFeature in trajectoryFeatures) {
     22     newTrajectory[[trajectoryFeature]] <- approx(x = trajectory$timestamp,
     23                                                  y = trajectory[[trajectoryFeature]],
     24                                                  xout = timestamp,
     25                                                  method = "linear",
     26                                                  rule = 2)$y
     27   }
     28   newTrajectory[["timestamp"]] <- timestamp
     29   
     30   return(do.call(createTrajectory, newTrajectory))
     31 }