Understanding the geosphere Package in R: A Deep Dive into Bearings and Courses
In this article, we will explore the geosphere package in R and its functionality related to bearings and courses. We will delve into why the bearings calculated using the bearing() function do not follow the expected 0-360 degrees range.
Introduction to Geosphere Package
The geosphere package is a collection of functions for calculating various geographic quantities, including distances, directions, and coordinates. It provides an efficient way to perform these calculations in R.
Installing and Loading the geosphere Package
To work with the geosphere package, you need to install it first using the following command:
install.packages("geosphere")
After installation, load the package using:
library(geosphere)
Understanding Bearings and Courses
When working with geographic coordinates, two fundamental concepts come into play: bearings and courses.
Bearings
Bearings are directions expressed in degrees, measured clockwise from North (0°). The initial bearing function, part of the geosphere package, calculates this direction. However, it does not follow a 0-360 degrees range but instead is expressed between -180° and +180°, representing left and right turns when facing North.
Courses
Courses, on the other hand, refer to standardized initial course directions (North = 0 or 360 degrees). The direction of a ship or aircraft is not measured by which way you look but rather which course direction it follows. For instance, to determine the course, left turns are subtracted from the North direction (0°), and positive values represent destinations on your right-hand side.
The Issue with geosphere Package Bearings
When using the bearing() function in the geosphere package, it returns angles expressed as azimuths between -180° and +180°. These angles represent directions when facing North, not courses.
To illustrate this difference, let’s consider an example where we calculate bearings from a point to all other points:
# 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)
In this example, the calculated bearings span -180° to +180°. These angles represent directions when facing North but not courses.
Converting Bearings to Courses
To convert these bearings into courses, we can add a full circle (360 degrees) and then apply the modulo operator to determine the remainder after subtracting the maximum positive value (i.e., 0). This approach ensures that the resulting course angle is always between -180° and +180°.
Here’s how you can do this in R:
course <- (b + 360) %% 360 # add full circle, i.e. +360, and determine modulo for 360
pts$BEARING <- b
pts$COURSE <- course
In the above code snippet, we first add 360 to each bearing to “shift” it into a positive range. Then, using the modulo operator (%%), we calculate the remainder of this value divided by 360, effectively determining the course angle.
Conclusion
The geosphere package in R provides an efficient way to calculate geographic quantities like distances and directions. However, its initial bearing function returns angles expressed as azimuths between -180° and +180°, representing directions when facing North, not courses. By adding a full circle and applying the modulo operator, you can convert these bearings into course angles that always fall within the desired 0-360 range.
By understanding how to work with bearings and courses in R using the geosphere package, you’ll be better equipped to tackle geographic data analysis tasks involving directions and coordinates.
Last modified on 2025-02-28