library(tidyverse)
library(sf)
library(units)
library(ggplot2)
library(ggrepel)
library(gghighlight)
library(ggthemes)
library(knitr)
region = data.frame(region = state.region, 
                    state_name = state.name)

south = right_join(USAboundaries::us_states(),
                   region,
                   by = "state_name") %>% 
  filter(region == "South")
plot(south['aland'])

cities = readr::read_csv("~/github/geog-176A-labs/data/uscities.csv") %>% 
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>% 
  st_filter(south, .predicate = st_intersects)

plot(south$geometry)
plot(cities, add= TRUE, pch = 16, cex = .1)

south_c = st_combine(south) %>% 
  st_cast("MULTILINESTRING")
south_c = st_transform(south_c, 5070)
cities = st_transform(cities, 5070)
cities = cities %>% 
  mutate(dist_to_state = st_distance(cities, south_c),
         dist_to_state = units::set_units(dist_to_state, "km"),
         dist_to_state = units::drop_units(dist_to_state))
big_cities = cities %>%
  group_by(state_name) %>%
  slice_max(population, n = 2)



ggplot() +
  geom_sf(data = south_c) +
  geom_sf(data = cities, aes(col = dist_to_state), size = .1) +
  geom_sf(data = big_cities, col = "navy") +
  scale_color_gradient(low = "gray", high = "red") +
  ggthemes::theme_map() +
  ggrepel::geom_label_repel(
    data = big_cities,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 4
  ) +
  labs(title = "Labeling Example",
       col = "Distance (km)")

ggplot() +
  geom_sf(data = south_c) +
  geom_sf(data = cities, aes(col = dist_to_state), size = .1) +
  gghighlight::gghighlight(population > 10000) +
  geom_sf(data = big_cities, col = "navy") +
  scale_color_gradient(low = "gray", high = "red") +
  ggthemes::theme_map() +
  labs(title = "Highlighting Example",
       col = "Distance (km)")

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

###1.2

library(USAboundaries) 

region = data.frame(region = state.region, 
                    state_name = state.name)


USA_states = right_join(USAboundaries::us_states(),
                   region,
                   by = "state_name") %>% 
  filter(!state_abbr %in% c("PR", "AK", "HI")) 

USA_states <- st_transform(USA_states, '+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')
  
plot(USA_states$geometry, main = "Map of Continental United States ")

#1.3

library(rnaturalearth)

boundaries = rnaturalearth::countries110 %>% 
  st_as_sf(crs = 4269) %>% 
  filter(SOVEREIGNT %in% c ("United States of America", "Canada", "Mexico")) %>% 
  st_transform(eqdc)

plot(boundaries$geometry)

#1.4

library(readr)
cities = readr::read_csv("~/github/geog-176A-labs/data/uscities.csv") %>% 
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>% 
  filter(!state_name %in% c("Hawaii", "Alaska", "Puerto Rico")) %>%
  st_transform(eqdc)

#Quesrion 2: #2.1

USA_states_string = st_union(USA_states) %>% 
  st_cast("MULTILINESTRING")

cities = cities %>% 
  mutate(dist2border = st_distance(cities, USA_states_string),
         dist2border = units::set_units(dist2border, "km"),
         dist2border = units::drop_units(dist2border))

furthest_cities = cities %>%
  slice_max(dist2border, n = 5) %>% 
  select(city, state_name, dist2border) %>% 
  st_drop_geometry()

kable(furthest_cities, caption = "The Furthest Cities From the American Boarders",
      col.names = c("City", "State", "Distance to Border in km"),
      format.args = list(big.mark = ","))
The Furthest Cities From the American Boarders
City State Distance to Border in km
Dresden Kansas 1,012.317
Herndon Kansas 1,007.750
Hill City Kansas 1,005.147
Atwood Kansas 1,004.734
Jennings Kansas 1,003.646

#2.2

USA_states_string2 = st_combine(USA_states) %>% 
  st_cast("MULTILINESTRING")

cities = cities %>% 
  mutate(dist2state = st_distance(cities, USA_states_string2),
         dist2state = units::set_units(dist2state, "km"),
         dist2state = units::drop_units(dist2state))

furthest_cities_from_state = cities %>% 
  slice_max(dist2state, n = 5) %>% 
  select(city, state_name, dist2state) %>% 
  st_drop_geometry()

kable(furthest_cities_from_state, caption = "The Furthest Cities From State Border",
      col.names = c("City", "State", "Distance to State Border in km"),
      format.args = list(big.mark = ","))
The Furthest Cities From State Border
City State Distance to State Border in km
Lampasas Texas 308.9216
Bertram Texas 302.8190
Kempner Texas 302.5912
Harker Heights Texas 298.8125
Florence Texas 298.6804

#2.3

Mexico_border = boundaries %>% 
filter(SOVEREIGNT == "Mexico")

cities = cities %>% 
  mutate(dist2Mexico = st_distance(cities, Mexico_border),
         dist2Mexico = units::set_units(dist2Mexico, "km"),
         dist2Mexico = units::drop_units(dist2Mexico))

furthest_cities_from_Mexico = cities %>% 
  slice_max(dist2Mexico, n = 5) %>% 
  select(city, state_name, dist2Mexico) %>% 
  st_drop_geometry()

kable(furthest_cities_from_Mexico, caption = "Furthest Cities From Mexico Border",
      col.names = c("City", "State", "Distance to Mexico Border in km"),
      format.args = list(big.mark = ","))
Furthest Cities From Mexico Border
City State Distance to Mexico Border in km
Caribou Maine 3,250.334
Presque Isle Maine 3,234.570
Calais Maine 3,134.348
Eastport Maine 3,125.624
Old Town Maine 3,048.366

#2.4

canada_border = boundaries %>% 
filter(SOVEREIGNT == "Canada")

cities = cities %>% 
  mutate(dist2Canada = st_distance(cities, canada_border),
         dist2Canada = units::set_units(dist2Canada, "km"),
         dist2Canada = units::drop_units(dist2Canada))

furthest_cities_from_Canada = cities %>% 
  slice_max(dist2Canada, n = 5) %>% 
  select(city, state_name, dist2Canada) %>% 
  st_drop_geometry()

kable(furthest_cities_from_Canada, caption = "Furthest Cities From Mexico Border",
      col.names = c("City", "State", "Distance to Mexico Border in km"),
      format.args = list(big.mark = ","))
Furthest Cities From Mexico Border
City State Distance to Mexico Border in km
Guadalupe Guerra Texas 2,206.455
Sandoval Texas 2,205.641
Fronton Texas 2,204.784
Fronton Ranchettes Texas 2,202.118
Evergreen Texas 2,202.020

#Question 3 #3.1

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

ggplot()+
  geom_sf(data = boundaries)+
  geom_sf(data = USA_states_string2)+
  geom_sf(data = big_cities, col = "red", size = 1.5)+
  ggthemes::theme_map()+
  labs(title = "The 10 Most Populated Cities in the US")+
  ggrepel::geom_label_repel(
    data = big_cities,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 3
  )

#3.2

furthest_cities2 = cities %>% 
  slice_max(dist2border, n = 5) %>% 
  select(city, state_name, dist2border)

ggplot()+
  geom_sf(data = cities, aes(col = dist2border), size = .5)+
  geom_sf(data = furthest_cities2, col = "blue")+
  geom_sf(data = USA_states_string)+
  scale_color_gradient(low = "white", high = "red")+
  ggthemes::theme_map()+
  labs(title = "5 Furthest Cities From Country Borders and Distance From Boarder")+
  ggrepel::geom_label_repel(
    data = furthest_cities2,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 3
  )

#3.3

furthest_cities3 = cities %>% 
  slice_max(dist2state, n = 5) %>% 
  select(city, state_name, dist2state)

ggplot()+
  geom_sf(data = cities, aes(col = dist2state), size = .5)+
  geom_sf(data = furthest_cities3, col = "blue")+
  geom_sf(data = USA_states_string2)+
  scale_color_gradient(low = "white", high = "red")+
  ggthemes::theme_map()+
  labs(title = "5 Furthest Cities From State Borders and Distance From Boarder")+
  ggrepel::geom_label_repel(
    data = furthest_cities3,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 3
  )

#3.4

cities = cities %>% 
  mutate(distance_Can_Mex = abs(dist2Canada - dist2Mexico))

equa_Can_Mex = cities %>% 
  filter(distance_Can_Mex < 100)

biggest_equa_Can_Mex = equa_Can_Mex %>% 
  slice_max(population, n = 5)

ggplot()+
  geom_sf(data = boundaries)+
  geom_sf(data = USA_states_string)+
  geom_sf(data = equa_Can_Mex, color = "green", size = .5)+
  geom_sf(data = biggest_equa_Can_Mex, color = "red", size = .5)+
  ggthemes::theme_map()+
  labs(title = "Cities Equadistant from Canada and Mexico Borders")+
  ggrepel::geom_label_repel(
    data = biggest_equa_Can_Mex,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 3
  )

#Question 4 #4.1

Border_zone = cities %>% 
  mutate(Mex_cities = dist2Mexico < 160) %>% 
  mutate(Can_cities = dist2Canada < 160) %>%
  filter(!Mex_cities %in% "FALSE" | !Can_cities %in% "FALSE")

#According to the data. there are 3296 cities in the boarder zone. 

sum(Border_zone$population)
## [1] 47082122
#According to the sum function for Border_zone, the population in the border zone is 47,082,122. This however may be skewed becauset the website says there shoul be 200 million people in the border zone. 

sum(cities$population)
## [1] 397213686
#According to the sum function for the cities data frame, there are 397,216,686 people in the United States. This means. That the ratio of people in the border zone to people in the US is as follows:

sum(Border_zone$population) / sum(cities$population)
## [1] 0.118531
#4.2
ggplot()+
  geom_sf(data = cities, aes(col = dist2border), size = .5)+
  geom_sf(data = Border_zone, col = "blue")+
  geom_sf(data = USA_states_string2)+
  scale_color_gradient(low = "white", high = "red")+
  ggthemes::theme_map()+
  labs(title = "5 Furthest Cities From State Borders and Distance From Boarder")+
  ggrepel::geom_label_repel(
    data = furthest_cities3,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 3
  )