Created
November 13, 2019 16:20
-
-
Save ericrobskyhuntley/71e57caee91bb57550ce778d726cef57 to your computer and use it in GitHub Desktop.
Estimate population adjacent to MBTA stops using Census API and MBTA API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('tidyverse') | |
require('tidycensus') | |
library('jsonlite') | |
require('sf') | |
census_api_key("<yourapikey>") | |
stops <- fromJSON("https://api-v3.mbta.com/stops/?filter[route]=Red", simplifyVector = TRUE)[[1]]$attributes %>% | |
st_as_sf(coords = c('longitude', 'latitude')) %>% | |
st_set_crs(4326) %>% | |
st_transform(2249) | |
pops <- get_acs(geography = "tract", | |
variables = "B01003_001E", | |
year = 2017, | |
state = "MA", | |
county = "Middlesex", | |
geometry = TRUE, | |
keep_geo_vars = FALSE, | |
survey = "acs5") %>% | |
rename_all(tolower) %>% | |
rename(pop = estimate) %>% | |
select(-c("moe", "variable")) %>% | |
st_transform(2249) %>% | |
mutate( | |
tot_area = as.numeric(st_area(geometry)) | |
) | |
stop_pops <- stops[pops,] %>% | |
st_buffer(dist=2640) %>% | |
st_intersection(pops) %>% | |
mutate( | |
int_area = as.numeric(st_area(geometry)), | |
pop_prop = pop * int_area/tot_area | |
) %>% | |
group_by(address) %>% | |
summarize( | |
name = first(name), | |
municipality = first(municipality), | |
pop_est = sum(pop_prop) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment