Finished Organizing Rmarkdown Script

This commit is contained in:
syounkin 2024-11-01 10:29:10 -05:00
parent c721c58a9a
commit 3583c0917b
2 changed files with 30 additions and 23 deletions

1
.gitignore vendored
View File

@ -20,3 +20,4 @@ archive/
trash/ trash/
api_key api_key
R/route_analysis.html R/route_analysis.html
log/

View File

@ -7,7 +7,7 @@ output:
toc_float: toc_float:
collapsed: false collapsed: false
smooth_scroll: true smooth_scroll: true
editor_options: editor_options:
chunk_output_type: console chunk_output_type: console
--- ---
@ -34,16 +34,19 @@ set.seed(1)
```{r gpkg, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} ```{r gpkg, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
WI_schools <- st_transform(st_read(dsn = "data/Schools/Wisconsin_Public_Schools_-5986231931870160084.gpkg"), crs = 4326) WI_schools <- st_transform(st_read(dsn = "data/Schools/Wisconsin_Public_Schools_-5986231931870160084.gpkg"), crs = 4326)
WI_schools <- WI_schools %>% mutate(geom = SHAPE) # WI_schools <- WI_schools %>% mutate(geom = SHAPE)
names(WI_schools)
``` ```
There was an error here. _SHAPE_ does not exist in the _WI\_schools_ object. The variable _geom_ is already in _WI\_schools_.
## Addresses Data ## Addresses Data
```{r addresses, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} ```{r addresses, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
addresses <- read_csv(file="data/addresses/Addresses_Students_EastHS_2024_GeocodeResults.csv") %>% addresses <- read_csv(file="data/addresses/Addresses_Students_EastHS_2024_GeocodeResults.csv") %>%
filter(lat > 0) %>% filter(lat > 0) %>%
st_as_sf(coords=c("lon","lat"), crs=4326) # remember x=lon and y=lat st_as_sf(coords=c("lon","lat"), crs=4326)
``` ```
(Remember that x = lon and y = lat.)
## Open Source Routing Machine (OSRM) ## Open Source Routing Machine (OSRM)
@ -57,14 +60,13 @@ options(osrm.profile = "walk")
```{r stadiamaps, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} ```{r stadiamaps, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
register_stadiamaps(key = substr(read_file(file = "api_keys/stadia_api_key"), 1, 36)) register_stadiamaps(key = substr(read_file(file = "api_keys/stadia_api_key"), 1, 36))
``` ```
# Analysis # Analysis
## Subset Addresses Within 1.5 Miles ## Subset Addresses Within 1.5 Miles
```{r walkBoundary, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} ```{r walkBoundary, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
walk_boundary_m <- 1.5 * 1609 ## walk boundary walk_boundary_m <- 1.5*1609
school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("550852000925")) ## school focus school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("550852000925"))
walk_boundary_poly <- fill_holes(st_make_valid(osrmIsodistance( walk_boundary_poly <- fill_holes(st_make_valid(osrmIsodistance(
loc = WI_schools %>% filter(NCES_CODE %in% school_focus$NCES_CODE), loc = WI_schools %>% filter(NCES_CODE %in% school_focus$NCES_CODE),
@ -75,6 +77,21 @@ walk_boundary_poly <- fill_holes(st_make_valid(osrmIsodistance(
addresses_near <- st_intersection(addresses, walk_boundary_poly) addresses_near <- st_intersection(addresses, walk_boundary_poly)
``` ```
## Calculate Routes
```{r routes, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
routes <- list(NULL)
for(i in addresses_near$number) {
routes[[i]] <- osrmRoute(
src = addresses_near %>% filter(number == i),
dst = WI_schools %>% filter(NCES_CODE %in% school_focus$NCES_CODE))
message(paste0("done - ", i, "of", max(addresses_near$number)))
}
routes <- bind_rows(routes)
```
## Bike Level of Traffic Stress (LTS) ## Bike Level of Traffic Stress (LTS)
```{r bikelts, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} ```{r bikelts, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
@ -91,21 +108,6 @@ bike_lts_scale <- data.frame(code = c(1, 2, 3, 4, 9),
"#fdae61", "#fdae61",
"#d7191c", "#d7191c",
"#d7191c")) "#d7191c"))
```
## The Rest
```{r therest, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
## calculate routes
routes <- list(NULL)
for(i in addresses_near$number) {
routes[[i]] <- osrmRoute(
src = addresses_near %>% filter(number == i),
dst = WI_schools %>% filter(NCES_CODE %in% school_focus$NCES_CODE))
message(paste0("done - ", i, "of", max(addresses_near$number)))
}
routes <- bind_rows(routes)
## combine routes ## combine routes
# Count the routes that intersect or overlap with each segment of the bike_tls network. # Count the routes that intersect or overlap with each segment of the bike_tls network.
@ -115,6 +117,11 @@ bike_lts_buffer <- st_buffer(st_intersection(bike_lts, walk_boundary_poly), 20)
bike_lts_buffer["student_use"] <- unlist(lapply(st_intersects(bike_lts_buffer, routes), length)) bike_lts_buffer["student_use"] <- unlist(lapply(st_intersects(bike_lts_buffer, routes), length))
bike_lts <- st_join(bike_lts, bike_lts_buffer %>% select(OBJECTID, student_use)) bike_lts <- st_join(bike_lts, bike_lts_buffer %>% select(OBJECTID, student_use))
```
# Make Maps
```{r maps, eval = TRUE, echo = FALSE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
## make maps ## make maps
# load logo # load logo
@ -260,6 +267,7 @@ ggmap(basemap) +
nudge_y = 0.0015, nudge_y = 0.0015,
label.size = 0.04, label.size = 0.04,
size = 2) size = 2)
ggsave(file = paste0("figures/", ggsave(file = paste0("figures/",
school_focus %>% pull(name), school_focus %>% pull(name),
" Addresses.pdf"), " Addresses.pdf"),
@ -269,8 +277,6 @@ ggsave(file = paste0("figures/",
width = 11, width = 11,
units = "in", units = "in",
create.dir = TRUE) create.dir = TRUE)
``` ```
# Appendix # Appendix