## https://goldengloberace.com/the-route/
library(ggplot2)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(geosphere)
library(mapproj)
## Loading required package: maps
## World map
world <- map_data("world")
worldmap <- ggplot(world) +
  geom_path(aes(x = long, y = lat, group = group), size = .2) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank()) +
  labs(x = "", y = "")

## Round the World Itinerary
api <- readLines("google.api") # Text file with the secret API key
register_google(key = api)

## Itinerary
ggrace <- read_csv("golden-globe-race.csv")
## Parsed with column specification:
## cols(
##   From = col_character(),
##   lon = col_logical(),
##   lat = col_logical()
## )
locations <- ggrace$From[ggrace$From != "Waypoint"]
ggrace <- tibble(From = locations) %>%
  bind_cols(geocode(locations))
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Les+Sables-d%E2%80%99Olonne,+France&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Canary+Islands,+Spain&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Trindade+and+Martin+Vaz,+Brazil&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Cape+of+Good+Hope,+South+Africa&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Cape+Leeuwin,+Australia&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Storm+Bay,+Australia&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Cape+Horn,+Chile&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Trindade+and+Martin+Vaz,+Brazil&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Canary+Islands,+Spain&key=xxx-8CdCyNc
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Les+Sables-d%E2%80%99Olonne,+France&key=xxx-8CdCyNc
## Split travel past 180 degrees
dl <- which(abs(diff(ggrace$lon)) > 180)
dr <- ifelse(ggrace$lon[dl] < 0, -180, 180)
dateline <- tibble(From = "dateline",
                   lon = c(dr, -dr),
                   lat = rep(mean(ggrace$lat[dl:(dl + 1)]), 2))
ggrace <- rbind(ggrace[1:dl, ], dateline,
                ggrace[(dl + 1):nrow(ggrace), ])

## Visualise
worldmap +
  geom_point(data = ggrace, aes(lon, lat), colour = "red", size = 4) +
  geom_path(data = ggrace, aes(lon, lat), colour = "red", size = 1) +
  coord_map("azequidistant", orientation = c(90, 0, 270))

## Great Circle Distance
sapply(1:(nrow(ggrace) - 1), function(l)
  distVincentyEllipsoid(ggrace[l, 2:3], ggrace[(l + 1), 2:3]) / 1000) %>%
  sum()
## [1] 46673.17
## Convert to Flat Earth Coordinates
flatearth.coords <- mapproject(world$long, world$lat,
                               "azequidistant", orientation = c(90, 0, 270))
r <- 6378.137
flatearth.coords <- mutate(world,
                           x = flatearth.coords$x * r,
                           y = flatearth.coords$y * r) %>%
  select(x, y, group, order, region, subregion)

flatearth <- ggplot(flatearth.coords) +
  geom_path(aes(x, y, group = group), size = .2) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank()) +
  labs(x = "", y = "") + 
  coord_equal()

coords <- mapproject(ggrace$lon, ggrace$lat, "azequidistant",
                     orientation = c(90, 0, 270))
coords <- tibble(x = coords$x * r, y = coords$y * r)

# Travel Distance
sum(sqrt(diff(coords$x)^2 + diff(coords$y)^2))
## [1] 101046.8
flatearth + 
  geom_point(data = coords, aes(x, y), colour = "red", size = 4) +
  geom_path(data = coords, aes(x, y), colour = "red", size = 1)