commit 9b418fd90481cdeb595aa0c5f811238aee7eaa9e
parent 37b6187947043f8aeb710cff00b9b1cdaef05fe7
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date: Thu, 22 Sep 2016 11:14:24 -0400
as.flightpath() and as.flighttrajectory() implemented.
Diffstat:
8 files changed, 118 insertions(+), 15 deletions(-)
diff --git a/NAMESPACE b/NAMESPACE
@@ -1,5 +1,13 @@
# Generated by roxygen2: do not edit by hand
+S3method(as.flightpath,data.frame)
+S3method(as.flightpath,flightpath)
+S3method(as.flightpath,matrix)
+S3method(as.flighttrajectory,data.frame)
+S3method(as.flighttrajectory,flighttrajectory)
+S3method(as.flighttrajectory,matrix)
+export(as.flightpath)
+export(as.flighttrajectory)
export(coordsToBearing)
export(createPath)
export(createTrajectory)
diff --git a/R/coercePath.R b/R/coercePath.R
@@ -0,0 +1,29 @@
+# Functions to coerce objects into instances of flightpath. Right now
+# these are pretty dumb; in the future it would be smart to look for columns
+# that look like, e.g., longitude, latitude, and altitude.
+
+#' Attempt to coerce an object into an instance of \code{flightpath}.
+#' @export
+as.flightpath <- function(x) UseMethod("as.flightpath", x)
+
+#' @method as.flightpath flightpath
+#' @export
+as.flightpath.flightpath <- function(x) {
+ return(x)
+}
+
+#' @method as.flightpath data.frame
+#' @export
+as.flightpath.data.frame <- function(x) {
+ return(do.call(createTrajectory, as.list(unname(x))))
+}
+
+#' @method as.flightpath matrix
+#' @export
+as.flightpath.matrix <- function(x) {
+ return(as.flightpath.data.frame(as.data.frame(x)))
+}
+
+as.flightpath.SpatialPoints <- function(x) {
+ return(as.flightpath.matrix(as.matrix(sp::coordinates(x))))
+}
diff --git a/R/coerceTrajectory.R b/R/coerceTrajectory.R
@@ -0,0 +1,30 @@
+# Functions to coerce objects into instances of flighttrajectory. Right now
+# these are pretty dumb; in the future it would be smart to look for columns
+# that look like, e.g., longitude, latitude, and altitude.
+
+#' Attempt to coerce an object into an instance of \code{flighttrajectory}.
+#' @export
+as.flighttrajectory <- function(x) UseMethod("as.flighttrajectory", x)
+
+#' @method as.flighttrajectory flighttrajectory
+#' @export
+as.flighttrajectory.flighttrajectory <- function(x) {
+ return(x)
+}
+
+#' @method as.flighttrajectory data.frame
+#' @export
+as.flighttrajectory.data.frame <- function(x) {
+ # TODO: Attempt to match columns to arguments
+ return(do.call(createTrajectory, as.list(unname(x))))
+}
+
+#' @method as.flighttrajectory matrix
+#' @export
+as.flighttrajectory.matrix <- function(x) {
+ return(as.flighttrajectory.data.frame(as.data.frame(x)))
+}
+
+as.flighttrajectory.SpatialPoints <- function(x) {
+ return(as.flighttrajectory.matrix(as.matrix(sp::coordinates(x))))
+}
diff --git a/R/distanceFromPath.R b/R/distanceFromPath.R
@@ -1,9 +1,9 @@
#' Calculate the distance of a flight trajectory from a flight path.
#'
-#' @param trajectory A \code{flighttrajectory} object indicating the trajectory
-#' of an aircraft.
-#' @param path A \code{flightpath} object indicating the ordered waypoints a
-#' pre-defined flight path.
+#' @param trajectory A \code{flighttrajectory} object (or an object that can be
+#' coerced into one) indicating the trajectory of an aircraft.
+#' @param path A \code{flightpath} object (or an object that can be coerced into
+#' one) indicating the ordered waypoints a pre-defined flight path.
#' @return A data.frame containing two columns representing the distance between
#' the aircraft and its planned flight path (in feet): \code{horizontal}
#' indicates the horizontal distance and \code{vertical} indicates the
@@ -12,8 +12,8 @@
#' @export
distanceFromPath <- function(trajectory, path) {
# Check inputs and get 3D coordinates
- trajectoryCoords <- get3dCoords(trajectory)
- pathCoords <- get3dCoords(path)
+ trajectoryCoords <- get3dCoords(as.flighttrajectory(trajectory))
+ pathCoords <- get3dCoords(as.flightpath(path))
numLegs <- nrow(pathCoords)-1
diff --git a/man/as.flightpath.Rd b/man/as.flightpath.Rd
@@ -0,0 +1,12 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/coercePath.R
+\name{as.flightpath}
+\alias{as.flightpath}
+\title{Attempt to coerce an object into an instance of \code{flightpath}.}
+\usage{
+as.flightpath(x)
+}
+\description{
+Attempt to coerce an object into an instance of \code{flightpath}.
+}
+
diff --git a/man/as.flighttrajectory.Rd b/man/as.flighttrajectory.Rd
@@ -0,0 +1,12 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/coerceTrajectory.R
+\name{as.flighttrajectory}
+\alias{as.flighttrajectory}
+\title{Attempt to coerce an object into an instance of \code{flighttrajectory}.}
+\usage{
+as.flighttrajectory(x)
+}
+\description{
+Attempt to coerce an object into an instance of \code{flighttrajectory}.
+}
+
diff --git a/man/distanceFromPath.Rd b/man/distanceFromPath.Rd
@@ -7,11 +7,11 @@
distanceFromPath(trajectory, path)
}
\arguments{
-\item{trajectory}{A \code{flighttrajectory} object indicating the trajectory
-of an aircraft.}
+\item{trajectory}{A \code{flighttrajectory} object (or an object that can be
+coerced into one) indicating the trajectory of an aircraft.}
-\item{path}{A \code{flightpath} object indicating the ordered waypoints a
-pre-defined flight path.}
+\item{path}{A \code{flightpath} object (or an object that can be coerced into
+one) indicating the ordered waypoints a pre-defined flight path.}
}
\value{
A data.frame containing two columns representing the distance between
diff --git a/tests/testthat/test_distanceFromPath.R b/tests/testthat/test_distanceFromPath.R
@@ -32,12 +32,24 @@ test_that("non-deviating paths have small distances for all input types", {
expect_equal(distanceFromPath(trajectory, path)$horizontal,
rep(0, trajectoryLength),
tolerance = 1)
- # Leaving these here but commented-out. I'd like to be able to accept multiple
- # input types again later, but it's a low priority.
- # expect_true(all(distanceFromPath(cbind(trajectory, 3500), cbind(path, 3500)) < distancePrecision))
- # expect_true(all(distanceFromPath(sp::SpatialPoints(trajectory), sp::SpatialPoints(path)) < distancePrecision))
- # expect_true(all(distanceFromPath(as.data.frame(trajectory), as.data.frame(path)) < distancePrecision))
+ # data.frame
+ expect_equal(distanceFromPath(data.frame(trajectory$longitude, trajectory$latitude),
+ data.frame(path$longitude, path$latitude))$horizontal,
+ rep(0, trajectoryLength),
+ tolerance = 1)
+
+ # matrix
+ expect_equal(distanceFromPath(cbind(trajectory$longitude, trajectory$latitude),
+ cbind(path$longitude, path$latitude))$horizontal,
+ rep(0, trajectoryLength),
+ tolerance = 1)
+
+ # SpatialPoints
+ expect_equal(distanceFromPath(sp::SpatialPoints(cbind(trajectory$longitude, trajectory$latitude)),
+ sp::SpatialPoints(cbind(path$longitude, path$latitude)))$horizontal,
+ rep(0, trajectoryLength),
+ tolerance = 1)
})
test_that("small horizontal deviations look OK", {