Packages

library(tidyverse)
library(sf)
library(janitor)
library(tidygeocoder)

Data

Access

Access data

Create a folder data/.

NC PMTW Streams:

download.file(paste0("https://opendata.arcgis.com/datasets/", 
                    "0cc135e6c6244c9e9646b45ee3cb6c1e_0.zip", 
                    "?outSR=%7B%22latestWkid%22%3A3857%2C%22",
                    "wkid%22%3A102100%7D"),
              destfile = "data/pmtw.zip")

unzip("data/pmtw.zip", exdir = "data/")
file.remove("data/pmtw.zip")

NC Public Schools:

download.file(paste0("https://opendata.arcgis.com/datasets/", 
                    "dea6ff0e8b4743a0ba361e13a85a4c70_3.zip", 
                    "?outSR=%7B%22latestWkid%22%3A32119%2C%22",
                    "wkid%22%3A32119%7D"),
              destfile = "data/schools.zip")

unzip("data/schools.zip", exdir = "data/")
file.remove("data/schools.zip")

Read

Read in the shapefiles

nc_counties <- st_read(system.file("shape/nc.shp", package = "sf"), 
                       quiet = T) %>% 
  clean_names() %>% 
  select(name, area)
nc_pmtw <- st_read("data/PMTW_Streams_2020.shp", quiet = T) %>%
  clean_names() %>% 
  select(name = displ_name, designation = first_wrc, length = shape_len)
nc_schools <- st_read("data/Public_Schools.shp", quiet = T) %>% 
  clean_names()

Align CRS

nc_counties <- st_transform(nc_counties, crs = st_crs(nc_schools))
nc_pmtw <- st_transform(nc_pmtw, crs = st_crs(nc_schools))
st_crs(nc_counties)[1]
#> $input
#> [1] "NAD83 / North Carolina"
st_crs(nc_pmtw)[1]
#> $input
#> [1] "NAD83 / North Carolina"
st_crs(nc_schools)[1]
#> $input
#> [1] "NAD83 / North Carolina"

Exercise 1

Problem

Use st_intersects() and st_contains() to plot all the public schools in Durham and the counties that neighbor Durham.