Skip to content

Instantly share code, notes, and snippets.

@barnettjacob
Last active April 6, 2016 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barnettjacob/58601c78f22616a02c3d3e1fa1aea724 to your computer and use it in GitHub Desktop.
Save barnettjacob/58601c78f22616a02c3d3e1fa1aea724 to your computer and use it in GitHub Desktop.
Drone Sighting Vis

Flight Data Vis

Spots represent Cities where drones have been sighted by pilots. Size of circle represents number of sightings.

library(dplyr)
library(RMySQL)
library(ggplot2)
library(lubridate)
library(tidyr)
library(readxl)
library(stringr)
library(leaflet)
library(ggmap)
#thanks to bob for the code that gets the excel and turns it into a nice format!!
URL1 <- "http://www.faa.gov/uas/media/UAS_Sightings_report_21Aug-31Jan.xlsx"
URL2 <- "http://www.faa.gov/uas/media/UASEventsNov2014-Aug2015.xls"
fil1 <- basename(URL1)
fil2 <- basename(URL2)
if (!file.exists(fil1)) download.file(URL1, fil1)
if (!file.exists(fil2)) download.file(URL2, fil2)
xl1 <- read_excel(fil1)
xl2 <- read_excel(fil2)
flight_data <- setNames(bind_rows(xl2[,1:3],
xl1[,c(1,3,4)]),
c("event_time", "city", "state"))
flight_data$city_state <- paste(flight_data$city, ', ', flight_data$state, sep = '')
#get list of unique cities
cities <- data.frame(flight_data %>% count(city_state))
#initialise empty data and loop through unique city names to get longlat from the geocode
#function in ggmap (just an easy way of interacting with the Google Maps API)
#don't run unless you want to make 400+ calls to the google maps API (takes ~10minutes)
longlats <- data.frame()
for (i in 1:nrow(cities)){
city_state <- cities[i,1]
ll <- geocode(city_state)
record <- cbind(city_state, ll)
longlats <- rbind(longlats, record)
}
#add long/lats to flight data
flight_data <- left_join(flight_data, longlats)
#group data by city
flight_data_grouped <- flight_data %>% group_by(lon, lat) %>% summarise(ct = n())
#create map
leaflet(flight_data_grouped) %>%
addProviderTiles("CartoDB.Positron") %>%
addCircleMarkers(radius = flight_data_grouped$ct/2, color = "steelblue", stroke = FALSE, fillOpacity = 0.6) %>%
setView(lng = mean(flight_data$lon, na.rm = T), lat = mean(flight_data$lat, na.rm = T), zoom = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment