```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(swirlify)
swirlify("Vector_Operation_Basics", "EDS_223_Spatial_Statistics")

```

## introduction

The following exercises are modified from [Chapters 3](https://geocompr.robinlovelace.net/attr.html), [4](https://geocompr.robinlovelace.net/spatial-operations.html), [5](https://geocompr.robinlovelace.net/geometry-operations.html) of Geocomputation with R by Rovin Lovelace.


## prerequisites

```{r load, include=TRUE, message=FALSE, warning=FALSE}
# add required packages here

library(sf)
library(spData)
library(tidyverse)

us_states = us_states %>% 
  janitor::clean_names()
us_states_df = us_states_df %>% 
  janitor::clean_names()
nz_height = nz_height %>%  
  janitor::clean_names()
nz = nz %>%  
  janitor::clean_names()
```

Using US States 

Find the states that: 

-   belong to the West region  

```{r}
west_states = filter(us_states, region == "West")

pull(west_states, name)
```

-   and greater than 5,000,000 residents in 2015

```{r}
us_states %>% 
  filter(total_pop_15 > 5000000) %>% 
  pull(name)
```

What was the total population of the US in 2015? 

```{r include=TRUE}
us_states %>% 
  summarise(pop_2015 = sum(total_pop_15, na.rm = TRUE)) %>% 
  pull(pop_2015)
```

Create a new variable named "us_states_stats" by adding variables from "us_states_df" to "us_states". 

```{r}
class(us_states_df)
names(us_states_df)

us_states_df = us_states_df %>% 
  rename(name = state)

us_states_stats = full_join(us_states, us_states_df)
```

-   What is the class of the new object? 

"sf"         "data.frame"


Calculate the percent change in population density between 2010 and 2015 in each state. 

```{r include=TRUE}
pop_change = mutate(us_states_stats, percent_change_pop = (total_pop_15 - total_pop_10)/total_pop_10 * 100)
```

In how many states did population density decrease? 

```{r include=TRUE}
pop_change %>% 
  filter(percent_change_pop < 0) %>% 
  nrow()
```

How many of New Zealand's high points are in the Canterbury region?

```{r include=TRUE}
names(nz)
names(nz_height)

canterbury = nz  %>% 
  filter(name == "Canterbury")

canterbury_height = nz_height[canterbury, ]
nrow(canterbury_height)
```

This could also be achieved by spatial joining then filter

```{r}
st_join(nz, nz_height) %>% 
  filter(name == "Canterbury") %>% 
  nrow()
```

Which region has the second highest number of "nz_height" points? And how many does it have? 

```{r include=TRUE}
nz_join = st_join(nz, nz_height) 

nz_join %>% 
  group_by(name) %>% 
  summarise(n = n()) %>% 
  arrange(desc(n))
```

Create a new object representing all of the states the geographically intersect with Colorado.

Hint: use the "us_states" dataset. The most concise way to do this is with the subsetting method "[".

```{r}
colorado = us_states %>% 
  filter(name == "Colorado")

colorado_intersect = us_states[colorado, , op = st_intersects]
```

Create another object representing all the objects that touch (have a shared boundary with) Colorado and plot the result.

Hint: remember you can use the argument op = st_intersects and other spatial relations during spatial subsetting operations in base R.

```{r}
colorado_touches = us_states[colorado, , op = st_touches]
```

```{r}
plot(colorado_intersect["name"])

plot(colorado_touches["name"])
```

Generate simplified versions of the "nz" dataset. Experiment with different values of keep (ranging from 0.5 to 0.00005) for **ms_simplify()** and dTolerance (from 100 to 100,000) for **st_simplify()**.

```{r}
nz_simp1 = st_simplify(nz, dTolerance = 100)
nz_simp4 = st_simplify(nz, dTolerance = 100000)
nz_simp2 = st_simplify(nz, dTolerance = 1000)
nz_simp3 = st_simplify(nz, dTolerance = 10000)
```


Map the results to show how the simplification changes as you change values.

```{r include=TRUE}
plot(nz_simp1["name"])
plot(nz_simp2["name"])
plot(nz_simp3["name"])
plot(nz_simp4["name"])
```

How many points from the "nz_height" dataset are within 100km of the Canterbury region?

```{r include=TRUE}
cant_buffer = st_buffer(canterbury, dist = 100000)

cant_buffer_height = nz_height[cant_buffer, ]
nrow(cant_buffer_height)
```

Find the geographic centroid of the country of New Zealand. How far is it from the geographic centroid of Canterbury?

```{r include=TRUE}
nz_agg = nz %>% 
  summarize(population = sum(population))

nz_centroid = st_centroid(nz_agg)

cant_centroid = st_centroid(canterbury) 

st_distance(nz_centroid, cant_centroid)
```

