flightpathr

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

test_coordsToBearing.R (2380B)


      1 library(flightpathr)
      2 context("coordsToBearing")
      3 
      4 test_that("Equatorial routes have constant bearing", {
      5   trajectory1 <- cbind(longitude = seq(-180, 180, length.out = 100),
      6                        latitude = 0)
      7   trajectory2 <- cbind(longitude = -74.6,
      8                        latitude = seq(89, -89, length.out = 100))
      9 
     10   expect_equal(angleDiff(coordsToBearing(trajectory1[, "longitude"],
     11                                          trajectory1[, "latitude"]),
     12                          rep(90, 100)),
     13                c(rep(0, 99), NA))
     14   expect_equal(angleDiff(coordsToBearing(trajectory2[, "longitude"],
     15                                          trajectory2[, "latitude"]),
     16                          rep(180, 100)),
     17                c(rep(0, 99), NA))
     18 })
     19 
     20 test_that("Non-equatorial routes look OK", {
     21   trajectory <- geosphere::gcIntermediate(c(-119.841499, 34.426194),
     22                                           c(-74.577166, 39.457583),
     23                                           100)
     24 
     25   expect_equal(coordsToBearing(trajectory[, "lon"], trajectory[, "lat"])[1],
     26                geosphere::bearing(trajectory[1, ], trajectory[2, ]))
     27   expect_equal(coordsToBearing(trajectory[, "lon"], trajectory[, "lat"])[99],
     28                geosphere::bearing(trajectory[99, ], trajectory[100, ]))
     29   expect_true(is.na(coordsToBearing(trajectory[, "lon"], trajectory[, "lat"])[100]))
     30 })
     31 
     32 test_that("Trajectories are somewhat reversable", {
     33   trajectory <- geosphere::destPoint(c(-119.841499, 34.426194), b = 68.4,
     34                                      d = seq(0, by = 40281.88, length.out = 100))
     35   bearings <- coordsToBearing(trajectory[, "lon"], trajectory[, "lat"])
     36   destPoints <- matrix(NA, nrow = 100, ncol = 2)
     37   destPoints[1, ] <- trajectory[1, ]
     38   for (r in seq(2,100)) {
     39     destPoints[r, ] <- geosphere::destPoint(trajectory[r-1, ], bearings[r-1],
     40                                             40281.88)
     41   }
     42 
     43   expect_equal(destPoints[1:200], trajectory[1:200], tolerance = 1e-8)
     44 })
     45 
     46 test_that("Repeated long/lats are handled correctly", {
     47   trajectory <- geosphere::gcIntermediate(c(-119.841499, 34.426194),
     48                                           c(-74.577166, 39.457583),
     49                                           100)
     50   trajectory <- rbind(trajectory, trajectory[100, ], trajectory[100, ])
     51   expect_true(all(is.na(coordsToBearing(trajectory[, "lon"], trajectory[, "lat"])[c(100, 101)])))
     52 })