-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab 3.qmd
More file actions
242 lines (199 loc) · 7.55 KB
/
Copy pathLab 3.qmd
File metadata and controls
242 lines (199 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
title: "Lab 4"
author: "Kai, Eva, Chloe"
format:
html:
self-contained: true
code-tools: true
code-fold: true
editor: visual
execute:
echo: true
include: true
message: false
warning: false
---
```{r}
# libraries
library(here)
library(tidyverse)
library(httr)
library(jsonlite)
library(leaflet)
library(leaflet.extras2)
```
**Create one Quarto file for all of Lab 4.**
Make sure your final file is carefully formatted, so that each analysis is clear and concise. Be sure your knitted .html file shows all your source code, including your function definitions.
## The Open-Notify API
One of the Open-Notify APIs provides predictions of pass times for a given location when given the corresponding latitude, longitude, and altitude are given:
[Pass Times API](https://g7vrd.co.uk/public-satellite-pass-rest-api)
### Pass Times for U.S. State Captials
You can find the latitudes and longitudes for the U.S. state capitals at [this site](https://people.sc.fsu.edu/~jburkardt/datasets/states/states.html).
1. Use the API to get the next 3 predicted pass times for all of the U.S. state capitals. Organize all of this information into a nice data frame (or data frame-like object) that will be easy to use for mapping.
```{r}
# Loading in capital dataset
capital <- read.table("https://people.sc.fsu.edu/~jburkardt/datasets/states/state_capitals_ll.txt")
capital <- capital |>
rename(State = V1,
Latitude = V2,
Longitude = V3)
```
```{r}
# Function to get pass times for a given location
pass_times <- function(latitude, longitude) {
url <- paste0("https://api.g7vrd.co.uk/v1/satellite-passes/25544/",
latitude, "/", longitude, ".json")
pass <- GET(url)
data <- fromJSON(rawToChar(pass$content))
if (length(data$pass) == 0) {
return(NULL)
}
dataframe <- data |>
data.frame(data) |>
slice_head(n = 3)
dataframe
}
```
```{r}
# Source for bind_rows(): https://dplyr.tidyverse.org/reference/bind_rows.html
#Creating dataset that includes each pass time per state capital
map_passes <- map2(capital$Latitude, capital$Longitude, pass_times) |>
bind_rows()
```
### Mapping the Data
1. Map the U.S. state capitals using `leaflet`
```{r}
# Map with U.S. capitals
leaflet() |>
addProviderTiles(providers$Stadia.StamenTerrain) |>
addCircleMarkers(data = map_passes,
lat = ~lat,
lng = ~lon,
radius = 4,
color = "darkgreen") |>
setView(lng = -100, lat = 40, zoom = 4)
```
2. Find an image of your choosing to use as the marker icon, that's relevant for these data.
```{r}
# Source for rocket image: https://pngtree.com/element/down?id=NTUzMTc4Nw==&type=1&time=1714350436&token=YmIyZDI1MzQwMzU0NTdkYjJmNDM5NTcxNDEwZDhjYjY=&t=0
# Source for adding marker icon: https://rdrr.io/cran/leaflet/man/icons.html
# Map with rocket marker icon
leaflet() |>
addProviderTiles(providers$Stadia.StamenTerrain) |>
addMarkers(data = map_passes,
lat = ~lat,
lng = ~lon,
icon = ~ icons(iconUrl = "Rocket.png",
iconWidth = 40,
iconHeight = 40)) |>
setView(lng = -100,
lat = 40,
zoom = 4)
```
3. The hover-over information for each marker should include the name of the state capital and the soonest predicted pass time. The click information should include the name of the state capital and all three predicted pass times. Make sure these times are in an easy-to-read format.
```{r}
# Source for capital data: https://www.thespreadsheetguru.com/list-united-states-capitals-abbreviations/
# Source for needing "\\" with a "." to separate by it: https://stackoverflow.com/questions/26665100/how-to-use-the-strsplit-function-with-a-period
# Source for collapse in paste: https://www.digitalocean.com/community/tutorials/paste-in-r
# Source for gsub function: https://www.digitalocean.com/community/tutorials/sub-and-gsub-function-r
# Reading in dataset with capital names
city <- read.csv(here("Data", "State Capitals.csv"))
# Joining together datasets
map_data <- map_passes %>%
left_join(capital, by = c("lon" = "Longitude")) %>%
left_join(city, by = "State") |>
# Making popup for click info column
# Separating date and time pieces of the passes.start
separate(passes.start,
into = c("Date", "Time.unedit"),
sep = "T", remove = FALSE) |>
# Separating the hour, minutes, and seconds of the time from the milliseconds
separate(Time.unedit,
into = c("Time", "Rest"),
sep = "\\.", remove = FALSE) |>
# Pasting together the cleaned date and time pieces
mutate(Date.Time = paste(Date, Time)) |>
# Pasting together the pass times by state
group_by(State) |>
mutate(state.date.times = paste(Date.Time, collapse = ", "),
click = paste("<strong>State Capital:", State.Capital,
"<br>",
"</strong>Predicted Passes:", "<br>",
gsub(",", "<br>", state.date.times, fixed = TRUE))) |>
# Separating the first pass time for each state
separate(state.date.times,
into = c("First.Pass", "Rest Passes"),
sep = ",", remove = FALSE)
#Creating hover labels for each state capital
hover <- paste(
"<strong>State Capital:", map_data$State.Capital,
"</strong><br>Next Pass:", map_data$First.Pass) %>%
lapply(htmltools::HTML)
```
```{r}
# Map with a click popup and hover popup
leaflet() |>
addProviderTiles(providers$Stadia.StamenTerrain) |>
addMarkers(data = map_data,
lat = ~lat,
lng = ~lon,
icon = ~icons(iconUrl = "Rocket.png",
iconWidth = 40,
iconHeight = 40),
popup = ~click,
label = ~hover
) |>
setView(lng = -100,
lat = 40,
zoom = 4)
```
### Drawing the Route of the ISS
Check out this video for [adding polylines](https://www.youtube.com/watch?v=iKESL0Iwmmw) to a `leaflet` map.
1. Overlay lines on your map that connect the U.S. state capitals in order of pass times (essentially east to west) so that we can see the expected pass order.
```{r}
#Arranging the data from east to west
map_data_arrange <- map_data |>
ungroup() |>
arrange(-lon)
#Renaming hover labels for each state capital
hover2 <- paste(
"<strong>State Capital:", map_data_arrange$State.Capital,
"</strong><br>Next Pass:", map_data_arrange$First.Pass) %>%
lapply(htmltools::HTML)
```
```{r}
#Leaflet Map
leaflet(map_data_arrange) |>
addProviderTiles(providers$Stadia.StamenTerrain) |>
addMarkers(lat = ~lat,
lng = ~lon,
icon = ~icons(iconUrl = "Rocket.png",
iconWidth = 40,
iconHeight = 40),
popup = ~click,
label = ~hover) |>
addPolylines(data = map_data_arrange,
lat = ~lat,
lng = ~lon,
color = "darkgreen",
label = ~hover2) |>
setView(lng = -100,
lat = 40,
zoom = 4) |>
addPolylines(lat = ~lat,
lng = ~lon,
color = "darkgreen",
opacity = 1,
weight = 4,
) |>
addArrowhead(lat = ~lat,
lng = ~lon,
color = "darkgreen",
opacity = 1,
weight = 4)|>
addControl(
html = "Map of the Order of Pass Times <br> for State Capitals<div>",
position = "topright"
)
## Source for addArrowhead: https://rdrr.io/cran/leaflet.extras2/src/R/arrowhead.R
```