Compare commits

..

No commits in common. "96cdcbc5ecdc820c4429c3d44f9ca46a33c37519" and "9e1ba330975e13188b63d361e833f29c17e4d3be" have entirely different histories.

6 changed files with 21 additions and 84 deletions

15
.gitignore vendored
View File

@ -1,19 +1,8 @@
.Rproj* .Rproj*
data/addresses/* data/addresses/*
api_keys/* api_keys/*
figures/* figures/*
.Rhistory .Rhistory
.Rproj.user .Rproj.user
*.md
.DS_Store
!README.md
.Rhistory
*\.\#*
data
!data/
.RData
data-bkup/
data-bkup
*.R
*.bak
archive/

View File

@ -1,15 +0,0 @@
route_analysis: R/route_analysis.Rmd
R -e 'library("rmarkdown"); old_path <- Sys.getenv("PATH"); Sys.setenv(PATH = paste(old_path, "/usr/local/bin", sep = ":")); rmarkdown::render(knit_root_dir = "./", output_dir = "./html", input = "./R/route_analysis.Rmd", output_file = "./html/route_analysis.html")'
clean: clean-data clean-figure clean-script
clean-data:
rm -vf ./R/data/*.rds
clean-script:
rm -rvf ./*.md
clean-figure:
rm -rvf ./figure/
.PHONY: data

View File

@ -15,7 +15,3 @@ This script will generate a few figures:
### A map of those routes colored by the level of traffic stress to bike ### A map of those routes colored by the level of traffic stress to bike
![example routes-lts figure](examples/example-routes-lts.png) ![example routes-lts figure](examples/example-routes-lts.png)
## Using make
The command `make route_analysis` will run *route_analysis.Rmd* which
is an R markdown file containing the original R script *route_analysis.R*

4
html/.gitignore vendored
View File

@ -1,4 +0,0 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore

View File

@ -1,17 +1,3 @@
---
title: "Route Analysis"
output:
html_document:
toc: true
toc_depth: 5
toc_float:
collapsed: false
smooth_scroll: true
---
```{r libs, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
date()
rm(list=ls())
library(tidyverse) library(tidyverse)
library(ggmap) library(ggmap)
library(sf) library(sf)
@ -19,13 +5,8 @@ library(osrm)
library(smoothr) library(smoothr)
library(magick) library(magick)
library(ggnewscale) library(ggnewscale)
fig.height <- 6
set.seed(1)
```
# Main R script
```{r Rscript, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE}
## school focus ## school focus
school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("550852000925")) school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("550852000925"))
@ -33,10 +14,10 @@ school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("55085200
walk_boundary_m <- 1.5 * 1609 walk_boundary_m <- 1.5 * 1609
## load school locations ## load school locations
WI_schools <- st_read(dsn = "../data/Schools/WI_schools.gpkg") WI_schools <- st_read(dsn = "data/Schools/WI_schools.gpkg")
## load addresses ## load addresses
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 x=lon and y=lat
@ -44,7 +25,7 @@ addresses <- read_csv(file="../data/addresses/Addresses_Students_EastHS_2024_Geo
options(osrm.server = "http://127.0.0.1:5000/") options(osrm.server = "http://127.0.0.1:5000/")
options(osrm.profile = "walk") options(osrm.profile = "walk")
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))
## subset addresses within 1.5 miles ## subset addresses within 1.5 miles
walk_boundary_poly <- fill_holes(st_make_valid(osrmIsodistance( walk_boundary_poly <- fill_holes(st_make_valid(osrmIsodistance(
@ -56,7 +37,7 @@ 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)
## load bike tls ## load bike tls
bike_lts <- st_read("../data/bike_lts/bike_lts_DANE.geojson") bike_lts <- st_read("data/bike_lts/bike_lts_DANE.geojson")
bike_lts[["lts"]] <- as.factor(bike_lts$LTS_F) bike_lts[["lts"]] <- as.factor(bike_lts$LTS_F)
bike_lts_scale <- data.frame(code = c(1, 2, 3, 4, 9), bike_lts_scale <- data.frame(code = c(1, 2, 3, 4, 9),
@ -77,7 +58,7 @@ for(i in addresses_near$number) {
routes <- bind_rows(routes) routes <- bind_rows(routes)
## combine routes ## combine routes
bike_lts_buffer <- st_buffer(st_intersection(bike_lts, walk_boundary_poly), 20) bike_lts_buffer <- st_buffer(st_intersection(bike_lts, walk_boundary_poly), 10)
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))
@ -85,8 +66,8 @@ bike_lts <- st_join(bike_lts, bike_lts_buffer %>% select(OBJECTID, student_use))
## make maps ## make maps
# load logo # load logo
logo <- image_read(path = "../other/BFW_Logo_180_x_200_transparent_background.png") logo <- image_read(path = "other/BFW_Logo_180_x_200_transparent_background.png")
school_symbol <- image_read_svg(path = "../other/school_FILL0_wght400_GRAD0_opsz24.svg") school_symbol <- image_read_svg(path = "other/school_FILL0_wght400_GRAD0_opsz24.svg")
bbox <- st_bbox(st_transform(st_buffer(addresses_near, dist = 500), crs = 4326)) bbox <- st_bbox(st_transform(st_buffer(addresses_near, dist = 500), crs = 4326))
@ -106,7 +87,7 @@ ggmap(basemap) +
x = NULL, x = NULL,
y = NULL, y = NULL,
color = NULL, color = NULL,
linewidth = "Potential student walkers") + linewidth = "How many students can use road") +
theme(axis.text=element_blank(), theme(axis.text=element_blank(),
axis.ticks=element_blank(), axis.ticks=element_blank(),
plot.caption = element_text(color = "grey")) + plot.caption = element_text(color = "grey")) +
@ -136,7 +117,7 @@ ggmap(basemap) +
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),
" Routes.pdf"), " Routes.pdf"),
title = paste0(school_focus %>% pull(name), " Walking Routes"), title = paste0(school_focus %>% pull(name), " Walking Routes"),
@ -150,11 +131,11 @@ ggsave(file = paste0("../figures/",
ggmap(basemap) + ggmap(basemap) +
labs(title = paste0("Walking routes for students at ", labs(title = paste0("Walking routes for students at ",
school_focus %>% pull(name)), school_focus %>% pull(name)),
subtitle = "only showing routes within the walk boundary", subtitle = "only showing routes within the 1.5 walk boundary",
x = NULL, x = NULL,
y = NULL, y = NULL,
color = NULL, color = NULL,
linewidth = "Potential student walkers") + linewidth = "How many students can use road") +
theme(axis.text=element_blank(), theme(axis.text=element_blank(),
axis.ticks=element_blank(), axis.ticks=element_blank(),
plot.caption = element_text(color = "grey")) + plot.caption = element_text(color = "grey")) +
@ -183,7 +164,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),
" Routes - Traffic Stress.pdf"), " Routes - Traffic Stress.pdf"),
title = paste0(school_focus %>% pull(name), " Walking Routes - Traffic Stress"), title = paste0(school_focus %>% pull(name), " Walking Routes - Traffic Stress"),
@ -227,7 +208,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"),
title = paste0(school_focus %>% pull(name), " Addresses"), title = paste0(school_focus %>% pull(name), " Addresses"),
@ -236,13 +217,3 @@ ggsave(file = paste0("../figures/",
width = 11, width = 11,
units = "in", units = "in",
create.dir = TRUE) create.dir = TRUE)
```
# Appendix
```{r chunklast, eval = TRUE, echo = TRUE, results = "show", warning = TRUE, error = TRUE, message = TRUE}
date()
sessionInfo()
```