knitr::opts_chunk$set(message = FALSE, warning = FALSE, fig.align = "center")
# Load the packages used in your analysis.
library(tidyverse)
library(magrittr)
library(sf)
library(tmap)
library(osmdata)
library(sfnetworks)
library(tidygraph)
library(tidycensus)
library(knitr)
library(tigris)
library(ggpubr)
library(kableExtra)
library(jsonlite)
library(ggrepel)
library(leafsync)
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.
# Assign tract labels and transform to local_crs for spatial calculations.
tracts <- tracts(
state = "GA",
county = c("Fulton", "DeKalb"),
year = 2024,
cb = TRUE
) |>
filter(GEOID %in% c("13121001102", "13089022203")) |>
select(GEOID) |>
mutate(
tract_label = if_else(GEOID == "13121001102", "More walkable", "Less walkable"))
tracts <- tracts |>
st_transform(local_crs)
Briefly explain your choices and what streetscape differences you expect to find (no more than 100 words).
Your explanation:
I selected these two tracts based on my personal experience. The more walkable tract selected is in Midtown Atlanta between 10th and 12th streets and adjacent to Piedmont Park. It is close to my home and where a lot of my friends live. The area is mostly on a grid, and has seen significant investment in walkability, lane reduction, and the addition of bike lanes in the past five years. While there are still some narrow sidewalks and sparse parking lots, I expect to see elements of higher walkability overall.
The less walkable tract I selected is in Decatur, close to the Avondale and Scottsdale city limits. It is where my boyfriend lives. I have spent a lot of time in this area and know from personal experience that it is a highly car-dependent, suburban environment with barely used, narrow sidewalks. I do expect to see more greenery reflected in the visual output as it is a fairly green area.
Overall, I expect to see a significant difference in the results, particularly in terms of sidewalks and building-to-street ratios.
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.
#Creating more walkable tract
road_network_more <- tracts |>
filter(tract_label == "More walkable")|>
st_transform(4326) |>
st_bbox() |>
opq() |>
add_osm_feature(key = "highway",
value = c("primary", "secondary", "tertiary", "residential")) |>
osmdata_sf() |>
osm_poly2line()
#Creating less walkable tract
road_network_less <- tracts |>
filter(tract_label == "Less walkable")|>
st_transform(4326) |>
st_bbox() |>
opq() |>
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.
roads_more <- road_network_more$osm_lines |>
st_transform(local_crs) |>
select(osm_id, highway) |>
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") |>
mutate(length = as.numeric(st_length(geometry)))
roads_less <- road_network_less$osm_lines |>
st_transform(local_crs) |>
select(osm_id, highway) |>
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") |>
mutate(length = as.numeric(st_length(geometry)))
segments_more <- roads_more |>
st_as_sf() |>
mutate(midpoint =
st_line_sample(geometry, sample = 0.5) |>
st_cast("POINT")
) |>
filter(length >= 91.44) |>
st_set_geometry("midpoint") |>
st_join(filter(tracts, tract_label == "More walkable"), join = st_within, left = FALSE) |>
st_set_geometry("geometry")
segments_less <- roads_less |>
st_as_sf() |>
mutate(midpoint =
st_line_sample(geometry, sample = 0.5) |>
st_cast("POINT")
) |>
filter(length >= 91.44) |>
st_set_geometry("midpoint") |>
st_join(filter(tracts, tract_label == "Less walkable"), join = st_within, left = FALSE) |>
st_set_geometry("geometry")
# Combine the samples and assign unique edge IDs.
combined_roads <- bind_rows(segments_more, segments_less) |>
select('tract_label', 'GEOID', 'highway', 'length') |>
st_transform(local_crs) |>
rowid_to_column(var = "edge_id")
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) {
mid_p3 <- line %>%
st_line_sample(sample = c(0.48, 0.5, 0.52)) %>%
st_cast("POINT") %>%
st_coordinates()
mid_p <- mid_p3[2,]
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
mid_heading_1 <- mid_heading_1 %% 360
mid_heading_2 <- mid_heading_2 %% 360
return(tribble(
~type, ~X, ~Y, ~heading,
"mid1", mid_p["X"], mid_p["Y"], mid_heading_1,
"mid2", mid_p["X"], mid_p["Y"], mid_heading_2))
}
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 <- combined_roads |>
st_geometry() |>
map_df(getHeading)
# Transform the points, update longitude/latitude, and assign image IDs.
edges_heading <- edges_heading |>
bind_cols(
combined_roads |>
st_drop_geometry() |>
slice(rep(1:nrow(combined_roads), each=2))
) |>
rowid_to_column(var = "img_id") |>
st_as_sf(coords = c("X", "Y"), crs = local_crs) |>
st_transform(4326) |>
mutate(
X = st_coordinates(geometry)[, "X"],
Y = st_coordinates(geometry)[, "Y"]
) |>
select('edge_id', 'img_id', 'tract_label', 'GEOID', 'highway', 'type', 'X', 'Y', 'heading')
edges <- combined_roads
edges_heading
NOTE: As both tracts contain less than 100 segments, all segments are used in the analysis.
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("Esri.WorldGrayCanvas") +
tm_shape(tracts) +
tm_polygons(
fill = "tract_label",
fill.scale = tm_scale_categorical(
values = c("More walkable" = "#2A9D8F",
"Less walkable" = "#E76F51")
),
fill.legend = tm_legend(title = "Walkability Label"),
col = "black",
lwd = 2,
fill_alpha = 0.5,
popup.vars = c(
"Tract" = "GEOID",
"Walkability" = "tract_label"
)
) +
tm_text(
text = "tract_label",
size = 0.8,
col = "black"
) +
tm_view(bbox = st_bbox(tracts))
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
location <- paste0(iterrow$Y %>% round(5), ",", iterrow$X %>% round(5))
heading <- iterrow$heading %>% round(1)
edge_id <- iterrow$edge_id
img_id <- iterrow$img_id
highway <- iterrow$highway
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 <- seg_output |>
left_join(edges_heading, by = "img_id")
total_pixels <- (768*768)
edges_seg_output <- 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 = building / street_pixels,
b2s_ratio = if_else(street_pixels == 0 | b2s_ratio > 4, NA_real_, b2s_ratio))
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.
valid_segments <- edges_seg_output |>
st_drop_geometry() |>
group_by(edge_id) |>
filter(
n() == 2,
all(c("mid1", "mid2") %in% type),
all(if_all(all_of(c(
"pct_building", "pct_sky", "pct_road","pct_sidewalk", "pct_greenness", "b2s_ratio")), is.finite))
) |>
ungroup()
# Average the two views within each segment and restore one midpoint geometry.
segment_results <- valid_segments |>
group_by(edge_id) |>
summarize(
GEOID = first(GEOID),
tract_label = first(tract_label),
highway = first(highway),
pct_building = mean(pct_building),
pct_sky= mean(pct_sky),
pct_road= mean(pct_road),
pct_sidewalk= mean(pct_sidewalk),
pct_greenness= mean(pct_greenness),
b2s_ratio= mean(b2s_ratio),
.groups = "drop"
)
segment_results <- segment_results |>
left_join((edges_seg_output |> select(edge_id, geometry)),
by = "edge_id", multiple = "first") |>
st_as_sf()
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.
street_geom <- segment_results |>
st_drop_geometry() |>
left_join(edges |> select(edge_id, geometry), by = "edge_id") |>
st_as_sf()
tmap_mode("view")
make_map <- function(tract_type, measure, palette, legend_title, show_legend = FALSE) {
tract_bbox <- tracts |>
filter(tract_label == tract_type) |>
st_bbox()
measure_max <- max(segment_results[[measure]], na.rm = TRUE)
tm_shape(tracts |> filter(tract_label == tract_type)) +
tm_polygons(
fill = "#CBD5E1", fill_alpha = 0.3,
col = "#334155", lwd = 2,
popup.vars = c("Tract" = "GEOID",
"Walkability" = "tract_label")
) +
tm_text(
text = "tract_label",
size = 0.7,
col = "#334155"
) +
tm_shape(street_geom |> filter(tract_label == tract_type)) +
tm_lines(col = "white", lwd = 4) +
tm_lines(
col = measure,
lwd = 3,
col.scale = tm_scale_continuous(
values = palette,
limits = c(0, measure_max)
),
col.legend = tm_legend_hide(),
popup.vars = setNames(
c("edge_id", "highway", measure),
c("Edge ID", "Road Class", legend_title)
)
) +
tm_shape(segment_results |> filter(tract_label == tract_type)) +
tm_dots(
fill = measure,
size = 0.5,
col = "black",
lwd = 1,
fill.scale = tm_scale_continuous(
values = palette,
limits = c(0, measure_max)
),
fill.legend = if (show_legend) {
tm_legend(title = legend_title)
} else {
tm_legend_hide()
},
popup.vars = setNames(
c("edge_id", "highway", measure),
c("Edge ID", "Road Class", legend_title)
)
) +
tm_basemap("OpenStreetMap.HOT") +
tm_view(bbox = tract_bbox)
}
# Row 1: Sidewalk percentage
map_sidewalk_more <- make_map(
"More walkable", "pct_sidewalk", "Blues", "Sidewalk (%)"
)
map_sidewalk_less <- make_map(
"Less walkable", "pct_sidewalk", "Blues", "Sidewalk (%)", TRUE
)
# Row 2: Greenness percentage
map_green_more <- make_map(
"More walkable", "pct_greenness", "Greens", "Greenness (%)"
)
map_green_less <- make_map(
"Less walkable", "pct_greenness", "Greens", "Greenness (%)", TRUE
)
# Row 3: Building-to-street ratio
map_b2s_more <- make_map(
"More walkable", "b2s_ratio", "Purples", "Building-to-Street Ratio"
)
map_b2s_less <- make_map(
"Less walkable", "b2s_ratio", "Purples", "Building-to-Street Ratio", TRUE
)
latticeView(
tmap_leaflet(map_sidewalk_more),
tmap_leaflet(map_sidewalk_less),
tmap_leaflet(map_green_more),
tmap_leaflet(map_green_less),
tmap_leaflet(map_b2s_more),
tmap_leaflet(map_b2s_less),
ncol = 2,
sync = list(c(1, 3, 5), c(2, 4, 6)),
no.initial.sync = TRUE
)
# Compare all six segment-level measures between tracts.
boxplot_data <- segment_results |>
st_drop_geometry() |>
select(tract_label, pct_building, pct_sky, pct_road,
pct_sidewalk, pct_greenness, b2s_ratio) |>
pivot_longer(
cols = -tract_label,
names_to = "measure",
values_to = "value"
) |>
mutate(
measure = factor(
measure,
levels = c("pct_building", "pct_sky", "pct_road",
"pct_sidewalk", "pct_greenness", "b2s_ratio"),
labels = c("Building (%)", "Sky (%)", "Road (%)",
"Sidewalk (%)", "Greenness (%)",
"Building-to-Street Ratio")
)
)
ggplot(boxplot_data, aes(x = tract_label, y = value, fill = tract_label)) +
geom_boxplot(width = 0.6, alpha = 0.8, outlier.alpha = 0.6) +
facet_wrap(~ measure, scales = "free_y", ncol = 2) +
scale_fill_manual(
values = c("More walkable" = "#2A9D8F",
"Less walkable" = "#E76F51")
) +
labs(
title = "Streetscape Characteristics by Tract",
subtitle = "Distribution of segment-level measures",
x = NULL,
y = NULL
) +
theme_minimal(base_size = 12) +
theme(
legend.position = "none",
strip.text = element_text(face = "bold", size = 11),
axis.text.x = element_text(size = 9),
panel.grid.major.x = element_blank(),
plot.title = element_text(face = "bold")
)
# Display sampled and included segment counts and mean measures by tract.
# Calculate sampled segment counts.
sample_counts <- edges |>
st_drop_geometry() |>
count(GEOID, tract_label, name = "sampled_segments")
# Calculate included segment counts and 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,
.names = "mean_{.col}"
),
.groups = "drop"
)
# Combine into one summary table.
tract_summary <- sample_counts |>
left_join(included_summary, by = c("GEOID", "tract_label")) |>
mutate(included_segments = replace_na(included_segments, 0L))
# Display the table.
tract_summary |>
kable(
digits = 2,
caption = "Summary of Streetscape Measures by Tract",
col.names = c(
"GEOID", "Tract", "Sampled", "Included",
"Building (%)", "Sky (%)", "Road (%)",
"Sidewalk (%)", "Greenness (%)", "Building-to-Street Ratio"
)
) |>
kable_styling(
bootstrap_options = c("striped", "hover", "condensed"),
full_width = FALSE
)
| GEOID | Tract | Sampled | Included | Building (%) | Sky (%) | Road (%) | Sidewalk (%) | Greenness (%) | Building-to-Street Ratio |
|---|---|---|---|---|---|---|---|---|---|
| 13089022203 | Less walkable | 73 | 72 | 3.09 | 23.23 | 34.94 | 1.77 | 34.26 | 0.09 |
| 13121001102 | More walkable | 20 | 20 | 16.79 | 11.15 | 35.91 | 3.55 | 26.58 | 0.40 |