Code
library(tidyverse)
library(sf)
library(terra)
Carolyn Koehn
Load libraries:
Load data:
ID WHP_ID
1 1 1952.8750
2 2 0.0000
3 3 741.4531
4 4 200.2812
5 5 0.0000
6 6 150.5938
We loaded the wildfire hazard data and the cejst data earlier in the example.
# custom function to download and load Forest Service data
download_unzip_read <- function(link){
tmp <- tempfile()
download.file(link, tmp)
tmp2 <- tempfile()
unzip(zipfile=tmp, exdir=tmp2)
shapefile.sf <- read_sf(tmp2)
}
### FS Boundaries
fs.url <- "https://data.fs.usda.gov/geodata/edw/edw_resources/shp/S_USA.AdministrativeForest.zip"
fs.bdry <- download_unzip_read(link = fs.url)
### CFLRP Data
cflrp.url <- "https://data.fs.usda.gov/geodata/edw/edw_resources/shp/S_USA.CFLR_HPRP_ProjectBoundary.zip"
cflrp.bdry <- download_unzip_read(link = cflrp.url)
Coordinate Reference System:
User input: NAD83
wkt:
GEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]]
Coordinate Reference System:
User input: unnamed
wkt:
PROJCRS["unnamed",
BASEGEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101004,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]],
CONVERSION["Albers Equal Area",
METHOD["Albers Equal Area",
ID["EPSG",9822]],
PARAMETER["Latitude of false origin",23,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8821]],
PARAMETER["Longitude of false origin",-96,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8822]],
PARAMETER["Latitude of 1st standard parallel",29.5,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8823]],
PARAMETER["Latitude of 2nd standard parallel",45.5,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8824]],
PARAMETER["Easting at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8826]],
PARAMETER["Northing at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8827]]],
CS[Cartesian,2],
AXIS["easting",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["northing",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]]
Coordinate Reference System:
User input: NAD83
wkt:
GEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]]
Coordinate Reference System:
User input: PROJCRS["unnamed",
BASEGEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101004,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]],
CONVERSION["Albers Equal Area",
METHOD["Albers Equal Area",
ID["EPSG",9822]],
PARAMETER["Latitude of false origin",23,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8821]],
PARAMETER["Longitude of false origin",-96,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8822]],
PARAMETER["Latitude of 1st standard parallel",29.5,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8823]],
PARAMETER["Latitude of 2nd standard parallel",45.5,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8824]],
PARAMETER["Easting at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8826]],
PARAMETER["Northing at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8827]]],
CS[Cartesian,2],
AXIS["easting",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["northing",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]]
wkt:
PROJCRS["unnamed",
BASEGEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101004,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]],
CONVERSION["Albers Equal Area",
METHOD["Albers Equal Area",
ID["EPSG",9822]],
PARAMETER["Latitude of false origin",23,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8821]],
PARAMETER["Longitude of false origin",-96,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8822]],
PARAMETER["Latitude of 1st standard parallel",29.5,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8823]],
PARAMETER["Latitude of 2nd standard parallel",45.5,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8824]],
PARAMETER["Easting at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8826]],
PARAMETER["Northing at false origin",0,
LENGTHUNIT["metre",1],
ID["EPSG",8827]]],
CS[Cartesian,2],
AXIS["easting",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["northing",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]]
[1] TRUE
[1] FALSE
cejst is our study area (the Pacific Northwest) to subset to.
Simple feature collection with 6 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -1598729 ymin: 2388182 xmax: -1475201 ymax: 3000813
Projected CRS: unnamed
GEOID10 LMI_PFS LHE HBF_PFS geometry
2 16025970100 0.75 0 0.60 MULTIPOLYGON (((-1485848 24...
17 16057005500 0.43 0 0.44 MULTIPOLYGON (((-1567845 28...
21 16057005600 0.30 0 0.05 MULTIPOLYGON (((-1573408 28...
22 16009950100 0.55 1 0.40 MULTIPOLYGON (((-1566013 28...
23 16017950500 0.75 1 0.53 MULTIPOLYGON (((-1545064 29...
24 16017950400 0.60 1 0.68 MULTIPOLYGON (((-1544958 29...
ID WHP_ID
1 1 2997.7951
2 2 182.8864
3 3 386.9580
4 4 173.1703
5 5 193.4199
6 6 210.4406
Many comparisons are possible! Here are some examples.
---
title: "Session 16 code"
author: "Carolyn Koehn"
format: html
---
Load libraries:
```{r}
#| message: false
#| warning: false
library(tidyverse)
library(sf)
library(terra)
```
Load data:
```{r}
#| eval: false
hospitals_pnw <- read_csv("/opt/data/data/assignment06/landmarks_pnw.csv") %>%
filter(., MTFCC == "K2543") %>%
st_as_sf(., coords = c("longitude", "latitude"), crs=4269) %>%
st_transform(crs = 5070)
```
```{r}
#| include: false
hospitals_pnw <- read_csv("C:/Users/carolynkoehn/Documents/HES505_Fall_2024/data/2023/assignment06/landmarks_pnw.csv") %>%
filter(., MTFCC == "K2543") %>%
st_as_sf(., coords = c("longitude", "latitude"), crs=4269) %>%
st_transform(crs = 5070)
```
## Rasterize
```{r}
raster_template = rast(ext(hospitals_pnw), resolution = 10000,
crs = st_crs(hospitals_pnw)$wkt)
hosp_raster1 = rasterize(hospitals_pnw, raster_template,
field = 1)
plot(hosp_raster1, colNA = "navy")
```
```{r}
# add dummy numeric data
hospitals_pnw$rand_capacity <- rnorm(n = nrow(hospitals_pnw),
mean = 5000,
sd = 2000)
hosp_raster3 = rasterize(hospitals_pnw, raster_template,
field = "rand_capacity", fun = sum)
plot(hosp_raster3)
```
## Raster to Vector
```{r}
dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))
cl = as.contour(dem)
```
## Creating New Data
### Distance
```{r}
raster_template = rast(ext(hospitals_pnw), resolution = 1000,
crs = st_crs(hospitals_pnw)$wkt)
hosp_raster1 = rasterize(hospitals_pnw, raster_template,
field = 1)
hosp_dist_rast <- distance(hosp_raster1)
plot(hosp_dist_rast)
```
### Attributes
```{r}
#| eval: false
wildfire_haz <- rast("/opt/data/data/assignment07/wildfire_hazard_agg.tif")
```
```{r}
#| include: false
wildfire_haz <- rast("C:/Users/carolynkoehn/Documents/HES505_Fall_2024/data/2023/assignment07/wildfire_hazard_agg.tif")
```
```{r}
hospitals_pnw_proj <- st_transform(hospitals_pnw, crs(wildfire_haz))
hosp_fire_haz <- terra::extract(wildfire_haz, hospitals_pnw_proj)
head(hosp_fire_haz)
hospitals_pnw_proj$wildfire <- hosp_fire_haz$WHP_ID
```
```{r}
#| eval: false
cejst <- st_read("/opt/data/data/assignment06/cejst_pnw.shp") %>%
st_transform(crs = crs(wildfire_haz)) %>%
filter(!st_is_empty(.))
```
```{r}
#| include: false
cejst <- st_read("C:/Users/carolynkoehn/Documents/HES505_Fall_2024/data/2023/assignment06/cejst_pnw.shp") %>%
st_transform(crs = crs(wildfire_haz)) %>%
filter(!st_is_empty(.))
```
```{r}
wildfire.zones <- terra::zonal(wildfire_haz, vect(cejst), fun="mean", na.rm=TRUE)
head(wildfire.zones)
```
## Practice
### Step 1: Load libraries
```{r}
#| message: false
#| warning: false
library(sf)
library(terra)
library(tidyverse)
library(tmap)
```
### Step 2: Load in data
We loaded the wildfire hazard data and the cejst data earlier in the example.
```{r}
# custom function to download and load Forest Service data
download_unzip_read <- function(link){
tmp <- tempfile()
download.file(link, tmp)
tmp2 <- tempfile()
unzip(zipfile=tmp, exdir=tmp2)
shapefile.sf <- read_sf(tmp2)
}
### FS Boundaries
fs.url <- "https://data.fs.usda.gov/geodata/edw/edw_resources/shp/S_USA.AdministrativeForest.zip"
fs.bdry <- download_unzip_read(link = fs.url)
### CFLRP Data
cflrp.url <- "https://data.fs.usda.gov/geodata/edw/edw_resources/shp/S_USA.CFLR_HPRP_ProjectBoundary.zip"
cflrp.bdry <- download_unzip_read(link = cflrp.url)
```
### Step 3: Check validity
```{r}
all(st_is_valid(fs.bdry))
all(st_is_valid(cflrp.bdry))
```
```{r}
fs.bdry <- st_make_valid(fs.bdry)
cflrp.bdry <- st_make_valid(cflrp.bdry)
```
```{r}
all(st_is_valid(fs.bdry))
all(st_is_valid(cflrp.bdry))
```
### Step 3: Check alignment
```{r}
st_crs(fs.bdry)
st_crs(wildfire_haz)
st_crs(cflrp.bdry)
st_crs(cejst)
st_crs(wildfire_haz) == st_crs(cejst)
st_crs(wildfire_haz) == st_crs(fs.bdry)
target_crs <- st_crs(wildfire_haz)
```
```{r}
fs.bdry_proj <- st_transform(fs.bdry, crs = target_crs)
st_crs(fs.bdry_proj) == target_crs
st_crs(fs.bdry_proj) == st_crs(wildfire_haz)
cflrp.bdry_proj <- st_transform(cflrp.bdry, crs = st_crs(wildfire_haz))
```
### Step 4: Subset to geographies
cejst is our study area (the Pacific Northwest) to subset to.
```{r}
fs.subset <- fs.bdry_proj[cejst, ]
cflrp.subset <- cflrp.bdry_proj[cejst, ]
# keep only tracts that intersect Forest Service land
cejst.subset <- cejst[fs.subset, ]
```
### Step 5: Select attributes of interest
```{r}
cejst.df <- cejst.subset %>%
select(GEOID10, LMI_PFS, LHE, HBF_PFS)
head(cejst.df)
```
### Step 6: Extract wildfire hazard
```{r}
cejst.fire <- terra::extract(wildfire_haz, vect(cejst.df), fun=mean, na.rm=TRUE)
head(cejst.fire)
```
```{r}
cejst.df$WHP_ID <- cejst.fire$WHP_ID
```
### Step 7: Does each tract intersect a CFLRP boundary?
```{r}
cejst.cflrp <- apply(st_intersects(cejst.df, cflrp.subset, sparse=FALSE), 1, any)
```
```{r}
cejst.df <- cejst.df %>%
mutate(CFLRP = cejst.cflrp)
```
### Step 8: Compare areas
Many comparisons are possible! Here are some examples.
```{r}
cflrp.summ <- cejst.df %>%
st_drop_geometry() %>%
group_by(CFLRP) %>%
summarise(across(LMI_PFS:WHP_ID, ~ mean(.x, na.rm=TRUE)))
ggplot(data = cejst.df, aes(x=CFLRP, y=WHP_ID)) +
geom_boxplot()
```