/earthtools

Tools for working on planet Earth

Primary LanguageRApache License 2.0Apache-2.0

---
output: 
  html_document:
    keep_md: true
---

A package to make Spherical Earth computations easier.

## Use

This package relies heavily on the [`geosphere`](https://cran.r-project.org/web/packages/geosphere/index.html) package.
The added value here is that it makes it much easier to use the spherical trigonometry functions with data in `data.frame`s. 

## Key Features

```{r, message=FALSE, warning=FALSE}
library(dplyr)
library(data.table)
library(earthtools)
```

First, some setup data:
```{r}
# 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:
```{r}
airports %>%
  mutate(dist=compute_dist_haversine(latitude, longitude, jfk$latitude, jfk$longitude))
```

Determine the initial/final bearing
```{r}
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:
```{r}
airports %>%
  compute_projection(latitude, longitude, bearing=90, distance=10)
```

The `data.table` way:
```{r}
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

```{r, eval=FALSE}       
# install.packages("devtools")
devtools::install_github("mitre/earthtools")
```