In this notebook we process a cross-tabulation of origin and destinations by people employed full-time in the Greater Golden Horseshoe (GGH) in 2016.
The source data is in file tts-2016-OD-FT-Employment.txt
, which was retrieved from the Transportation Tomorrow Survey Data Retrieval System on October 28, 2021.
The centroids of all work zones (origins) and full-time (FT) employment zones (destinations) are calculated.
Load packages:
library(readxl) library(sf) library(tidyverse) library(units)
Read the boundaries of the traffic analysis zones:
ggh_taz <- st_read("TTS16-data-inputs/OD-by-FT-Employment/tts06_83_region.shp")
Project object:
ggh_taz <- ggh_taz %>% st_transform(crs = 32617)
Prepare traffic analysis zones:
ggh_taz <- ggh_taz %>% transmute(GTA06 = as.character(GTA06), AREA = st_area(geometry) %>% set_units(km^2) %>% drop_units())
Extract the IDs for all TAZ:
ghta_taz_id <- ggh_taz$GTA06
Read trip tables. The query was for "trips" by zone of residence, zone of work, and trip purpose (Work):
od_ft <- read_delim(file = "TTS16-data-inputs/OD-by-FT-Employment/tts-2016-OD-FT-Employment.txt", delim = "\t", col_names = FALSE)
Find the position in the table where the information on work trips begins:
idx <- which(od_ft$X1 == "TABLE : emp_stat (Full time)")
Slice the table to obtain the work trips (remove heading information):
od_ft <- od_ft %>% slice((idx+2):n())
Separate the zone identifiers and the trips, and convert to numeric:
od_ft <- od_ft %>% separate(X1, into = c("Zones", "Persons"), sep = " (?=[^ ]+$)") %>% mutate(Zones = str_trim(Zones, side = "both")) %>% separate(Zones, into = c("Origin", "Destination"), sep = " (?=[^ ]+$)") %>% mutate(Origin = str_trim(Origin), Destination = str_trim(Destination), Persons = as.numeric(Persons))
The resulting object od_ft
has the number of individuals who are employed full-time by place of residence (Origin) and place of work (Destination) using the GTA06 zoning system.
The od table can be used to calculate the workers and jobs by traffic analysis zone:
workers_ft <- od_ft %>% group_by(Origin) %>% summarize(workers = sum(Persons), .groups = "drop") %>% transmute(GTA06 = Origin, workers) jobs_ft <- od_ft %>% group_by(Destination) %>% summarize(jobs = sum(Persons), .groups = "drop") %>% transmute(GTA06 = Destination, jobs)
Join workers and jobs to simple features object:
ggh_taz <- ggh_taz %>% # Join workers left_join(workers_ft, by = "GTA06") %>% # Join jobs left_join(jobs_ft, by = "GTA06") %>% # Replace NAs with zeros mutate(workers = replace_na(workers, 0), jobs = replace_na(jobs, 0))
Plot worker population in the ggh:
ggplot() + geom_sf(data = ggh_taz, aes(fill = workers), color = NA) + scale_fill_fermenter(direction = 1)
Plot job distribution in the ggh:
ggplot() + geom_sf(data = ggh_taz, aes(fill = jobs), color = NA) + scale_fill_fermenter(direction = 1)
Create centroids for each feature in ggh_taz:
ggh_taz_cent <- st_centroid(ggh_taz)
Attach the centroid point geometry to the origin object (workers) and the destination object (jobs)
#workers (origins) workers_ft_cent <- workers_ft %>% merge(ggh_taz_cent %>% select(GTA06), by="GTA06") %>% st_sf work_origins <- cbind(workers_ft_cent, st_coordinates(st_transform(workers_ft_cent, crs = 4326))) %>% rename(lon = "X", lat = "Y", id = "GTA06") %>% as.data.frame() %>% select(id, lon, lat) %>% as.data.frame()
#jobs (destinations) jobs_ft_cent <- jobs_ft %>% merge(ggh_taz_cent %>% select(GTA06), by="GTA06") %>% st_sf job_destinations <- cbind(jobs_ft_cent, st_coordinates(st_transform(jobs_ft_cent, crs = 4326))) %>% rename(lon = "X", lat = "Y", id = "GTA06") %>% as.data.frame() %>% select(id, lon, lat)
save("work_origins", file = "TTS16-data-inputs/Travel-Time-Calculations/inputs/work_origins.Rdata") save("job_destinations", file = "TTS16-data-inputs/Travel-Time-Calculations/inputs/job_destinations.Rdata")
save("ggh_taz", file = "TTS16-data-inputs/OD-by-FT-Employment/ggh_taz.Rdata") save("od_ft", file = "TTS16-data-inputs/OD-by-FT-Employment/od_ft.Rdata")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.