flightconflicts

Tools to analyze conflicts between aircraft.
git clone https://git.eamoncaddigan.net/flightconflicts.git
Log | Files | Refs | README | LICENSE

lonlatToXY.R (1847B)


      1 #' Convert lon/lat coordinates to north/east coordinates (in feet) using a 
      2 #' flat-Earth approximation.
      3 #' 
      4 #' @param longitude A numeric vector.
      5 #' @param latitude A numeric vector.
      6 #' @param originLongintude The logntidue for which x = 0 feet.
      7 #' @param originLatitude The latitude for which x = 0 feet.
      8 #' @return A n x 2 matrix giving the x and y distance (respectively), in feet,
      9 #'   of each longitude/latitude pair from the origin longitude/latitude.
     10 lonlatToXY <- function(longitude, latitude, originLongitude, originLatitude) {
     11   # This implementation is based on code from:
     12   # http://williams.best.vwh.net/avform.htm#flat
     13   lon0 <- originLongitude * pi / 180.0
     14   lat0 <- originLatitude * pi / 180.0
     15   
     16   dlon <- longitude * pi / 180.0 - lon0
     17   dlat <- latitude * pi / 180.0 - lat0
     18   
     19   # Radius in feet and flattening based on WGS84
     20   a <- 20925646
     21   #a <- 6378137
     22   f <- 1/298.257223563
     23   e2 <- f*(2-f)
     24   
     25   R1 <- a*(1-e2)/(1-e2*(sin(lat0))^2)^(3/2)
     26   R2 <- a/sqrt(1-e2*(sin(lat0))^2)
     27   
     28   xyFeet <- cbind(R2 * cos(lat0) * dlon, 
     29                   R1 * dlat)
     30   
     31   return(xyFeet)
     32 }
     33 
     34 #' Convert a bearing (in degrees) and velocity (in knots) to north and east 
     35 #' velocity (in ft / s).
     36 #' 
     37 #' @param bearing A numeric vector giving the instantaneous direction of the 
     38 #'   velocity in degrees.
     39 #' @param speed A numeric vector giving the instantaneous magnitude of the 
     40 #'   velocity in knots.
     41 #' @return A n x 2 matrix giving the north/south and east/west components of the
     42 #'   velocity, in feet/s.
     43 bearingToXY <- function(bearing, speed) {
     44   # Velocity should be in knots. Convert to ft / s
     45   fps <- speed * 1.68781
     46   # Bearing should be degrees from north. Convert to radians.
     47   theta <- bearing * pi / 180
     48   # Return x and y components of the velocity in ft / s
     49   return(cbind(fps * sin(theta),
     50                fps * cos(theta)))
     51 }