knitr::opts_chunk$set(message = FALSE, warning = FALSE)
# Load the packages used in your analysis.
library(tidyverse)
library(sf)
library(tigris)
library(osmdata)
library(sfnetworks)
library(tidygraph)
library(tmap)
library(glue)
library(magrittr)
Complete the blank code chunks, provide explanations where requested, and add code comments where necessary. Keep the existing code chunks and section headings. Use methods from class and the course labs; code needed to preserve your inputs is supplied.
Work through the file in order:
eval=FALSE
and run them manually in RStudio.save-inputs to save your sample, then complete
image collection and Colab processing in Section 4.eval=FALSE in
place: the report loads your saved inputs instead of resampling roads or
downloading images again.Save this Rmd, major2_inputs.rds, and the downloaded
seg_output.csv in the same project folder. Use a
downloaded_image subfolder for the JPG files. Open an
RStudio project in that folder so relative paths work both interactively
and when knitting. Do not include private credentials in the
document.
Select two Census tracts that you expect to differ in walkability. Compare their streetscapes using images randomly sampled from each road network. Then compare the tracts within road classes represented in both samples. Does your interpretation change when you compare similar kinds of roads?
The labels “More walkable” and “Less walkable” describe your initial expectations, not a conclusion established by the model. Image composition captures only some aspects of walkability.
primary, secondary,
tertiary, and residential roads. Exclude other
OSM classes, including connector roads.require_equal = "highway"
and retain attributes with
summarise_attributes = "first".fov=90, and pitch=0.local_crs <- 32616 # PCS / WGS84 / UTM 16N
sampling_seed <- 2026
Download tract geometry and choose one tract you expect to be more walkable and another you expect to be less walkable. Your choices can reflect personal experience, a preliminary map review, or another stated reason.
Create tracts, an sf object with two
polygon features and columns GEOID and
tract_label. GEOID identifies the tract;
tract_label records your initial walkability expectation –
use the labels "More walkable" and
"Less walkable" throughout the analysis.
# Download the tract geometry and select the two tracts.
tracts <- tigris::tracts(
state = "GA",
county = "Fulton",
year = 2024,
cb = FALSE
) %>%
filter(NAME %in% c("96.01", "96.04"))
# Assign tract labels and transform to local_crs for spatial calculations.
tracts <- tracts %>%
mutate(
tract_label = case_when(
NAME == "96.04" ~ "More walkable",
NAME == "96.01" ~ "Less walkable"
)
) %>%
st_transform(local_crs)
tracts %>%
st_drop_geometry() %>%
select(GEOID, NAME, tract_label)
Briefly explain your choices and what streetscape differences you expect to find (no more than 100 words).
Your explanation: I selected two census tracts in Buckhead that I am familiar with. I expect Tract 96.04 to be more walkable because it is closer to the Buckhead Village area, where there are shops and restaurants. In contrast, I find walking in Tract 96.01 tiring. It is an office area with construction, heavy and fast-moving traffic, limited shade, and streetscapes that feel less interesting. In summer, walking there can be especially hot. I expect the images to show differences in sidewalks, buildings, and vegetation between the two tracts.
For each tract:
sampling_seed and a consistent row order to sample
up to 100 segments across all eligible road classes. Use all segments if
fewer than 100 are eligible.Combine the two samples into edges, an sf
object of LINESTRING features in local_crs. Keep
tract_label, GEOID, highway, and
length (meters). Assign a unique edge_id to
each segment across the combined sample.
# Download and clean the road networks for the two tracts.
set_overpass_url("https://overpass-api.de/api/interpreter")
osm_roads <- lapply(seq_len(nrow(tracts)), function(i) {
tract_bbox <- tracts[i, ] %>%
st_transform(4326) %>%
st_bbox()
opq(bbox = tract_bbox, timeout = 180) %>%
add_osm_feature(
key = "highway",
value = c("primary", "secondary", "tertiary", "residential")
) %>%
osmdata_sf() %>%
osm_poly2line()
})
# Calculate lengths and retain segments with eligible midpoints and lengths.
# Clean the road network for Tract 96.04.
net_9604 <- osm_roads[[1]]$osm_lines %>%
st_transform(local_crs) %>%
select(osm_id, highway) %>%
sfnetworks::as_sfnetwork(directed = FALSE) %>%
activate("edges") %>%
filter(!edge_is_multiple()) %>%
filter(!edge_is_loop()) %>%
convert(to_spatial_subdivision) %>%
convert(
to_spatial_smooth,
require_equal = "highway",
summarise_attributes = "first"
)
# Clean the road network for Tract 96.01.
net_9601 <- osm_roads[[2]]$osm_lines %>%
st_transform(local_crs) %>%
select(osm_id, highway) %>%
sfnetworks::as_sfnetwork(directed = FALSE) %>%
activate("edges") %>%
filter(!edge_is_multiple()) %>%
filter(!edge_is_loop()) %>%
convert(to_spatial_subdivision) %>%
convert(
to_spatial_smooth,
require_equal = "highway",
summarise_attributes = "first"
)
net_9604
net_9601
# Extract edges and calculate lengths.
edges_9604 <- net_9604 %>%
st_as_sf("edges") %>%
select(osm_id, highway) %>%
mutate(length = as.numeric(st_length(.))) %>%
filter(length >= 91.44)
edges_9601 <- net_9601 %>%
st_as_sf("edges") %>%
select(osm_id, highway) %>%
mutate(length = as.numeric(st_length(.))) %>%
filter(length >= 91.44)
# Find the midpoint of each whole segment.
midpoints_9604 <- st_line_sample(st_geometry(edges_9604), sample = 0.5)
midpoints_9601 <- st_line_sample(st_geometry(edges_9601), sample = 0.5)
# Keep whole segments whose midpoint falls within the matching tract.
edges_9604 <- edges_9604[
lengths(st_intersects(midpoints_9604, tracts[tracts$NAME == "96.04", ])) > 0,
]
edges_9601 <- edges_9601[
lengths(st_intersects(midpoints_9601, tracts[tracts$NAME == "96.01", ])) > 0,
]
# Inspect eligible counts before sampling.
table(edges_9604$highway)
table(edges_9601$highway)
# Use sampling_seed to randomly sample up to 100 segments per tract.
# Both tracts have fewer than 100 eligible segments, so keep all of them.
# Combine the samples and assign unique edge IDs.
edges_9604 <- edges_9604 %>%
mutate(
GEOID = "13121009604",
tract_label = "More walkable"
)
edges_9601 <- edges_9601 %>%
mutate(
GEOID = "13121009601",
tract_label = "Less walkable"
)
edges <- bind_rows(edges_9604, edges_9601) %>%
mutate(edge_id = row_number())
Adapt getHeading() from the GSV lab to take one
projected LINESTRING and return two midpoint views. Sample points at
0.48, 0.50, and 0.52 along the segment. From the midpoint (0.50),
calculate headings toward the other two points, looking toward each end
of the segment.
Return two rows with type ("mid1" and
"mid2"), X, Y, and
heading. Use the midpoint coordinates for both rows and
express headings from 0 to less than 360 degrees.
getHeading <- function(line) {
# Locate the midpoint and the nearby points used for heading calculations.
mid_p3 <- line %>%
st_line_sample(sample = c(0.48, 0.50, 0.52)) %>%
st_cast("POINT") %>%
st_coordinates()
# Calculate both viewing directions and return their locations and headings.
mid_heading_1 <- atan2(
mid_p3[1, "X"] - mid_p3[2, "X"],
mid_p3[1, "Y"] - mid_p3[2, "Y"]
) * 180 / pi
mid_heading_2 <- atan2(
mid_p3[3, "X"] - mid_p3[2, "X"],
mid_p3[3, "Y"] - mid_p3[2, "Y"]
) * 180 / pi
tibble(
type = c("mid1", "mid2"),
X = mid_p3[2, "X"],
Y = mid_p3[2, "Y"],
heading = c(mid_heading_1, mid_heading_2) %% 360
)
}
Apply the function to edges and create
edges_heading. Create points using local_crs,
and transform them to EPSG:4326. Update X and
Y to longitude and latitude after transforming. Assign
unique img_id values across both tracts. Colab extracts the
image ID from the filename and preserves it in the CSV.
edges_heading must have two POINT rows per sampled
segment and contain edge_id, img_id,
tract_label, GEOID, highway,
type, X, Y, and
heading.
# Apply getHeading() and attach the segment attributes.
edges_heading <- edges %>%
st_geometry() %>%
map_df(getHeading) %>%
bind_cols(
edges %>%
st_drop_geometry() %>%
slice(rep(1:nrow(edges), each = 2))
)
# Transform the points, update longitude/latitude, and assign image IDs.
edges_heading <- edges_heading %>%
st_as_sf(coords = c("X", "Y"), crs = local_crs) %>%
st_transform(4326) %>%
mutate(
X = st_coordinates(.)[, "X"],
Y = st_coordinates(.)[, "Y"],
heading = heading %% 360,
img_id = seq_len(nrow(.))
)
Run the supplied save chunk manually after preparing your sample. If you change the tracts or sampling before collecting images, regenerate and save the inputs together. Once image collection starts, keep the IDs fixed.
# Provided: run once after preparing your inputs.
saveRDS(
list(tracts = tracts, edges = edges, edges_heading = edges_heading),
"major2_inputs.rds"
)
# Provided: restore the same inputs when knitting.
saved_inputs <- readRDS("major2_inputs.rds")
tracts <- saved_inputs$tracts
edges <- saved_inputs$edges
edges_heading <- saved_inputs$edges_heading
stopifnot(nrow(tracts) == 2, !anyDuplicated(tracts$GEOID))
stopifnot(setequal(tracts$tract_label, c("More walkable", "Less walkable")))
stopifnot(!anyNA(edges$edge_id), !anyDuplicated(edges$edge_id))
stopifnot(nrow(edges_heading) == 2 * nrow(edges))
stopifnot(!anyNA(edges_heading$img_id), !anyDuplicated(edges_heading$img_id))
stopifnot(!anyNA(edges_heading$edge_id), !anyNA(edges_heading$type))
stopifnot(setequal(edges_heading$edge_id, edges$edge_id))
# Each sampled segment must have exactly one view of each type.
view_counts <- edges_heading %>%
sf::st_drop_geometry() %>%
dplyr::group_by(edge_id) %>%
dplyr::summarise(
n_views = dplyr::n(),
n_mid1 = sum(type == "mid1"),
n_mid2 = sum(type == "mid2"),
.groups = "drop"
)
stopifnot(all(view_counts$n_views == 2L),
all(view_counts$n_mid1 == 1L),
all(view_counts$n_mid2 == 1L))
Create an interactive map showing both selected tracts with clearly identified tract labels. Include this map in the knitted report.
# Map the selected tract polygons and identify the two tracts.
tmap_mode("view")
tm_basemap("OpenStreetMap") +
tm_shape(tracts) +
tm_polygons(
fill = "tract_label",
fill_alpha = 0.4,
col = "black",
lwd = 2
) +
tm_text("NAME", size = 0.8)
Adapt getImage() from the GSV lab to download one image
from one row of edges_heading:
type, img_id,
edge_id, and heading. Format
location as latitude,longitude (Y,X).request using the lab’s Street View Static API endpoint and
image settings.fpath to the supplied filename inside
downloaded_image.The filename and download commands are provided below.
getImage <- function(iterrow) {
# Extract row values, format location and heading, and construct the request URL.
type <- iterrow$type
img_id <- iterrow$img_id
edge_id <- iterrow$edge_id
heading <- round(iterrow$heading, 1)
location <- paste0(
round(iterrow$Y, 5), ",",
round(iterrow$X, 5)
)
key <- Sys.getenv("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}"
)
# Provided filename: keep img_id as the first number in the filename.
fname <- glue::glue("GSV-img_{img_id}-eid_{edge_id}-type_{type}.jpg")
# Create fpath by combining the output folder with fname.
fpath <- file.path("downloaded_image", fname)
# Provided: skip an existing file rather than request it again.
if (!file.exists(fpath)) {
download.file(request, fpath, mode = "wb", quiet = TRUE)
Sys.sleep(1)
}
}
Run the supplied download chunk manually, keeping
eval=FALSE. Inspect the images and remove gray “no imagery”
placeholders or invalid files before running Colab. Do not change or
reuse image IDs to fill gaps. Contact me if technical problems prevent
you from completing collection or processing.
# Provided: run manually, not when knitting.
dir.create("downloaded_image", showWarnings = FALSE)
for (i in seq_len(nrow(edges_heading))) {
getImage(edges_heading[i, ])
}
Zip the image folder as gsv_images.zip. Use the Colab
notebook shared for the computer-vision lab (https://colab.research.google.com/drive/1s0HiZIwjke-xtmHwzFmFp6PJrNkysoy9),
set the extracted image-folder path, run segmentation on all images, and
download seg_output.csv to your project folder. Running the
supplied Python is required; writing Python or changing the model is not
assessed. Do not include the notebook’s demonstration outputs or
individual image predictions in this report.
Read seg_output.csv and join it to
edges_heading using the shared img_id column.
Create image_results, with one row per matching image and
the original segment and tract attributes.
Determine the total number of pixels in the output mask from Colab or
by summing all category counts for one image, as in the lab. Use that
total to calculate these six image-level measures. As in the lab, the
five pct_* measures are percentages from 0 to 100; multiply
the pixel fractions by 100 and label their plots and tables accordingly.
The building-to-street ratio is not a percentage:
pct_building: (building pixels / total pixels) *
100.pct_sky: (sky pixels / total pixels) * 100.pct_road: (road pixels / total pixels) * 100.pct_sidewalk: (sidewalk pixels / total pixels) *
100.pct_greenness: ((vegetation pixels + terrain pixels) /
total pixels) * 100.b2s_ratio: building pixels divided by road + sidewalk +
car pixels. Apply the lab’s rule: set the ratio to NA_real_
when the street-pixel total is zero, or the ratio exceeds 4. Apply this
rule to each image before averaging the views.# Read the CSV, join image IDs to locations, and calculate image-level measures.
seg_output <- read.csv("seg_output.csv")
image_results <- edges_heading %>%
inner_join(seg_output, by = "img_id")
total_pixels <- 589824
image_results <- image_results %>%
mutate(
pct_building = building / total_pixels * 100,
pct_sky = sky / total_pixels * 100,
pct_road = road / total_pixels * 100,
pct_sidewalk = sidewalk / total_pixels * 100,
pct_greenness = (vegetation + terrain) / total_pixels * 100,
street_pixels = road + sidewalk + car,
b2s_ratio = if_else(
street_pixels == 0 | building / street_pixels > 4,
NA_real_,
building / street_pixels
)
)
Create segment_results, an sf object with
one midpoint POINT per segment. Keep only segments with both views
(mid1 and mid2) and valid values for all six
measures in each view (no missing or infinite values). Keep excluded
images in image_results.
Average each image-level measure across the segment’s two views. In
particular, average the two image-level building-to-street ratios rather
than calculate a new ratio from pooled counts. Keep
edge_id, GEOID, tract_label,
highway, and the six measure names. Drop geometry before
grouping and summarizing measurements, then join the summary back to one
midpoint per segment.
# Identify complete pairs with valid measurements.
measure_names <- c(
"pct_building", "pct_sky", "pct_road",
"pct_sidewalk", "pct_greenness", "b2s_ratio"
)
valid_images <- image_results %>%
st_drop_geometry() %>%
filter(if_all(all_of(measure_names), is.finite))
# Average the two views within each segment and restore one midpoint geometry.
segment_summary <- valid_images %>%
group_by(edge_id, GEOID, tract_label, highway) %>%
filter(n() == 2, setequal(type, c("mid1", "mid2"))) %>%
summarise(
across(all_of(measure_names), mean),
.groups = "drop"
)
# Restore one midpoint geometry per segment.
segment_points <- edges_heading %>%
filter(type == "mid1") %>%
select(edge_id)
segment_results <- segment_points %>%
inner_join(segment_summary, by = "edge_id")
Use segment_results to create:
tract_label on the x-axis and the measure
on the y-axis, giving two boxes per panel. Use readable panel titles.
Each panel may have its own y-axis scale, shared by both tracts.tract_summary with one row per tract. Include the sampled
segment count from edges, the included segment count from
segment_results, and the mean of each of the six measures
for included segments.If a tract has no valid pairs, report zero included segments and
NA means. Show the available results and explain any
comparisons you cannot make. If neither tract has valid pairs, skip the
measure maps and boxplots. Keep the code chunks and required columns in
empty result objects.
# Map the three measures and make the two tracts easy to compare.
tmap_mode("view")
# 1. Sidewalk percentage
tm_basemap("OpenStreetMap") +
tm_shape(tracts) +
tm_borders(col = "black", lwd = 2) +
tm_shape(segment_results) +
tm_dots(
size = 0.6,
fill = "pct_sidewalk",
fill.scale = tm_scale(
values = c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c")
),
fill.legend = tm_legend(title = "Sidewalk (%)")
) +
tm_shape(tracts) +
tm_text("tract_label", size = 0.8)
# 2. Greenness percentage
tm_basemap("OpenStreetMap") +
tm_shape(tracts) +
tm_borders(col = "black", lwd = 2) +
tm_shape(segment_results) +
tm_dots(
size = 0.6,
fill = "pct_greenness",
fill.scale = tm_scale(
values = c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c")
),
fill.legend = tm_legend(title = "Greenness (%)")
) +
tm_shape(tracts) +
tm_text("tract_label", size = 0.8)
# 3. Building-to-street ratio
tm_basemap("OpenStreetMap") +
tm_shape(tracts) +
tm_borders(col = "black", lwd = 2) +
tm_shape(segment_results) +
tm_dots(
size = 0.6,
fill = "b2s_ratio",
fill.scale = tm_scale(
values = c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c")
),
fill.legend = tm_legend(title = "Building-to-street ratio")
) +
tm_shape(tracts) +
tm_text("tract_label", size = 0.8)
# Compare all six segment-level measures between tracts.
segment_results %>%
st_drop_geometry() %>%
pivot_longer(
cols = c(
b2s_ratio, pct_building, pct_greenness,
pct_road, pct_sidewalk, pct_sky
),
names_to = "variable",
values_to = "value"
) %>%
ggplot(aes(x = tract_label, y = value)) +
geom_boxplot(fill = "#c7ffd6") +
theme_bw() +
facet_wrap(
~ variable,
scales = "free_y",
nrow = 3,
labeller = as_labeller(c(
b2s_ratio = "Building-to-street ratio",
pct_building = "Building (%)",
pct_greenness = "Greenness (%)",
pct_road = "Road (%)",
pct_sidewalk = "Sidewalk (%)",
pct_sky = "Sky (%)"
))
) +
labs(
x = "Tract",
y = "Image percentage / building-to-street ratio"
) +
theme(
axis.text.x = element_text(angle = 25, hjust = 1)
)
# Display sampled and included segment counts and mean measures by tract.
# Count sampled segments in each tract.
sampled_counts <- edges %>%
st_drop_geometry() %>%
count(GEOID, tract_label, name = "sampled_segments")
# Count included segments and calculate mean measures.
included_summary <- segment_results %>%
st_drop_geometry() %>%
group_by(GEOID, tract_label) %>%
summarise(
included_segments = n(),
across(
c(
pct_building, pct_sky, pct_road,
pct_sidewalk, pct_greenness, b2s_ratio
),
mean
),
.groups = "drop"
)
# Combine counts and means into the required table.
tract_summary <- sampled_counts %>%
left_join(included_summary, by = c("GEOID", "tract_label")) %>%
mutate(included_segments = coalesce(included_segments, 0L))
tract_summary
## # A tibble: 2 × 10
## GEOID tract_label sampled_segments included_segments pct_building pct_sky
## <chr> <chr> <int> <int> <dbl> <dbl>
## 1 131210096… Less walka… 22 21 9.00 20.1
## 2 131210096… More walka… 58 57 3.83 14.1
## # ℹ 4 more variables: pct_road <dbl>, pct_sidewalk <dbl>, pct_greenness <dbl>,
## # b2s_ratio <dbl>