Calculate geodesic bearing to a location or locations based on longitude, latitude (from) input vectors and longitude, latitude (to) input vectors. The unit of bearing is degrees. The to values may be a single value or individual to each from location.
track_bearing_to(x, y, to_x, to_y)
longitude
latitude
longitude vector of to location/s
latitude vector of to locations/s
a numeric vector of absolute bearing-to in degrees, see Details
No missing values are required as padding, but input data with NA
s will incur an
NA
in the output.
To use this on multiple track ids, use a grouped data frame with tidyverse code like
data %>% group_by(id) %>% mutate(bearing_to = track_bearing_to(lon, lat, to_lon, to_lat))
.
Absolute bearing is relative to North (0), and proceeds clockwise positive and anti-clockwise
negative N = 0, E = 90, S = +/-180, W = -90
.
There is no NA
padding in the output value (though missing values in the input will be mirrored
in the output).
track_bearing_to(trips0$x, trips0$y, to_x = 147, to_y = -42)[1:10]
#> [1] 99.91451 101.82232 102.51534 100.00572 100.66705 99.97313 100.85156
#> [8] 102.60897 105.31088 103.43349
# N E S W
track_bearing_to(0,0, c(0, 10, 0, -10), c(5, 0, -5, 0))
#> [1] 0 90 180 -90
# maximum and minimum value are the same direction (due south)
track_bearing(c(0, -0.00001), c(0, -1))
#> [1] -179.9994 NA
track_bearing(c(0, 0.00001), c(0, -1))
#> [1] 179.9994 NA
# the absolute minimum is north
track_bearing(c(0, 0), c(0, 1))
#> [1] 0 NA