This assignment is divided into three main sections.
In the first section, you will select two Census Tracts within Fulton and DeKalb Counties, GA — one that you believe is the most walkable and another that is the least walkable. You may choose any tracts within these two counties. If the area you want to analyze is not well represented by a single tract, you may select multiple adjacent tracts (e.g., two contiguous tracts as one “walkable area”). The definition of walkable is up to you — it can be based on your personal experience (e.g., places where you’ve had particularly good or bad walking experiences), Walk Score data, or any combination of criteria. After making your selections, provide a brief explanation of why you chose those tracts.
The second section is the core of this assignment. You will prepare OpenStreetMap (OSM) data, download Google Street View (GSV) images, and apply the computer vision technique covered in class — semantic segmentation.
In the third section, you will summarize and analyze the results. After applying computer vision to the images, you will obtain pixel counts for 19 different object categories. Using the data, you will:
Importing the necessary packages is part of this assignment. Add any required packages to the code chunk below as you progress through the tasks.
library(tidyverse)
library(magrittr)
library(osmdata)
library(sfnetworks)
library(units)
library(sf)
library(ggplot2)
library(tidygraph)
library(tidycensus)
library(tmap)
library(here)
library(progress)
library(nominatimlite)
ttm()
Use the Census Tract map in the following code chunk to identify the GEOIDs of the tracts you consider walkable and unwalkable.
# TASK ////////////////////////////////////////////////////////////////////////
# Set up your api key here
census_api_key(Sys.getenv("CENSUS_API_KEY"))
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Download Census Tract polygon for Fulton and DeKalb
tract <- get_acs("tract",
variables = c('pop' = 'B01001_001'),
year = 2023,
state = "GA",
county = c("Fulton", "DeKalb"),
geometry = TRUE)
tmap_mode("view")
tm_basemap("OpenStreetMap") +
tm_shape(tract) +
tm_polygons(fill_alpha = 0.2)
# =========== NO MODIFY ZONE ENDS HERE ========================================
Once you have the GEOIDs, create two Census Tract objects – one representing your most walkable area and the other your least walkable area.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Specify the GEOIDs of your walkable and unwalkable Census Tracts.
# e.g., tr_id_walkable <- c("13121001205", "13121001206")
# 2. Extract the selected Census Tracts using `tr_id_walkable` and `tr_id_unwalkable`
# For the walkable Census Tract(s)
tr_id_walkable <- c("13121001205")
tract_walkable <- tract %>%
filter(GEOID %in% tr_id_walkable)
# For the unwalkable Census Tract(s)
tr_id_unwalkable <- c("13121012000")
tract_unwalkable <- tract %>%
filter(GEOID %in% tr_id_unwalkable)
# //TASK //////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# Create an interactive map showing `tract_walkable` and `tract_unwalkable`
tm_shape(tract) +
tm_borders(col = "gray80") +
tm_shape(tract_walkable) +
tm_fill(col = "green", alpha = 0.25) +
tm_borders(col = "green", lwd = 2) +
tm_shape(tract_unwalkable) +
tm_fill(col = "red", alpha = 0.25) +
tm_borders(col = "red", lwd = 2) +
tm_layout(title = "Walkable vs Unwalkable Census Tracts")
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: use 'fill' for the fill color of polygons/symbols
## (instead of 'col'), and 'col' for the outlines (instead of 'border.col').
## [v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.
## [v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.
## [v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`
## This message is displayed once every 8 hours.
# //TASK //////////////////////////////////////////////////////////////////////
Provide a brief description of your selected Census Tracts. Why do you consider these tracts walkable or unwalkable? What factors do you think contribute to their walkability?
To obtain the OSM network for your selected Census Tracts: (1) Create
bounding boxes. (2) Use the bounding boxes to download OSM data. (3)
Convert the data into an sfnetwork object and clean it.
# TASK ////////////////////////////////////////////////////////////////////////
# Create one bounding box (`tract_walkable_bb`) for your walkable Census Tract(s) and another (`tract_unwalkable_bb`) for your unwalkable Census Tract(s).
# For the walkable Census Tract(s)
tract_walkable_bb <- tract_walkable %>%
st_bbox()
# For the unwalkable Census Tract(s)
tract_unwalkable_bb <- tract_unwalkable %>%
st_bbox()
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Get OSM data for the two bounding boxes
osm_walkable <- opq(bbox = tract_walkable_bb) %>%
add_osm_feature(key = 'highway',
value = c("primary", "secondary", "tertiary", "residential")) %>%
osmdata_sf() %>%
osm_poly2line()
osm_unwalkable <- opq(bbox = tract_unwalkable_bb) %>%
add_osm_feature(key = 'highway',
value = c("primary", "secondary", "tertiary", "residential")) %>%
osmdata_sf() %>%
osm_poly2line()
# =========== NO MODIFY ZONE ENDS HERE ========================================
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Convert `osm_walkable` and `osm_unwalkable` into sfnetwork objects (as undirected networks),
# 2. Clean the network by (1) deleting parallel lines and loops, (2) creating missing nodes, and (3) removing pseudo nodes (make sure the `summarise_attributes` argument is set to 'first' when doing so).
net_walkable <- osm_walkable$osm_lines %>%
select(osm_id, highway) %>%
sfnetworks::as_sfnetwork(directed = FALSE) %>%
activate("edges") %>%
filter(!edge_is_multiple()) %>% # remove duplicated edges
filter(!edge_is_loop()) %>% # remove loops
convert(., sfnetworks::to_spatial_subdivision) %>% # subdivide edges
convert(., sfnetworks::to_spatial_smooth, summarise_attributes = "first") # delete pseudo nodes
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
net_unwalkable <- osm_unwalkable$osm_lines %>%
select(osm_id, highway) %>%
sfnetworks::as_sfnetwork(directed = FALSE) %>%
activate("edges") %>%
filter(!edge_is_multiple()) %>% # remove duplicated edges
filter(!edge_is_loop()) %>% # remove loops
convert(., sfnetworks::to_spatial_subdivision) %>% # subdivide edges
convert(., sfnetworks::to_spatial_smooth, summarise_attributes = "first") # delete pseudo nodes
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
# //TASK //////////////////////////////////////////////////////////////////////
# TASK //////////////////////////////////////////////////////////////////////
# Using `net_walkable` and`net_unwalkable`,
# 1. Activate the edge component of each network.
# 2. Create a `length` column.
# 3. Filter out short (<300 feet) segments.
# 4. Randomly Sample 100 rows per road type.
# 5. Assign the results to `edges_walkable` and `edges_unwalkable`, respectively.
# OSM for the walkable part
set.seed(123) # for reproducibility
edges_walkable <- net_walkable %>%
st_as_sf("edges") %>%
select(osm_id, highway) %>%
# Add a length column
mutate(length = st_length(.) %>% unclass()) %>%
# Drop short segments (< 300 feet)
filter(length >= 91) %>%
# Sample 100 rows per road type
group_by(highway) %>%
slice_sample(n = 100) %>%
# Assign a unique ID for each edge
ungroup() %>%
mutate(edge_id = seq(1,nrow(.)))
# OSM for the unwalkable part
edges_unwalkable <- net_unwalkable %>%
st_as_sf("edges") %>%
select(osm_id, highway) %>%
# Add a length column
mutate(length = st_length(.) %>% unclass()) %>%
# Drop short segments (< 300 feet)
filter(length >= 91) %>%
# Sample 100 rows per road type
group_by(highway) %>%
slice_sample(n = 100) %>%
# Assign a unique ID for each edge
ungroup() %>%
mutate(edge_id = seq(1,nrow(.)))
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Merge the two
edges <- bind_rows(edges_walkable %>% mutate(is_walkable = TRUE),
edges_unwalkable %>% mutate(is_walkable = FALSE)) %>%
mutate(edge_id = seq(1,nrow(.)))
# =========== NO MODIFY ZONE ENDS HERE ========================================
getAzimuth() function.In this assignment, you will collect two GSV images per road segment, as illustrated in the figure below. To do this, you will define a function that extracts the coordinates of the midpoint and the azimuths in both directions.
If you can’t see this image, try changing the markdown editing mode from ‘Source’ to ‘Visual’ (you can find the buttons in the top-left corner of this source pane).
getAzimuth <- function(line){
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Use the `st_line_sample()` function to sample three points at locations 0.48, 0.5, and 0.52 along the line. These points will be used to calculate the azimuth.
# 2. Use `st_cast()` function to convert the 'MULTIPOINT' object into a 'POINT' object.
# 3. Extract coordinates using `st_coordinates()`.
# 4. Assign the coordinates of the midpoint to `mid_p`.
# 5. Calculate the azimuths from the midpoint in both directions and save them as `mid_azi_1` and `mid_azi_2`, respectively.
# 1-3
mid_p3 <- line %>%
st_line_sample(sample = c(0.48, 0.5, 0.52)) %>%
st_cast("POINT") %>%
st_coordinates()
# 4
mid_p <- mid_p3[2,]
# 5
mid_azi_1 <- atan2(mid_p3[1,"X"] - mid_p3[2, "X"],
mid_p3[1,"Y"] - mid_p3[2, "Y"])*180/pi
mid_azi_2 <- atan2(mid_p3[3,"X"] - mid_p3[2, "X"],
mid_p3[3,"Y"] - mid_p3[2, "Y"])*180/pi
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
return(tribble(
~type, ~X, ~Y, ~azi,
"mid1", mid_p["X"], mid_p["Y"], mid_azi_1,
"mid2", mid_p["X"], mid_p["Y"], mid_azi_2))
# =========== NO MODIFY ZONE ENDS HERE ========================================
}
Apply the getAzimuth() function to the
edges object. Once this step is complete, your data will be
ready for downloading GSV images.
# TASK ////////////////////////////////////////////////////////////////////////
# Apply getAzimuth() function to all edges.
# Remember that you need to pass edges object to st_geometry() before you apply getAzimuth()
edges_azi <- edges %>%
st_geometry() %>%
map_df(getAzimuth, .progress = T)
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
edges_azi <- edges_azi %>%
bind_cols(edges %>%
st_drop_geometry() %>%
slice(rep(1:nrow(edges),each=2))) %>%
st_as_sf(coords = c("X", "Y"), crs = 4326, remove=FALSE) %>%
mutate(img_id = seq(1, nrow(.)))
# =========== NO MODIFY ZONE ENDS HERE ========================================
getImage <- function(iterrow){
# This function takes one row of `edges_azi` and downloads GSV image using the information from the row.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Extract required information from the row of `edges_azi`
# 2. Format the full URL and store it in `request`. Refer to this page: https://developers.google.com/maps/documentation/streetview/request-streetview
# 3. Format the full path (including the file name) of the image being downloaded and store it in `fpath`
type = iterrow$type
location <- paste0(iterrow$Y %>% round(5), ",", iterrow$X %>% round(5))
heading <- iterrow$azi %>% round(1)
edge_id <- iterrow$edge_id
img_id <- iterrow$img_id
highway <- iterrow$highway
key <- Sys.getenv("GOOGLE_API_KEY") # your Google API key
endpoint <- "https://maps.googleapis.com/maps/api/streetview"
request <- glue::glue("{endpoint}?size=640x640&location={location}&heading={heading}&fov=90&pitch=0&key={key}")
fname <- glue::glue("GSV-nid_{img_id}-eid_{edge_id}-type_{type}-Location_{location}-heading_{heading}-highway_{highway}.jpg")
fpath <- "gsv_images" # Set your own directory for the downloaded images
# //TASK //////////////////////////////////////////////////////////////////////
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Download images
# if (!file.exists(fpath)){
# download.file(furl, fpath, mode = 'wb')
download.file(request, destfile = file.path(fpath, fname), mode = 'wb')
Sys.sleep(1)
# =========== NO MODIFY ZONE ENDS HERE ========================================
}
Before you download GSV images, make sure the row
number in edges_azi is not too large! Each row corresponds
to one GSV image, so if the row count exceeds your API quota, consider
selecting different Census Tracts.
You do not want to run the following code chunk more than once, so the code chunk option
eval=FALSEis set to prevent the API call from executing again when knitting the script.
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
for (i in seq(1,nrow(edges_azi))){
getImage(edges_azi[i,])
}
# =========== NO MODIFY ZONE ENDS HERE ========================================
ZIP THE DOWNLOADED IMAGES AND NAME IT ‘gsv_images.zip’ FOR STEP 6.
Use this Google Colab script to apply the pretrained semantic segmentation model to your GSV images.
Once all of the images are processed and saved in your Colab session
as a CSV file, download the CSV file and merge it back to
edges_azi.
# TASK ////////////////////////////////////////////////////////////////////////
# Read the downloaded CSV file containing the semantic segmentation results.
seg_output <- read.csv("seg_output_walkable.csv")
# //TASK ////////////////////////////////////////////////////////////////////////
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Join the `seg_output` data to `edges_azi`.
# 2. Calculate the proportion of predicted pixels for the following categories: `building`, `sky`, `road`, and `sidewalk`. If there are other categories you are interested in, feel free to include their proportions as well.
# 3. Calculate the proportion of greenness using the `vegetation` and `terrain` categories.
# 4. Calculate the building-to-street ratio. For the street, use `road` and `sidewalk` pixels; including `car` pixels is optional.
edges_seg_output <- edges_azi %>%
inner_join(seg_output, by=c("img_id"="img_id"))
edges_seg_output %<>%
mutate(pct_building = building/(768*768),
pct_sky = sky/(768*768),
pct_road = road/(768*768),
pct_sidewalk = sidewalk/(768*768))
edges_seg_output %<>%
mutate(pct_greenness = (vegetation+terrain)/(768*768))
edges_seg_output %<>%
mutate(b2s_ratio = building/(road+sidewalk+car)) %>%
mutate(b2s_ratio = case_when(
b2s_ratio >= 1 ~ 1, # Set the upper limit as 1.
TRUE ~ b2s_ratio))
# //TASK ////////////////////////////////////////////////////////////////////////
At the beginning of this assignment, you specified walkable and unwalkable Census Tracts. The key focus of this section is the comparison between these two types of tracts.
Create interactive maps showing the proportion of sidewalk, greenness, and the building-to-street ratio for both walkable and unwalkable areas. In total, you will produce 6 maps. Provide a brief description of your findings.
# TASK ////////////////////////////////////////////////////////////////////////
# Plot interactive map(s)
# As long as you can deliver the message clearly, you can use any format/package you want.
# Ensure CRS consistency
walk_bb <- st_transform(tract_walkable, st_crs(edges_seg_output))
unwalk_bb <- st_transform(tract_unwalkable, st_crs(edges_seg_output))
# Subset SVI points by bounding boxes
pts_walk <- edges_seg_output[lengths(st_intersects(edges_seg_output, walk_bb)) > 0, ]
pts_unwalk <- edges_seg_output[lengths(st_intersects(edges_seg_output, unwalk_bb)) > 0, ]
# Label group
pts_walk$area_type <- "Walkable"
pts_unwalk$area_type <- "Unwalkable"
# Combine
pts_all <- rbind(pts_walk, pts_unwalk)
tm_shape(tract_walkable) +
tm_borders(col = "green", lwd = 2) +
tm_shape(pts_walk) +
tm_dots(size = 0.7,
fill = "pct_sidewalk",
fill.scale = tm_scale(values = c("#F7FBFF", "#6BAED6", "#3182BD", "#08519C")))
tm_shape(tract_walkable) +
tm_borders(col = "green", lwd = 2) +
tm_shape(pts_walk) +
tm_dots(size = 0.7,
fill = "pct_greenness",
fill.scale = tm_scale(values = c("darkgray", "yellow", "green", "darkgreen")))
tm_shape(tract_walkable) +
tm_borders(col = "green", lwd = 2) +
tm_shape(pts_walk) +
tm_dots(size = 0.7,
fill = "b2s_ratio",
fill.scale = tm_scale(values = c("#EED8AE", "#C4A484", "#8B5A2B", "brown")))
tm_shape(tract_unwalkable) +
tm_borders(col = "red", lwd = 2) +
tm_shape(pts_unwalk) +
tm_dots(size = 0.7,
fill = "pct_sidewalk",
fill.scale = tm_scale(values = c("#F7FBFF", "#6BAED6", "#3182BD", "#08519C")))
tm_shape(tract_unwalkable) +
tm_borders(col = "red", lwd = 2) +
tm_shape(pts_unwalk) +
tm_dots(size = 0.7,
fill = "pct_greenness",
fill.scale = tm_scale(values = c("darkgray", "yellow", "green", "darkgreen")))
tm_shape(tract_unwalkable) +
tm_borders(col = "red", lwd = 2) +
tm_shape(pts_unwalk) +
tm_dots(size = 0.7,
fill = "b2s_ratio",
fill.scale = tm_scale(values = c("#EED8AE", "#C4A484", "#8B5A2B", "brown")))
# //TASK //////////////////////////////////////////////////////////////////////
Create boxplots for the proportion of each category (building, sky, road, sidewalk, greenness, and any additional categories of interest) and the building-to-street ratio for walkable and unwalkable tracts. Each plot should compare walkable and unwalkable tracts. In total, you will produce 6 or more boxplots. Provide a brief description of your findings.
# TASK ////////////////////////////////////////////////////////////////////////
# Create boxplot(s) using ggplot2 package.
# Select key variables for boxplots
vars <- c("pct_building", "pct_sky", "pct_road", "pct_sidewalk", "pct_greenness", "b2s_ratio")
df_long <- pts_all %>%
st_drop_geometry() %>%
select(area_type, all_of(vars)) %>%
tidyr::pivot_longer(
cols = all_of(vars),
names_to = "category",
values_to = "proportion"
)
# Building
ggplot(filter(df_long, category == "pct_building"),
aes(x = area_type, y = proportion, fill = area_type)) +
geom_boxplot(alpha = 0.7, width = 0.6) +
scale_fill_manual(values = c("Walkable" = "#4CAF50", "Unwalkable" = "#E64A19")) +
labs(title = "Building proportion", x = "", y = "Proportion") +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
# Sky
ggplot(filter(df_long, category == "pct_sky"),
aes(x = area_type, y = proportion, fill = area_type)) +
geom_boxplot(alpha = 0.7, width = 0.6) +
scale_fill_manual(values = c("Walkable" = "#4CAF50", "Unwalkable" = "#E64A19")) +
labs(title = "Sky proportion", x = "", y = "Proportion") +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
# Road
ggplot(filter(df_long, category == "pct_road"),
aes(x = area_type, y = proportion, fill = area_type)) +
geom_boxplot(alpha = 0.7, width = 0.6) +
scale_fill_manual(values = c("Walkable" = "#4CAF50", "Unwalkable" = "#E64A19")) +
labs(title = "Road proportion", x = "", y = "Proportion") +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
# Sidewalk
ggplot(filter(df_long, category == "pct_sidewalk"),
aes(x = area_type, y = proportion, fill = area_type)) +
geom_boxplot(alpha = 0.7, width = 0.6) +
scale_fill_manual(values = c("Walkable" = "#4CAF50", "Unwalkable" = "#E64A19")) +
labs(title = "Sidewalk proportion", x = "", y = "Proportion") +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
# Greenness
ggplot(filter(df_long, category == "pct_greenness"),
aes(x = area_type, y = proportion, fill = area_type)) +
geom_boxplot(alpha = 0.7, width = 0.6) +
scale_fill_manual(values = c("Walkable" = "#4CAF50", "Unwalkable" = "#E64A19")) +
labs(title = "Greenness", x = "", y = "Proportion") +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
# Building-to-street ratio
ggplot(filter(df_long, category == "b2s_ratio"),
aes(x = area_type, y = proportion, fill = area_type)) +
geom_boxplot(alpha = 0.7, width = 0.6) +
scale_fill_manual(values = c("Walkable" = "#4CAF50", "Unwalkable" = "#E64A19")) +
labs(title = "Building-to-street ratio", x = "", y = "Ratio") +
theme_minimal(base_size = 14) +
theme(legend.position = "none")
# //TASK //////////////////////////////////////////////////////////////////////
Perform t-tests on the mean proportion of each category (building, sky, road, sidewalk, greenness, and any additional categories of interest) as well as the building-to-street ratio between street segments in the walkable and unwalkable tracts. This will result in 6 or more t-test results. Provide a brief description of your findings.
# TASK ////////////////////////////////////////////////////////////////////////
# Perform t-tests and report both the differences in means and their statistical significance.
# As long as you can deliver the message clearly, you can use any format/package you want.
df_test <- pts_all %>%
st_drop_geometry() %>%
select(area_type, all_of(vars))
t_results <- lapply(vars, function(v) {
ttest <- t.test(df_test[[v]] ~ df_test$area_type)
data.frame(
variable = v,
mean_walkable = mean(df_test[[v]][df_test$area_type == "Walkable"], na.rm = TRUE),
mean_unwalkable = mean(df_test[[v]][df_test$area_type == "Unwalkable"], na.rm = TRUE),
t_statistic = ttest$statistic,
p_value = ttest$p.value
)
})
t_results <- bind_rows(t_results)
print(t_results)
## variable mean_walkable mean_unwalkable t_statistic p_value
## t...1 pct_building 0.20496931 0.05140554 -9.3628532 1.744899e-12
## t...2 pct_sky 0.12505622 0.24669755 6.5964544 1.070017e-08
## t...3 pct_road 0.33701945 0.36370849 2.0926506 4.146221e-02
## t...4 pct_sidewalk 0.05155872 0.04069135 -2.3709535 2.084979e-02
## t...5 pct_greenness 0.24147797 0.25961427 0.8785614 3.827748e-01
## t...6 b2s_ratio 0.51444138 0.12448134 -9.3061900 2.316283e-12
# //TASK //////////////////////////////////////////////////////////////////////