flightpathr

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

commit da7705cec5650b6a09891608121f9fb8f9c2ce01
parent c4dc250d8eeb3bac3a8af8d4a2ad3492f0a803dc
Author: eamoncaddigan <eamon.caddigan@gmail.com>
Date:   Wed, 27 Apr 2016 12:02:11 -0400

Input checking.

Diffstat:
MR/coordsToBearing.R | 10+++++++---
Mtests/testthat/test_coordsToBearing.R | 4++++
2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/R/coordsToBearing.R b/R/coordsToBearing.R @@ -9,8 +9,12 @@ #' @export coordsToBearing <- function(trajectory) { trajectoryCoords <- get3dCoords(trajectory) - numCoords <- nrow(trajectoryCoords) - bearings <- geosphere::bearing(trajectoryCoords[1:(numCoords-1), 1:2], - trajectoryCoords[2:numCoords, 1:2]) + numPoints <- nrow(trajectoryCoords) + if (numPoints < 2) { + stop("At least two time points must be specified") + } + + bearings <- geosphere::bearing(trajectoryCoords[1:(numPoints-1), 1:2], + trajectoryCoords[2:numPoints, 1:2]) return(c(bearings, NA)) } diff --git a/tests/testthat/test_coordsToBearing.R b/tests/testthat/test_coordsToBearing.R @@ -1,6 +1,10 @@ library(flightpathr) context("coordsToBearing") +test_that("Input checking works", { + expect_error(coordsToBearing(matrix(c(-74.577166, 39.457583), nrow = 1))) +}) + test_that("Equatorial routes have constant bearing", { trajectory1 <- cbind(longitude = seq(-180, 180, length.out = 100), latitude = 0)