Libraries

# SPDS
library(tidyverse)
library(sf)
library(units)
library(dplyr)

# Data
library(USAboundaries)
library(rnaturalearth)

# Visualization
library(gghighlight)
library(ggrepel)
library(knitr)
library(ggthemes)

Question 1:

1.1 Define a Projection

eqdc = '+proj=eqdc +lat_0=40 +lon_0=-96 +lat_1=20 +lat_2=60 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs'

1.2 Get USA state boundaries

Access state boundaries

Filter data to only continental USA

conus = USAboundaries::us_states() %>%
  filter(!state_name %in% c("Puerto Rico",
                            "Alaska",
                            "Hawaii")) %>% 
  st_transform(5070)

1.3 Get country boundaries for Mexico, the USA, and Canada

World boundaries

Make sure data is in simple features format.

countries_sf = st_as_sf(countries110) %>% st_transform(5070)

Filter to Mexico, USA, Canada

mex_can_us = countries_sf %>% 
  filter(admin %in% c("Canada", "United States of America", "Mexico"))

1.4 Get city locations from the CSV file

cities = cities <- read_csv("../data/uscities.csv") %>% 
  st_as_sf(coords=c("lng","lat"), crs=4326) %>% 
   filter(!state_name %in% c("Puerto Rico",
                            "Alaska",
                            "Hawaii")) %>%  st_transform(5070)

Question 2:

2.1 Distance to USA Border (coastline or national) (km)

conus_resolved = st_union(conus) %>%
  st_cast("MULTILINESTRING")


cities = cities %>% 
  mutate(distance_to_us_border = st_distance(cities, conus_resolved),
         distance_to_us_border = units::set_units(distance_to_us_border, "km"),
         distance_to_us_border = units::drop_units(distance_to_us_border))

farthest5_from_border = cities %>% 
  slice_max(distance_to_us_border, n=5) %>% 
  select(city, state_name, distance_to_us_border) %>% 
  st_drop_geometry()

knitr::kable(farthest5_from_border,
             caption = "5 US Cities Farthest from Border",
             col.names = c("City", "State", "Distance to Border"))
5 US Cities Farthest from Border
City State Distance to Border
Hill City Kansas 1035.632
Palco Kansas 1032.352
Dresden Kansas 1031.450
Jennings Kansas 1030.252
Bogue Kansas 1028.555

2.2 Distance to States (km)

conus_preserved = st_combine(conus) %>%
  st_cast("MULTILINESTRING")

cities = cities %>%
mutate(distance_to_states = st_distance(cities, conus_preserved),
     distance_to_states = units::set_units(distance_to_states, "km"),
     distance_to_states = units::drop_units(distance_to_states))



farthest_from_state = cities %>%
  slice_max(distance_to_states, n=5) %>%
  select(city, state_name, distance_to_states) %>%
  st_drop_geometry()

knitr::kable(farthest_from_state,
           caption = "5 US Cities Farthest from State Border",
            col.names = c("City", "State", "Distance to state border"))
5 US Cities Farthest from State Border
City State Distance to state border
Lampasas Texas 311.6903
Bertram Texas 306.8676
Kempner Texas 304.9356
Florence Texas 301.8986
Harker Heights Texas 300.6722

Distance to Mexico (km)

mexico = countries_sf %>% 
  filter(admin %in% c("Mexico")) 
  
cities = cities %>% 
  mutate(distance_to_mexico = st_distance(cities, mexico),
         distance_to_mexico = units::set_units(distance_to_mexico, "km"),
         distance_to_mexico = units::drop_units(distance_to_mexico))


farthest_from_mexico = cities %>% 
  slice_max(distance_to_mexico, n=5) %>% 
  select(city, state_name, distance_to_mexico) %>% 
  st_drop_geometry()

knitr::kable(farthest_from_mexico,
             caption = "5 US Cities Farthest from Mexico",
             col.names = c("City", "State", "Distance to Mexico"))
5 US Cities Farthest from Mexico
City State Distance to Mexico
Caribou Maine 3292.785
Presque Isle Maine 3277.282
Calais Maine 3181.785
Eastport Maine 3174.086
Old Town Maine 3090.254

Distance to Canada (km)

canada = countries_sf %>% 
  filter(admin %in% c("Canada"))


cities = cities %>% 
  mutate(distance_to_canada = st_distance(cities, canada),
         distance_to_canada = units::set_units(distance_to_canada, "km"),
         distance_to_canada = units::drop_units(distance_to_canada))

farthest_from_canada = cities %>% 
  slice_max(distance_to_canada, n=5) %>% 
  select(city, state_name, distance_to_canada) %>% 
  st_drop_geometry()

knitr::kable(farthest_from_canada,
             caption = "5 US Cities Farthest from Canada",
             col.names = c("City", "State", "Distance to Canada"))
5 US Cities Farthest from Canada
City State Distance to Canada
Guadalupe Guerra Texas 2255.590
Sandoval Texas 2254.783
Fronton Texas 2253.929
Fronton Ranchettes Texas 2251.062
Evergreen Texas 2250.952

Question 3

3.1 Data

largest10_cities =cities %>% 
            slice_max(population, n=10)

cityfromborder = cities %>% 
  arrange(-distance_to_states) %>% 
  slice_max(distance_to_states, n = 5)

ggplot()+
  geom_sf(data = mex_can_us, color = "black") +
  geom_sf(data = conus, color = "black") +
  geom_sf(data = largest10_cities, color = "red", size = 2)+
  ggrepel::geom_label_repel(
    data = largest10_cities,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 2
  )

3.2 City Distance from the Border

ggplot() + 
  geom_sf(data = conus) +
  geom_sf(data = cities, aes(col = distance_to_us_border), size = .1) +
  scale_color_gradient(low = "gray", high = "red") +
  geom_sf(data = farthest5_from_border, col = "blue", size = 2)+
    ggrepel::geom_label_repel(
      data = farthest5_from_border,
      aes(label = city, geometry = geometry),
          stat = "sf_coordinates",
          size = 2)

3.3 City Distance from Nearest State

ggplot()+
geom_sf(data = farthest5_from_border, color = "green")+
geom_sf(data = farthest_from_state)+
geom_sf(data = farthest_from_state, aes(col = as.numeric(dist)), size=.1) +
ggrepel::geom_label_repel(data = farthest5_from_border,
                        aes(label = city, geometry=geometry),
                       stat = "sf_coordinates",
                      size = 2)

Question 4 Real World Application

4.1 Quantifing Border Zone

mex_cities_160km = cities %>% 
  filter(distance_to_mexico<160)

can_cities_160km = cities %>% 
  filter(distance_to_canada<160)

to_border

cities = cities %>% 
  mutate(mex_cities_160km = distance_to_mexico<160) %>% 
  mutate(can_cities_160km = distance_to_canada<160)

4.2 Mapping Border Zone

ggplot()+
  geom_sf(data = mex_can_us, color = "black")+
  geom_sf(data = mex_cities_160km, color = "red")+
  geom_sf(data = can_cities_160km, color = "blue")