简体   繁体   中英

In geosphere package in R, why aren't bearings in 0-360 degrees?

When I calculated bearings between points using the bearing function in the geosphere package, the resulting bearings spanned -180 - 180 degrees. However, based on the geosphere package documentation , I expected the bearings to span 0-360 degrees. Here's a quote from the documentation:

Directions are expressed in degrees (North = 0 and 360, East = 90, Sout = 180, and West = 270 degrees).

What am I missing?

Here's a small example:

# set up
library(geosphere)
library(ggplot2)

# create data frame of long/lat
long <- c(-55.25, -55.25, -55.25, -55, -55, -55, -54.75, -54.75, -54.75)
lat <- c(-13.5, -13.25, -13, -13.5, -13.25, -13, -13.5, -13.25, -13)
id <- c("a", "b", "c", "d", "e", "f", "g", "h", "i")
pts <- data.frame(id=id, long=long, lat=lat)

# plot
ggplot(pts, aes(x=long, y=lat, colour=id)) + 
  geom_point()

# calculate bearings from point e to all other points
pts <- pts[,c(2:3)]
b <- bearing(pts[5,], pts)

# I expected this:
# b[1] = 225
# b[2] = 270
# b[3] = 315
# but instead, found this:
b[1]
b[2]
b[3]

Dependent on the navigational task, terms like 'angle', 'direction', 'heading', and/or 'bearing' can mean different things.

The documentation of the bearing() function speaks about

the initial bearing (direction; azimuth) to go from point p1 to point p2.

Thus, the directions that the function geosphere::bearing() deliver are azimuths (angles). Obviously, these are expressed in degrees -180 ... 180. That is the relative direction you would turn when facing North. This is important as it assumes you are looking to the North and thus your destination (or point p2) could be reached with an initial left/right turn, ie negative azimuths reflect a left turn, positive azimuths showing your destination on your right-hand side.

When you speak about a standardised initial course direction (North := 000 or 360 degrees), your reference is not which way you look , but which course direction you follow. For example, ships or aircraft fly a specific course while they have to correct for wind offsets. I do not go into more details here on the difference between course and heading (the direction the nose of the ship or aircraft points). However, in order to determine the course, a left turn (negative azimuth) needs to be subtracted from the 'North direction 360', while positive azimuths are added to the (North direction interpreted as 0).

To fix this mathematically, you can go the long angle-math way (cf this atan-based solution ).

We can force a positive sign by adding a "full circle" (360 degrees) and then check how far we have moved beyond the North-mark (360) by using the modulo operator. Eg 90 + 360 = 450 with 450 %modulo-360% = 1 * 360 + 90 = 90. For negative azimuths, this yields -90 + 360 = 270 or expressed as modulo of 360: 270 = 0 * 360 + 270. The modulo is always the rest after the plus. Please note that you could also add 2 or more full circles. This will not impact the rest .

To do this in R simply use the modulo operator, ie %%, to determine the rest beyond 360 :

course <- (b + 360) %% 360 # add full circle, i.e. +360, and determine modulo for 360
pts$BEARING <- b
pts$COURSE  <- course
pts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM