# Load packages
library(tidyverse) # All the tidy things
library(jsonlite) # Converting json data into data frames
library(magrittr) # Extracting items from list objects using piping grammar
library(httr) # provides a wrapper for the curl package, allows us to use GET()MyersAssignment5
Assignment 5 - APIs w/ ISS Coordinates
In the API taken from Open-Notify (http://open-notify.org/), we are able to track the ever-changing location of the International Space Station - which moves at over 28,000 km/hr, so its coordinates are constantly updating!
The API is simple - it requires no input, and it outputs a timestamp vector, a mission status vector, and an ISS position vector, which includes latitude and longitude. The data is scraped from the NASA and NORAD databases.
This API could be used for a multitude of reasons - simply for the passion of space/spacecraft, the velocity of the ISS, and other factors that could be calculated from it.
Walkthrough
The first step manipulating this API is to load the correct packages that will allow us to use different functions and code within the following blocks.
In this section, we are defining the endpoint URL, which is taken directly from Open Notify and provides the current coordinates and timestamps - this is in the form of the JSON file. The response is processed using the ‘context’ function, which extracts it with UTF-8 encoding.
ISS_location <- "http://api.open-notify.org/iss-now.json"
ISS_location_data <-
GET(ISS_location) %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()After we’ve done so, we can go ahead and print the timestamp, latitude, and longitude of a single recorded time - in pulling these objects, we can observe the distance traveled between recorded timestamps. This will allow us to ensure that the code is working thus far.
cat(" Timestamp:", ISS_location_data$timestamp, "\n",
"Latitude:", ISS_location_data$iss_position$latitude, "\n",
"Longitude:", ISS_location_data$iss_position$longitude, "\n") Timestamp: 1729654232
Latitude: 35.9189
Longitude: -8.0881
In this section, we create an empty data frame ISS_locations_df to store the last 10 coordinates of the ISS. We can define a function, store_ISS_location(), which makes a call to the API using GET to retrieve the current location. The function processes the response by converting the JSON data into a structured format and stores the timestamp, latitude, and longitude in a new data frame row.
Next, we use a for loop to call this function 10 times in order to collect 10 consecutive ISS location coordinates. After each API call, the newly fetched coordinates are stored in the ISS_locations_df data frame using the bind_rows() function. Between each API call, the Sys.sleep(10) command forces a 10-second delay, ensuring that the ISS, since it moves as such an intense velocity.
Finally, after the loop completes, we print out the collected data, showing the last 5 recorded locations of the ISS. This allows for easy visibility to see the difference between the coordinates.
ISS_locations_df <- data.frame()
#create a function to store ISS location
store_ISS_location <- function() {
ISS_location_data <- GET(ISS_location) %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()
#create a new row for
new_row <- data.frame(
timestamp = ISS_location_data$timestamp,
latitude = ISS_location_data$iss_position$latitude,
longitude = ISS_location_data$iss_position$longitude
)
}
# Loop to collect the last 5 ISS locations
for (i in 1:5) {
new_location <- store_ISS_location()
ISS_locations_df <- bind_rows(ISS_locations_df, new_location)
# Wait a few seconds before making the next request
if (1<5) {
Sys.sleep(7)
}
}
# Display the last 5 locations
print(ISS_locations_df) timestamp latitude longitude
1 1729654232 35.9189 -8.0881
2 1729654239 36.2306 -7.6573
3 1729654246 36.5200 -7.2518
4 1729654253 36.8077 -6.8429
5 1729654260 37.0938 -6.4309