test_createTrajectory.R (2440B)
1 library(flightpathr) 2 context("createTrajectory") 3 4 library(geosphere) 5 6 # Find the lon/lat of the flight path between KACY and K17N flying at 100 knots 7 kacy <- c(-74.5771667, 39.4575833) 8 k17n <- c(-75.0330031, 39.7054758) 9 distMeters <- distHaversine(kacy, k17n) 10 # distMeters m * (1 hr / 100 nmi) * (1 nmi / 1852 m) * (3600 s / 1 hr) 11 timeSec <- round(distMeters * (1/100) * (3600/1852)) 12 trajectoryMat <- gcIntermediate(kacy, k17n, n = timeSec-2, addStartEnd = TRUE) 13 trajectory <- createTrajectory(trajectoryMat[, "lon"], 14 trajectoryMat[, "lat"], 15 rep(2500, timeSec)) 16 17 test_that("Passed arguments are stored correctly", { 18 expect_equal(trajectory$longitude, trajectoryMat[, "lon"]) 19 expect_equal(trajectory$latitude, trajectoryMat[, "lat"]) 20 expect_equal(trajectory$altitude, rep(2500, timeSec)) 21 }) 22 23 test_that("Bearing and groundspeed calculations are correct", { 24 expect_equal(trajectory$timestamp, seq(1, timeSec)) 25 expect_equal(trajectory$groundspeed, rep(100, timeSec), tolerance = 0.5) 26 expect_equal(trajectory$bearing[1:(timeSec-1)], 27 bearing(trajectoryMat[1:(timeSec-1), ], trajectoryMat[2:timeSec, ])) 28 }) 29 30 test_that("Input types are checked", { 31 expect_error(createTrajectory(paste(trajectoryMat[, "lon"]), trajectoryMat[, "lat"]), 32 "\"longitude\" must be a numeric vector") 33 expect_error(createTrajectory(trajectoryMat[, "lon"], paste(trajectoryMat[, "lat"])), 34 "\"latitude\" must be a numeric vector") 35 expect_error(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[, "lat"], paste(rep(2500, timeSec))), 36 "\"altitude\" must be a numeric vector") 37 }) 38 39 test_that("Input lengths are checked", { 40 expect_error(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[1, "lat"]), 41 paste("Vector \"latitude\" has length = 1, expected length =", timeSec)) 42 # Altitude can have length of 1 or timeSec, otherwise there should be an error 43 expect_error(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[, "lat"], rep(2500, 2)), 44 paste("Vector \"altitude\" has length = 2, expected length =", timeSec)) 45 expect_equal(createTrajectory(trajectoryMat[, "lon"], trajectoryMat[, "lat"], 2500)$altitude, 46 rep(2500, timeSec)) 47 }) 48 49 test_that("Trajectories are identified", { 50 expect_true(is.flighttrajectory(trajectory)) 51 expect_false(is.flighttrajectory(trajectoryMat)) 52 })