A package to make Spherical Earth computations easier.

Use

This package relies heavily on the geosphere package. The added value here is that it makes it much easier to use the spherical trigonometry functions with data in data.frames.

Key Features

library(dplyr)
library(data.table)
library(earthtools)

First, some setup data:

# for use in dplyr operations
airports <- data.frame(airport=c("EWR", "PHL", "JFK", "LGA"),
                       latitude=c(40.6924798333333, 39.8720833333333, 
                                  40.63992575, 40.77725),
                       longitude=c(-74.1686867777778, -75.2406611111111,
                                   -73.7786949722222, -73.8726111111111))

# for use in data.table operations
airports_dt <- setDT(copy(airports))

jfk <- airports %>% filter(airport=="JFK")

Now we can compute distances:

airports %>%
  mutate(dist=compute_dist_haversine(latitude, longitude, jfk$latitude, jfk$longitude))

Determine the initial/final bearing

airports %>%
  filter(airport!="JFK") %>%
  mutate(bearing_initial=compute_bearing_initial(latitude, longitude, 
                                            jfk$latitude, jfk$longitude),
         bearing_terminal=compute_bearing_terminal(latitude, longitude, 
                                              jfk$latitude, jfk$longitude))

And do point projections. Since the projected coordinates have both a latitude and longitude output, this was implemented as an S3 method to support both dplyr and data.table use cases.

The dplyr way:

airports %>%
  compute_projection(latitude, longitude, bearing=90, distance=10)

The data.table way:

airports_dt[,
            c("end_latitude", "end_longitude"):=compute_projection(latitude, longitude, 
                                                              bearing=90, distance=10)
            ]
airports_dt

See the function documentation for all supported operations.

Installation

# install.packages("devtools")
devtools::install_github("mitre/earthtools")


mitre/earthtools documentation built on May 21, 2019, 1:19 p.m.