---
title: "Spatial Data"
output:
  html_notebook: default
  pdf_document: default
  word_document: default
---

The new version of the book is now available [mdsr2e](https://beanumber.github.io/mdsr2e/).  The new chapter [Chapter 17 Working with geospatial data](https://beanumber.github.io/mdsr2e/ch-spatial.html) used the new R package [sf](https://r-spatial.github.io/sf/) and could be further updated to the the new R package [cholera](https://github.com/lindbrook/cholera).


```{r}
library(tidyverse)
library(mdsr)
library(sf)
plot(CholeraDeaths["Count"])
```

It is probably best to do this in an R Project.

```{r}
download.file("http://rtwilson.com/downloads/SnowGIS_SHP.zip",
              dest="SnowGIS.zip", mode="wb")
unzip("SnowGIS.zip")

dsn <- fs::path("SnowGIS_SHP")
list.files(dsn)
```

```{r}
st_layers(dsn)
```



```{r}
CholeraDeaths <- st_read(dsn, layer = "Cholera_Deaths")
```

```{r}
class(CholeraDeaths)
```

```{r}
CholeraDeaths
```


```{r}
summary(CholeraDeaths)
```

```{r}
ggplot(CholeraDeaths) +
  geom_sf()
```

In the new edition of the book the authors use Open Street Maps instead of Google Maps API.

```{r}
library(ggspatial)

ggplot(CholeraDeaths) + 
  annotation_map_tile(type = "osm", zoomin = 0) + 
  geom_sf(aes(size = Count), alpha = 0.7)
```

```{r}
st_bbox(CholeraDeaths)
```

# Projections

```{r}
library(mapproj)
library(maps)

map("world", projection = "mercator", wrap = TRUE)
map("world", projection = "cylequalarea", param = 45, wrap = TRUE)
```


```{r}
map(
  "state", projection = "lambert", 
  parameters = c(lat0 = 20, lat1 = 50), wrap = TRUE
)
map(
  "state", projection = "albers", 
  parameters = c(lat0 = 20, lat1 = 50), wrap = TRUE
)
```


```{r}
st_crs(CholeraDeaths)
```

```{r}
st_crs(4326)$epsg
```

```{r}
st_crs(3857)
```

```{r}
st_crs(27700)$proj4string
```

```{r}
cholera_4326 <- CholeraDeaths %>% 
  st_transform(4326)
```

```{r}
st_bbox(cholera_4326)
```

```{r}
ggplot(cholera_4326) + 
  annotation_map_tile(type = "osm", zoomin = 0) + 
  geom_sf(aes(size = Count), alpha = 0.7)
```

```{r}
help("spTransform-methods", package = "rgdal")
```

```{r}
st_crs(CholeraDeaths)$proj4string
```

```{r}
cholera_latlong <- CholeraDeaths %>%
  st_set_crs(27700) %>%
  st_transform(4326)

snow <- ggplot(cholera_latlong) + 
  annotation_map_tile(type = "osm", zoomin = 0) + 
  geom_sf(aes(size = Count))
```

```{r}
pumps <- st_read(dsn, layer = "Pumps")
```

```{r}
pumps_latlong <- pumps %>% 
  st_set_crs(27700) %>%
  st_transform(4326)
snow +
  geom_sf(data = pumps_latlong, size = 3, color = "red")
```

# Leaflet

```{r}
library(tidygeocoder)
white_house <- tibble(
  address = "The White House, Washington, DC"
) %>%
  tidygeocoder::geocode(address, method = "osm")

library(leaflet)
white_house_map <- leaflet() %>% 
  addTiles() %>%
  addMarkers(data = white_house)
white_house_map 
```

```{r}
white_house <- white_house %>%
  mutate(
    title = "The White House", 
    street_address = "1600 Pennsylvania Ave"
  )
white_house_map %>% 
  addPopups(
    data = white_house, 
    popup = ~paste0("<b>", title, "</b></br>", street_address)
  )
```


