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'
CONUS= USAboundaries::us_states(resolution = 'low') %>% 
  filter(!name %in% c("Puerto Rico", "Alaska", "Hawaii", "District of Columbia"))
CONUS<- st_transform(CONUS, eqdc)
st_crs(CONUS)
## Coordinate Reference System:
##   User input: +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 
##   wkt:
## PROJCRS["unknown",
##     BASEGEOGCRS["unknown",
##         DATUM["North American Datum 1983",
##             ELLIPSOID["GRS 1980",6378137,298.257222101,
##                 LENGTHUNIT["metre",1]],
##             ID["EPSG",6269]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8901]]],
##     CONVERSION["unknown",
##         METHOD["Equidistant Conic"],
##         PARAMETER["Latitude of natural origin",40,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",-96,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["Latitude of 1st standard parallel",20,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8823]],
##         PARAMETER["Latitude of 2nd standard parallel",60,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8824]],
##         PARAMETER["False easting",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["(E)",east,
##             ORDER[1],
##             LENGTHUNIT["metre",1,
##                 ID["EPSG",9001]]],
##         AXIS["(N)",north,
##             ORDER[2],
##             LENGTHUNIT["metre",1,
##                 ID["EPSG",9001]]]]
plot(CONUS$geometry)

MEX= rnaturalearth::countries110 %>% 
  st_as_sf() %>% 
  filter(admin== 'Mexico')
plot(MEX$geometry)

CAN= rnaturalearth::countries110 %>% 
  st_as_sf() %>% 
  filter(admin== 'Canada')
plot(CAN$geometry)

QUESTION #1

1.2 Get US State boundaries

region = data.frame(region = state.region, 
                    state_name = state.name)
USA = right_join(USAboundaries::us_states(),
                   region,
                   by = "state_name") %>% 
  filter(!state_abbr %in% c("HI", "PR", "AK")) 

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

1.3 - Get country boundaries for Mexico, the United States of America, and Canada

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 - Get city locations

cities = readr::read_csv('/Users/lfinn443/github/geog-176A-labs/data/uscities.csv')%>%   #read in raw data
  st_as_sf(coords = c("lng", "lat"), crs = 4326) %>%  
  st_transform(eqdc) %>% 
  st_filter(CONUS)

QUESTION 2.1

USA_u = st_union(USA) %>%
  st_cast("MULTILINESTRING") %>% 
  st_transform(st_crs(cities))
plot(USA_u)

#DISTANCE TO US BORDER
cities = cities %>% 
  mutate(dist_to_border = st_distance(cities, USA_u),
         dist_to_border = units::set_units(dist_to_border, "km"),
         dist_to_border = units::drop_units(dist_to_border))

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

kable(farthest_cities, caption = "Furthest US Cities from National Border",
      col.names = c("City", "State", "Nearest US Border/Coastline: km"),
      format.args = list(big.mark = ","))
Furthest US Cities from National Border
City State Nearest US Border/Coastline: 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

QUESTION 2.2

USA_c=st_combine(USA)  %>%
  st_cast("MULTILINESTRING") %>% 
  st_transform(st_crs(cities))
plot(USA_c)

#Distance to States
cities = cities %>% 
  mutate(dist_to_state = st_distance(cities, USA_c),
         dist_to_state= units::set_units(dist_to_state, "km"),
         dist_to_state = units::drop_units(dist_to_state))
cities_far_state=cities %>%
  slice_max(dist_to_state, n = 5) %>% 
  select(city, state_name, dist_to_state) %>% 
  st_drop_geometry()

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

QUESTION 2.3 - Distance to Mexico (km)

MEX= boundaries %>% 
  filter(sovereignt == "Mexico") %>% 
  st_transform(st_crs(cities))

cities = cities %>% 
  mutate(MEX_dist = st_distance(cities, MEX),
         MEX_dist = units::set_units(MEX_dist, "km"),
         MEX_dist = units::drop_units(MEX_dist))

#furthest US Cities from MEX
 UScities_MEX= cities %>% 
  slice_max(MEX_dist, n = 5) %>% 
  select(city, state_name, MEX_dist) %>% 
  st_drop_geometry()

kable(UScities_MEX, caption = "Furthest US Cities From Mexican Border",
      col.names = c("City", "State", "Distance to Mexico Border: km"),
      format.args = list(big.mark = ","))
Furthest US Cities From Mexican Border
City State Distance to Mexico Border: 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 - Distance to Canada (km)

CAN=boundaries %>% 
  filter(sovereignt=='Canada')

cities = cities %>% 
  mutate(CAN_dist = st_distance(cities, CAN),
         CAN_dist = units::set_units(CAN_dist, "km"),
         CAN_dist = units::drop_units(CAN_dist))

#furthest US cities from CAN
UScities_CAN=cities %>% 
  slice_max(CAN_dist, n=5) %>% 
  select(city,state_name, CAN_dist) %>% 
  st_drop_geometry()

kable(UScities_CAN, caption = "Furthest US Cities From Canadian Border",
      col.names = c("City", "State", "Distance to Canada Border: km"),
      format.args = list(big.mark = ","))
Furthest US Cities From Canadian Border
City State Distance to Canada Border: 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

3.1 Data

#10 most populated cities in US
big_cities = cities %>%
  slice_max(population, n = 10)

ggplot()+
  geom_sf(data = boundaries)+
  geom_sf(data = USA_c)+
  geom_sf(data = big_cities, col = "red", size = 1)+
  ggthemes::theme_map()+
  labs(title = "10 Most Populated US Cities")+
  ggrepel::geom_label_repel(
    data = big_cities,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 4
  )

3.2 City Distance from the Border

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

ggplot()+
  geom_sf(data = cities, aes(col = dist_to_border), size = 1)+
  geom_sf(data = farthest_2, col = "darkblue")+
  geom_sf(data = USA_u)+
  scale_color_gradient(low = "grey", high = "red")+
  ggthemes::theme_map()+
  labs(title = "Furthest US Cities from Domestic Border")+
  ggrepel::geom_label_repel(
    data = farthest_2,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 4
  )

3.3 City Distance from Nearest State

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

ggplot()+
  geom_sf(data = cities, aes(col = dist_to_state), size = 1)+
  geom_sf(data = farthest_3, col = "darkblue")+
  geom_sf(data = USA_c)+
  scale_color_gradient(low = "white", high = "red")+
  ggthemes::theme_map()+
  labs(title = "City Distance from Nearest State")+
  ggrepel::geom_label_repel(
    data = farthest_3,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 4
  )

#3.4 Equidistance boundary from Mexico and Canada
cities = cities %>% 
  mutate(Can_Mex = abs(CAN_dist - MEX_dist))

equid_Can_Mex = cities %>% 
  filter(Can_Mex < 100)

max_equid_Can_Mex = equid_Can_Mex %>% 
  slice_max(population, n = 5)

ggplot()+
  geom_sf(data = boundaries)+
  geom_sf(data = USA_u)+
  geom_sf(data = equid_Can_Mex, color = "lightgreen", size = .5)+
  geom_sf(data = max_equid_Can_Mex, color = "red", size = .5)+
  ggthemes::theme_map()+
  labs(title = "Cities = distance from CAN&MEX border ± 100 km")+
  ggrepel::geom_label_repel(
    data = max_equid_Can_Mex,
    aes(label = city, geometry = geometry),
    stat = "sf_coordinates",
    size = 4
  )

#Question 4 
border_zone=cities %>% 
  filter(dist_to_border <= 160)

There are 3,296 cities are within the 100 mile border zone, or “external boundary”.

Border Zone/“External Boundary” Population is: 47,082,122

sum(border_zone$population)
## [1] 254658082

The % of the total population that lives in this zone is %12

sum(border_zone$population)/sum(cities$population)
## [1] 0.6498619