```{r setup}
library(tidyverse)
library(sf)
library(units)

library(USAboundaries)
library(rnaturalearth)

library(gghighlight)
library(ggrepel)
library(knitr)
```



```{r}
# 1.1 and 1.2

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'

USAboundaries::us_states(resolution = "low") 

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

```


```{r}
# 1.3

rnaturalearth::countries110 


```


```{r}

# 1.4

read_csv("../data/uscities.csv")


```



```{r}
df = data.frame(name = state.name, 
                X = state.center$x, 
                Y = state.center$y)
head(df)

(df_sf_gcs = st_as_sf(df, 
                      coords = c("X", "Y"), 
                      crs = 4269))

ggplot() + 
  geom_sf(data = df_sf_gcs) + 
  coord_sf(datum = st_crs(df_sf_gcs)) +
  theme_linedraw()

```

```{r}
# Question 2.1 

us_c_ml = st_combine(conus) %>%
  st_cast("MULTILINESTRING")
us_u_ml = st_union(conus) %>%
  st_cast("MULTILINESTRING")

# near5 = uscities %>%
#  select(state_name, city, lat, lng) %>%
#   mutate(dist = st_distance) %>%
#   st_cast("38.9034° N, 77.0457° W") %>%
#   slice_min(dist, n = 5) ->
# near5
  
# knitr::kable(near5, caption = "5 Cities Farthest from a State Border")

```



```{r}

# Question 2.2

us_c_ml = st_combine(conus) %>%
  st_cast("MULTILINESTRING")
us_u_ml = st_union(conus) %>%
  st_cast("MULTILINESTRING")

near5 = uscities %>%
  select(state_name, city, lat, lng) %>%
  st_cast("MULTILINESTRING") %>%
  mutate(dist = st_distance) %>%
  slice_min(dist, n = 5) ->
  near5 

knitr::kable(near5, caption = "5 Cities Farthest from a State Border")

```


```{r}
# Question 2.3, Distance to Mexico 

countries110 %>%
  filter(countries, country = "Mexico") %>%
  select(city, state_name, lat, lng) %>%
  mutate(dist = st_distance) %>%
  slice_min(dist, n = 5)
  near5
  
knitr::kable(near5, caption = "5 Cities Farthest from Mexico")
```

```{r}
# Question 2.4, Distance to Canada

countries110 %>%
  filter(countries, country = "Canada") %>%
  select(city, state_name, lat, lng) %>%
  mutate(dist = st_distance) %>%
  slice_min(dist, n = 5)
  near5
  
knitr::kable(near5, caption = "5 Cities Farthest from Canada")
```

```{r}
# Question 3.1 

read_csv("uscities.csv")

sub_cities = cities %>% 
  filter(name %in% us_boundaries()) %>% 
  slice_max(population, n = 10) %>% 

ggplot() + 
  geom_sf(data = df_sf_gcs) + 
  coord_sf(datum = st_crs(df_sf_gcs)) +
  theme_linedraw()
  ggrepel::geom_label_repel


```
```{r}
# Question 3.2

cities = read_csv("data/uscities.csv") %>%
  st_as_sf(coords = c("lng", "lat"), crs = 5070) %>% 
  get_conus("state_name") %>% 
  select(city, state)
  mutate(dist = st_distance) %>%
  slice_max(dist, n = 5)
  
ggplot() 
  geom_sf(data = uscities, aes(fill = aland), lty = 3, alpha = .5) + 
  gghighlight(aland > 5^10) +
  geom_sf(data = state, size = 0.5, fill = "light_blue") +

ggrepel::geom_label_repel
  data = uscities %>%
  ggthemes::theme_map() 
```

```{r}
# Question 3.3

cities = read_csv("data/uscities.csv") %>%
  st_as_sf(coords = c("lng", "lat"), crs = 5070) %>% 
  get_conus("state_name") %>% 
  select(city, state)
  mutate(dist = st_distance) %>%
  slice_min(dist, n = 5)
  near5

ggplot() 
  geom_sf(data = uscities, aes(fill = aland), lty = 3, alpha = .5) + 
  gghighlight(aland > 5^10) +
  geom_sf(data = state, size = 0.5, fill = "light_blue") +

ggrepel::geom_label_repel
  data = us_cities()
  aes(label = city, geometry = geometry),
  stat = "sf_coordinates",
  size = 3) +
  labs(fill = "Area Land") + 
  ggthemes::theme_map() 

```
```{r}
library(sf)

# Question 4.1

USAboundaries::us_states(resolution = "low") 

conus = USAboundaries::us_states() %>%
  filter(!state_name %in% c("Puerto Rico", "Alaska", "Hawaii"))
  select(state.name, state.region, population)
  mutate(distance100 = st_distance(par = 100))

knitr::kable(distance100, caption = "Populations of Cities within 100 miles of a border")


# Question 4.2

gghighlight(states) +
  geom(sf) +
  st_filter(cities, sb, .predicate = st_is_within_distance, 100)
  ggplot(states) + 
  geom_sf(data = districts, aes(fill = aland), lty = 3, alpha = .5) + 
  scale_fill_gradient(low = 'orange', high = "darkred") +
  labs(fill = "States within protected zone") + 
  ggthemes::theme_map() 

 
  


```












