# flightconflicts

Tools to analyze conflicts between aircraft.

test_calculateTCPA.R (1415B)

```      1 library(flightconflicts)
2 context("calculateTCPA")
3
4 library(geosphere)
5 library(flightpathr)
6
7 kacy <- c(-74.5771667, 39.4575833)
8 kphl <- c(-75.2408658, 39.8722494)
9
10 meterPer200Knots <- 0.009719
11
12 test_that("correct tCPA for head-on collision", {
13   trajectory1 <- createTrajectory(kacy[1], kacy[2],
14                                   bearing = bearingRhumb(kacy, kphl),
15                                   groundspeed = 200)
16   trajectory2 <- createTrajectory(kphl[1], kphl[2],
17                                   bearing = bearingRhumb(kphl, kacy),
18                                   groundspeed = 200)
19
20   expect_equal(calculateTCPA(trajectory1, trajectory2),
21                distGeo(kacy, kphl)/2 * meterPer200Knots,
22                tolerance = 1)
23 })
24
25 test_that("TCPA for actual trajectories", {
26   numPoints <- round(distGeo(kacy, kphl) * meterPer200Knots)
27   coords <- gcIntermediate(kacy, kphl,
28                            n = numPoints)
29   trajectory1 <- createTrajectory(coords[, "lon"], coords[, "lat"])
30   trajectory2 <- createTrajectory(rev(coords[, "lon"]), rev(coords[, "lat"]))
31   tcpaVector <- calculateTCPA(trajectory1, trajectory2)
32
33   expect_equal(tcpaVector[seq(ceiling(numPoints/2)+1, numPoints)],
34                0 * seq(ceiling(numPoints/2)+1, numPoints))
35
36   expect_equal(diff(tcpaVector[seq(1, floor(numPoints/2))]),
37                -1*diff(seq(1, floor(numPoints/2))),
38                tolerance = 1e-3)
39 })
```