library(tidyverse)
library(tidyjson)
library(lubridate)
library(lutz)
<- read_table(
states "https://people.sc.fsu.edu/~jburkardt/datasets/states/state_capitals_ll.txt",
col_names = c("state", "lat", "long")
|>
) filter(state != "US") |>
mutate(tz = tz_lookup_coords(lat, long, method = "accurate"))
<- function(state, lat, long, tz, wait = 0.1) {
get_datetimes Sys.sleep(wait)
tryCatch(
{<- httr::GET(
start_times ::glue(
glue"https://api.g7vrd.co.uk/v1/satellite-passes/25544/{lat}/{long}.json"
),::timeout(10)
httr$content |>
)rawToChar() |>
as.tbl_json() |>
enter_object("passes") |>
gather_array() |>
spread_all() |>
mutate(start_dt = as_datetime(start, tz = tz)) |>
filter(!is.na(start_dt)) |>
arrange(start_dt) |>
pull(start_dt)
<- \(n)
valid_pass if (length(start_times) >= n) start_times[n] else NA_POSIXct_
tibble(
pass1 = valid_pass(1),
pass2 = valid_pass(2),
pass3 = valid_pass(3),
)
},error = function(e) {
warning(glue::glue(
"Error processing API for {state} ({lat},{long}): {e$message}"
))tibble(
pass1 = NA_POSIXct_,
pass2 = NA_POSIXct_,
pass3 = NA_POSIXct_
)
}
)
}
<- bind_cols(
result
states,pmap_df(states, get_datetimes)
)
Pass Times for U.S. State Captials
International Space Station Pass Times (Local Time Zones)
library(leaflet)
<- makeIcon(
rocket_icon iconUrl = "rocket.svg",
iconWidth = 30,
iconHeight = 30
)<- \(date) format(date, "%Y-%m-%d %H:%M:%S")
custom_format_date
<- leaflet(result) |>
m addTiles() |>
addMarkers(
lng = ~long,
lat = ~lat,
icon = rocket_icon,
label = ~ glue::glue("Next pass: {custom_format_date(pass1)}"),
popup = ~ glue::glue(
"<b>{state}</b><br/>",
"Pass 1: {custom_format_date(pass1)}<br/>",
"Pass 2: {custom_format_date(pass2)}<br/>",
"Pass 3: {custom_format_date(pass3)}"
)
)
m
Drawing the Route of the ISS
library(leaflet.extras2)
|>
m addArrowhead(
data = result |>
filter(!is.na(pass1)) |>
arrange(pass1),
lat = ~lat,
lng = ~long,
color = "red",
options = arrowheadOptions(
frequency = "200px",
size = "10px",
color = "purple",
opacity = 1,
fill = TRUE
) )