The United States of Buc’ee: Finding and mapping the most efficient route to visit all 46 Buc'ees locations
Loading in the necessary Libraries
library(plotly)
library(tidyverse)
library(knitr)
library(kableExtra)
library(geosphere)
library(doParallel)
library(ggmap)
library(leaflet)
library(leaflet.extras)
library(htmlwidgets)
library(htmltools)
library(TSP)
Reading in csv file created through webscraping with python
bucees <- read.csv('bucees.csv')
bucees <- bucees %>%
select(city, state, full_addy, lat, lon)
bucees
Create distance matrix between locations to run TSP model
bucees_coords <- bucees %>% select(lon, lat)
dist_matrix <- as.matrix(
distm(bucees_coords, fun = distHaversine))/1000
rownames(dist_matrix) <- bucees$city
colnames(dist_matrix) <- bucees$city
dist_matrix %>%
kbl(digits = 2)
Run TSP Model to find most efficient path and output to a dataframe
tsp_prob <- TSP(dist_matrix)
tsp_prob <- insert_dummy(tsp_prob, label = 'dummy')
tour <- solve_TSP(
tsp_prob,
method = 'two_opt',
control = list(rep =16)
)
path <- names(cut_tour(tour, 'dummy'))
route <- as.data.frame(path)
route$order <- 1:nrow(route)
route <- route %>%
select(order, city = path)
bucees_plot <- left_join(route, bucees, by = 'city')
bucees_plot
Create interactive map of each Bucee’s location using the leaflet package
middle_lon = mean(bucees_plot$lon)
middle_lat = mean(bucees_plot$lat)
map <- leaflet(bucees_plot) %>%
addTiles() %>%
setView( lng = middle_lon, lat = middle_lat, zoom = 5.25 ) %>%
addCircleMarkers(popup = ~city) %>%
addPolylines(lat = ~lat, lng = ~lon, color = 'green')
Load in API key for ggmaps package
ggmap::register_google(key = "KEY_GOES_HERE", write = FALSE)
Create model to loop through location data and find most efficient driving routes between Buc’ees using Google Maps API
route <- data.frame(matrix(ncol = 3, nrow = 0))
x <- c("lat", "lon", "route")
colnames(route) <- x
i = 1
while (i < 47) {
singlepath <-trek(bucees_plot$full_addy[i], bucees_plot$full_addy[(i + 1)])
i <- (i + 1)
route <- rbind(route, singlepath)
}
Read in the saved route info and plot the path
road_path <- read.csv('bucees_route.csv')
map <- leaflet(road_path,
options = leafletOptions(attributionControl = FALSE,
minZoom = 5.25
)) %>%
addTiles('https://tile.jawg.io/9675b164-059d-4351-aa07-ed736a72cf8d/{z}/{x}/{y}{r}.png?access-token=KEY_GOES_HERE') %>%
setView( lng = -89.8485, lat = 33.7966, zoom = 5.25 ) %>%
addPolylines(lat = ~lat,
lng = ~lon,
color = '#D31145',
opacity = 0.9)
map <- map %>%
setMaxBounds(-110, 25, -75, 45)
Convert Buc’ees logo to a plotabble icon and add markers for each location
bucee_icon <- makeIcon(
iconUrl = "https://upload.wikimedia.org/wikipedia/en/thumb/2/2c/Buc-ee%27s_beaver.svg/352px-Buc-ee%27s_beaver.svg.png?20210624041100",
iconWidth = 25, iconHeight = 25)
map_with_bucee <- map %>%
addMarkers(
data = bucees_plot,
label = ~city,
icon = bucee_icon) %>%
addResetMapButton()
Overlay title card that was made in Adobe Illustrator
bucee_with_title <- map_with_bucee %>%
htmlwidgets::onRender("
function(el, x) {
console.log(this);
var myMap = this;
var imageUrl ='https://raw.githubusercontent.com/trevriekenberg/data/main/Bucees%20Sign-02-02.png';
var imageBounds = [[40.00, -101.00], [36.00, -91]];
L.imageOverlay(imageUrl, imageBounds, { opacity: 0.95}).addTo(myMap);
}
")
bucee_with_title
Save leaflet map as an html widget
saveWidget(bucee_with_title, 'bucees_map.html')