title: “Geography 176A” author: “Van Jackson” subtitle: ‘Lab 03: Distances and the Border Zone’ output: html_document: theme: journal —

# SPDS
library(tidyverse)
## ── Attaching packages ──────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.1
## ✓ tidyr   1.1.1     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(units)
## udunits system database from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/units/share/udunits
# Data
library(USAboundaries)
library(rnaturalearth)

# Visualization
library(gghighlight)
library(ggrepel)
library(knitr)
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'
 
# remotes::install_github("ropensci/USAboundaries")
# remotes::install_github("ropensci/USAboundariesData")
# remotes::install_github("ropenscilabs/rnaturalearthdata")

#Question 1

#1.1
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'

#This is the base projection being used to map the North American vontinent for this lab. 

#1.2
ContinentalUS = USAboundaries::us_states(resolution = "low") %>% 
  filter(name != "Hawaii") %>% 
  filter(name != "Alaska") %>% 
  filter(name != "Puerto Rico")

ContinentalUS <- st_transform(ContinentalUS, eqdc)
#st_crs(ContinentalUS)

#1.3

na = rnaturalearth::countries110 %>% 
  st_as_sf() %>% 
  filter(admin %in% c("Mexico", "Canada", "United States of America")) %>%   st_transform(eqdc)

#1.4
cities = read_csv("../data/uscities.csv") %>% 
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>% 
  st_transform(eqdc) %>% 
  st_filter(ContinentalUS)
## Parsed with column specification:
## cols(
##   city = col_character(),
##   city_ascii = col_character(),
##   state_id = col_character(),
##   state_name = col_character(),
##   county_fips = col_double(),
##   county_name = col_character(),
##   county_fips_all = col_character(),
##   county_name_all = col_character(),
##   lat = col_double(),
##   lng = col_double(),
##   population = col_double(),
##   density = col_double(),
##   source = col_character(),
##   military = col_logical(),
##   incorporated = col_logical(),
##   timezone = col_character(),
##   ranking = col_double(),
##   zips = col_character(),
##   id = col_double()
## )

#Question 2

#2.1
conus_string = st_union(ContinentalUS) %>% 
  st_cast("MULTILINESTRING")

na_string = st_combine(na) %>% 
  st_cast("MULTILINESTRING")


cities = cities %>% 
  mutate(dist_to_border = st_distance(cities, conus_string),
         dist_to_border = units::set_units(dist_to_border, "km"),
         dist_to_border = units::drop_units(dist_to_border))

furthest_from_border = cities %>% 
  slice_max(dist_to_border, n = 5) %>% 
  select(city, state_name, dist_to_border) %>% 
  st_drop_geometry()

kable(furthest_from_border, caption = "Furthest Cities From Border", col.names = c ("City", "State", "Distance"))
Furthest Cities From Border
City State Distance
Dresden Kansas 1012.317
Herndon Kansas 1007.750
Hill City Kansas 1005.147
Atwood Kansas 1004.734
Jennings Kansas 1003.646
#2.2
conus_string2 = st_combine(ContinentalUS) %>% 
  st_cast("MULTILINESTRING")

cities = cities %>% 
  mutate(dist_to_state = st_distance(cities, conus_string2),
         dist_to_state = units::set_units(dist_to_state, "km"),
         dist_to_state = units::drop_units(dist_to_state))

furthest_from_state = cities %>% 
  slice_max(dist_to_state, n = 5) %>% 
  select(city, state_name, dist_to_state) %>% 
  st_drop_geometry()

kable(furthest_from_state, caption = "Furthest Cities From State Border", col.names = c ("City", "State", "Distance"))
Furthest Cities From State Border
City State Distance
Lampasas Texas 308.9216
Bertram Texas 302.8190
Kempner Texas 302.5912
Harker Heights Texas 298.8125
Florence Texas 298.6804
#2.3
cities = cities %>% 
  mutate(dist_to_mex = st_distance(cities, filter(na, admin == "Mexico")),
         dist_to_mex = units::set_units(dist_to_mex, "km"),
         dist_to_mex = units::drop_units(dist_to_mex))


furthest_from_mex = cities %>% 
  slice_max(dist_to_mex, n = 5) %>% 
  select(city, state_name, dist_to_mex) %>% 
  st_drop_geometry()

kable(furthest_from_mex, caption = "Furthest Cities From Mexico", col.names = c ("City", "State", "Distance"))
Furthest Cities From Mexico
City State Distance
Caribou Maine 3250.334
Presque Isle Maine 3234.570
Calais Maine 3134.348
Eastport Maine 3125.624
Old Town Maine 3048.366
#2.4
cities = cities %>% 
  mutate(dist_to_can = st_distance(cities, filter(na, admin == "Canada")),
         dist_to_can = units::set_units(dist_to_can, "km"),
         dist_to_can = units::drop_units(dist_to_can))


furthest_from_can = cities %>% 
  slice_max(dist_to_can, n = 5) %>% 
  select(city, state_name, dist_to_can) %>% 
  st_drop_geometry()

kable(furthest_from_can, caption = "Furthest Cities From Canada", col.names = c ("City", "State", "Distance"))
Furthest Cities From Canada
City State Distance
Guadalupe Guerra Texas 2206.455
Sandoval Texas 2205.641
Fronton Texas 2204.784
Fronton Ranchettes Texas 2202.118
Evergreen Texas 2202.020

#Question 3

#3.1
top_ten = cities %>% 
  slice_max(population, n = 10)

ggplot() + 
  geom_sf(data = ContinentalUS) + 
  geom_sf(data = na_string) +
  geom_sf(data = top_ten) +
  geom_label_repel(
  data = top_ten,
  aes(label = city, geometry=geometry),
  stat = "sf_coordinates") +
  theme_linedraw()

#3.2
ggplot() +
  geom_sf(data = cities, size = .1, aes(color = dist_to_border)) +
  scale_color_gradient(low = "navy", high = "red") +
  geom_sf(data= slice_max(cities, dist_to_border, n =5), col = "black") +
  geom_label_repel(
    data = slice_max(cities, dist_to_border, n =5),
    aes(label = city, geometry = geometry),
    stat= "sf_coordinates") +
  theme_linedraw()

#3.3
ggplot() +
  geom_sf(data = cities, size = .1, aes(color = dist_to_state)) +
  scale_color_gradient(low = "navy", high = "red") +
  geom_sf(data= slice_max(cities, dist_to_state, n =5), col = "black") +
  geom_label_repel(
    data = slice_max(cities, dist_to_state, n =5),
    aes(label = city, geometry = geometry),
    stat= "sf_coordinates") +
  theme_linedraw()

#3.4
dist_mex_can = cities %>% 
  mutate(diff = abs(dist_to_mex - dist_to_can))

disttop5 = dist_mex_can %>% 
  filter(diff<=100) %>% 
  slice_max(population, n=5)

ggplot() +
  geom_sf(data = dist_mex_can, size = .1, aes(col = diff)) +
  scale_color_gradient(low = "navy", high = "red") +
  gghighlight(diff<=100) + 
  geom_sf(data = disttop5, col = "green") +
  geom_label_repel(
    data = disttop5,
    aes(label = city, geometry = geometry),
    stat= "sf_coordinates") +
  theme_linedraw()

#Question 4

#4.1
cities_100m = cities %>% 
  group_by(state_name) %>% 
  filter(dist_to_border <= 160.9) %>% 
  ungroup()

#There are 12,302 cities within the 100 mile zone

popcount = cities_100m %>% 
  st_drop_geometry() %>% 
  summarise(population) %>% 
  sum()

#There are 261547106 people in the zone

percent = popcount/328200000

#79.7% of the population are in this zone

#Does it match? #No, they claim 2/3 of the population are in these zones when it is actually 79%

#4.2
ggplot() + 
  geom_sf(data = cities, size = .2) + 
  scale_color_gradient(low = "orange", high = "dark red") +
  gghighlight(dist_to_border <= 160.9)