flightpathr

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

commit d56a686cf97800d13c2b0c90e9dc30786af13db9
parent 5cda2e02278ac2ecfaa0d7765d15cc6fb707e0d1
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date:   Wed, 21 Sep 2016 13:53:15 -0400

flightpath stuff is coming online

Diffstat:
MNAMESPACE | 2++
AR/createPath.R | 45+++++++++++++++++++++++++++++++++++++++++++++
Aman/createPath.Rd | 31+++++++++++++++++++++++++++++++
Aman/is.flightpath.Rd | 12++++++++++++
Atests/testthat/test_createPath.R | 45+++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 135 insertions(+), 0 deletions(-)

diff --git a/NAMESPACE b/NAMESPACE @@ -1,11 +1,13 @@ # Generated by roxygen2: do not edit by hand export(coordsToBearing) +export(createPath) export(createTrajectory) export(distanceFromPath) export(identifyAltitudeChanges) export(identifyBearingChanges) export(interpolateTrajectory) +export(is.flightpath) export(is.flighttrajectory) export(maxDistanceFromPath) export(parseCoordinates) diff --git a/R/createPath.R b/R/createPath.R @@ -0,0 +1,45 @@ +#' Create a flightpath object from coordinate 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. +#' @return A flightpath object encapsulating these parameters (with default +#' values substituded as necessary). +#' +#' @details \code{longitude} and \code{latitude} must be the same length. +#' \code{altitude} must also have a length equal to these parameters or be +#' scalar. If \code{altitude} is \code{NA}, operations that compare values to +#' flightpaths will be affected. +#' +#' @export +createPath <- function(longitude, latitude, altitude = NA) { + if (!is.numeric(longitude)) stop("\"longitude\" must be a numeric vector") + nCoord <- length(longitude) + + # Check latitude type and length + if (!is.numeric(latitude)) stop("\"latitude\" must be a numeric vector") + if (length(latitude) != nCoord) stop("\"latitude\" and \"longitude\" length mismatch") + + # If altitude is NA or scalar, coerce its length. Otherwise, raise an error + if (isTRUE(all(is.na(altitude)))) { + altitude <- rep(NA, nCoord) + } else if (length(altitude) == 1) { + altitude <- rep(altitude, nCoord) + } else { + if (!is.numeric(altitude)) stop("\"altitude\" must be NA or a numeric vector") + if (length(altitude) != nCoord) stop("\"altitude\" has incorrect length") + } + + flightpath <- list(longitude = longitude, + latitude = latitude, + altitude = altitude) + class(flightpath) <- "flightpath" + + return(flightpath) +} + +#' Check if an object is a \code{flightpath} +#' @export +is.flightpath <- function(x) inherits(x, "flightpath") diff --git a/man/createPath.Rd b/man/createPath.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/createPath.R +\name{createPath} +\alias{createPath} +\title{Create a flightpath object from coordinate info.} +\usage{ +createPath(longitude, latitude, altitude = NA) +} +\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.} +} +\value{ +A flightpath object encapsulating these parameters (with default + values substituded as necessary). +} +\description{ +Create a flightpath object from coordinate info. +} +\details{ +\code{longitude} and \code{latitude} must be the same length. + \code{altitude} must also have a length equal to these parameters or be + scalar. If \code{altitude} is \code{NA}, operations that compare values to + flightpaths will be affected. +} + diff --git a/man/is.flightpath.Rd b/man/is.flightpath.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/createPath.R +\name{is.flightpath} +\alias{is.flightpath} +\title{Check if an object is a \code{flightpath}} +\usage{ +is.flightpath(x) +} +\description{ +Check if an object is a \code{flightpath} +} + diff --git a/tests/testthat/test_createPath.R b/tests/testthat/test_createPath.R @@ -0,0 +1,45 @@ +library(flightpathr) +context("createPath") + +library(geosphere) + +# Data for a flight path +pathMat <- matrix(c(-74.5771667, 39.4575833, + -74.9671439, 39.5376711, + -75.0330031, 39.7054758), + nrow = 3, byrow = TRUE, + dimnames = list(c("KACY", "VCN", "K17N"), + c("lon", "lat"))) + +test_that("Handling paths with no altitude", { + path <- createPath(pathMat[, "lon"], pathMat[, "lat"]) + expect_equal(path$longitude, pathMat[, "lon"]) + expect_equal(path$latitude, pathMat[, "lat"]) + expect_equal(path$altitude, rep(NA, nrow(pathMat))) +}) + +# test_that("Bearing and groundspeed calculations are correct", { +# expect_equal(trajectory$timestamp, seq(1, timeSec)) +# expect_equal(trajectory$groundspeed, rep(100, timeSec), tolerance = 0.5) +# expect_equal(trajectory$bearing[1:(timeSec-1)], +# bearing(trajectoryMat[1:(timeSec-1), ], trajectoryMat[2:timeSec, ])) +# }) +# +# test_that("Input types are checked", { +# expect_error(createTrajectory(paste(trajectoryMat[, "lon"]), trajectoryMat[, "lat"]), +# "\"longitude\" must be a numeric vector") +# expect_error(createTrajectory(trajectoryMat[, "lon"], paste(trajectoryMat[, "lat"])), +# "\"latitude\" must be a numeric vector") +# expect_error(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[, "lat"], paste(rep(2500, timeSec))), +# "\"altitude\" must be a numeric vector") +# }) +# +# test_that("Input lengths are checked", { +# expect_error(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[1, "lat"]), +# paste("Vector \"latitude\" has length = 1, expected length =", timeSec)) +# # Altitude can have length of 1 or timeSec, otherwise there should be an error +# expect_error(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[, "lat"], rep(2500, 2)), +# paste("Vector \"altitude\" has length = 2, expected length =", timeSec)) +# expect_equal(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[, "lat"], 2500)$altitude, +# rep(2500, timeSec)) +# })