commit 00b1fbc06fac8a051685c2165822cde1f59866ee
parent bd1b8ef6ae287dd528d769dce4f3b91d6e8e365f
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date: Wed, 18 May 2016 13:56:36 -0400
Function to great flighttrajectory objects.
Diffstat:
5 files changed, 152 insertions(+), 1 deletion(-)
diff --git a/DESCRIPTION b/DESCRIPTION
@@ -5,8 +5,10 @@ Authors@R: person("Eamon", "Caddigan", email = "eamon.caddigan@tgobrien.com",
role = c("aut", "cre"))
Description: This package contains tools to analyze aircraft trajectories and
identify loss of well-clear (LoWC) and near mid-air collisions (NMAC).
-Depends:
+Depends:
R (>= 3.2.3)
License: What license is it under?
LazyData: true
RoxygenNote: 5.0.1
+Suggests: flightpathr,
+ geosphere
diff --git a/NAMESPACE b/NAMESPACE
@@ -1,2 +1,4 @@
# Generated by roxygen2: do not edit by hand
+export(createTrajectory)
+export(is.flighttrajectory)
diff --git a/R/createTrajectory.R b/R/createTrajectory.R
@@ -0,0 +1,96 @@
+#' Create a flighttrajectory object from the flight info.
+#'
+#' @param longitude Required; numeric vector giving aircraft longitude in
+#' degrees.
+#' @param latitude Required; numeric vector giving aircraft latitude in degrees.
+#' @param altitude Optional; numeric vector giving aircraft altitude (AGL) in
+#' feet. If missing, it will be set to 0.
+#' @param timestamp Optional; numeric vector giving the time of each observation
+#' in seconds. If missing, the observation period is assumed to be 1 s.
+#' @param bearing Optional; numeric vector giving the current bearing in
+#' degrees. If missing, it is estimated using pairs of successive lon/lat
+#' observations.
+#' @param groundspeed Optional; numeric vector giving the current ground speed
+#' of the aircraft in knots. If missing, it is estimated using pairs of
+#' successive lon/lat observations.
+#'
+#' @details \code{longitude} and \code{latitude} must be the same length.
+#' \code{timestamp}, \code{bearing}, and \code{groundspeed}, if present, must
+#' also match this length. \code{altitude} must also have a length equal to
+#' these parameters or be scalar.
+#'
+#' @export
+createTrajectory <- function(longitude, latitude, altitude = 0, timestamp = NULL,
+ bearing = NULL, groundspeed = NULL) {
+ if (!is.numeric(longitude)) stop("\"longitude\" must be a numeric vector")
+ nCoord <- length(longitude)
+
+ # Helper function to throw an error if the length of a vector is incorrect.
+ checkLength <- function(x) {
+ if (!is.numeric(x)) {
+ stop("\"", deparse(substitute(x)), "\" must be a numeric vector")
+ } else if (length(x) != nCoord) {
+ stop("Vector \"", deparse(substitute(x)), "\" has length = ", length(x),
+ ", expected length = ", nCoord)
+ }
+ return(TRUE)
+ }
+
+ checkLength(latitude)
+ coords <- cbind(longitude, latitude)
+
+ if (length(altitude) == 1) {
+ altitude <- rep(altitude, nCoord)
+ } else {
+ checkLength(altitude)
+ }
+
+ if (is.null(timestamp)) {
+ timestamp <- seq(1, nCoord)
+ } else {
+ checkLength(timestamp)
+ }
+
+ # Use flightpathr to calculate bearing between successive points if not
+ # specified.
+ if (is.null(bearing)) {
+ if (requireNamespace("flightpathr", quietly = TRUE)) {
+ bearing <- flightpathr::coordsToBearing(cbind(coords, altitude))
+ bearing[nCoord] <- bearing[nCoord-1]
+ } else {
+ stop("Package \"flightpathr\" must be installed or bearing must be specified")
+ }
+ } else {
+ checkLength(bearing)
+ }
+
+ # Use geosphere to find the distance between points and use the timestamps to
+ # calculate groundspeed if not specified.
+ if (is.null(groundspeed)) {
+ if (requireNamespace("geosphere", quietly = TRUE)) {
+ distNM <- geosphere::distCosine(coords[1:(nCoord-1), ],
+ coords[2:nCoord, ],
+ r = 3444)
+ groundspeed <- distNM / timestamp[1:(nCoord-1)] * 3600
+ groundspeed <- c(groundspeed, groundspeed[nCoord-1])
+ } else {
+ stop("Package \"geosphere\" must be installed or groundspeed must be specified")
+ }
+ } else{
+ checkLength(groundspeed)
+ }
+
+ flighttrajectory <- list(longitude = longitude,
+ latitude = latitude,
+ altitude = altitude,
+ timestamp = timestamp,
+ bearing = bearing,
+ groundspeed = groundspeed)
+ class(flighttrajectory) <- "flighttrajectory"
+
+ return(flighttrajectory)
+}
+
+#' Check if an object is a flighttrajectory
+#' @export
+is.flighttrajectory <- function(x) inherits(x, "flighttrajectory")
diff --git a/man/createTrajectory.Rd b/man/createTrajectory.Rd
@@ -0,0 +1,39 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/createTrajectory.R
+\name{createTrajectory}
+\alias{createTrajectory}
+\title{Create a flighttrajectory object from the flight info.}
+\usage{
+createTrajectory(longitude, latitude, altitude = 0, timestamp = NULL,
+ bearing = NULL, groundspeed = NULL)
+}
+\arguments{
+\item{longitude}{Required; numeric vector giving aircraft longitude in
+degrees.}
+
+\item{latitude}{Required; numeric vector giving aircraft latitude in degrees.}
+
+\item{altitude}{Optional; numeric vector giving aircraft altitude (AGL) in
+feet. If missing, it will be set to 0.}
+
+\item{timestamp}{Optional; numeric vector giving the time of each observation
+in seconds. If missing, the observation period is assumed to be 1 s.}
+
+\item{bearing}{Optional; numeric vector giving the current bearing in
+degrees. If missing, it is estimated using pairs of successive lon/lat
+observations.}
+
+\item{groundspeed}{Optional; numeric vector giving the current ground speed
+of the aircraft in knots. If missing, it is estimated using pairs of
+successive lon/lat observations.}
+}
+\description{
+Create a flighttrajectory object from the flight info.
+}
+\details{
+\code{longitude} and \code{latitude} must be the same length.
+ \code{timestamp}, \code{bearing}, and \code{groundspeed}, if present, must
+ also match this length. \code{altitude} must also have a length equal to
+ these parameters or be scalar.
+}
+
diff --git a/man/is.flighttrajectory.Rd b/man/is.flighttrajectory.Rd
@@ -0,0 +1,12 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/createTrajectory.R
+\name{is.flighttrajectory}
+\alias{is.flighttrajectory}
+\title{Check if an object is a flighttrajectory}
+\usage{
+is.flighttrajectory(x)
+}
+\description{
+Check if an object is a flighttrajectory
+}
+