Introduction to the assignment

This assignment consists of three main sections.

In the first section, you need to select one Census Tract that you think is the most walkable and another one that you think is least walkable within Fulton and DeKalb Counties, GA. As long as the two Census Tracts are within the two counties, you can pick any two you want. If the area you want to use as walkable/unwalkable area is not well-covered by one Census Tract, you can select multiple tracts (e.g., selecting three adjacent tracts as one walkable area). The definition of ‘walkable’ can be your own - you can choose solely based on your experience (e.g., had best/worst walking experience), refer to Walk Score, or any other mix of criteria you want. After you make the selection, provide a short write-up with a map explaining why you chose those Census Tracts.

The second section is the main part of this assignment in which you prepare OSM data, download GSV images, apply computer vision (i.e., semantic segmentation).

In the third section, you will summarise and analyze the output and provide your findings. After you apply computer vision to the images, you will have the number of pixels in each image that represent 150 categories in your data. You will focus on the following categories in your analysis: building, sky, tree, road, and sidewalk. Specifically, you will (1) create maps to visualize the spatial distribution of different elements, (2) compare the mean of each category between the two Census Tract and (3) draw box plots to compare the distributions.

library(tidyverse)
library(tidycensus)
library(osmdata)
library(sfnetworks)
library(units)
library(sf)
library(tidygraph)
library(tmap)
library(here)
ttm()

Section 1. Choose your Census Tracts.

Select walkable Census Tract(s) and unwalkable Census Tract(s) within Fulton and DeKalb counties.

In the quest to search for Census Tracts, you can use an approach similar to what we did in Step 1 of ‘Module4_getting_GSV_images.Rmd’. This time, instead of cities, we are focusing on Census Tracts; and the search boundary is the two counties, instead of metro Atlanta.

Provide a brief description and visualization of your Census Tracts. Why do you think the Census Tracts are walkable and unwalkable? What were the contributing factors?

##description The Census Tracts which I think it is unwalkable is the one whose GOEID equals 13121010402. It is the biggest one in my first sight at the southwest corner of Fulton county. The reason why I choose it as the unwalkable one is that we know Census Tracts are divided by population size. So, the population density at this Census Tract should be very low. Then, people maybe have to drive more to reach some places. If people only drive there and nobody walk. it should be unwalkable as well.

And I choose the No.13121004900 as the walkable one for the same reason. It is a Census Tract located near the midtown of Atlanta with a high density of population.

# get Census Tract of Fulton
tracts_fulton <- tigris::tracts(state = 'GA', county = 'Fulton', year = 2021)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |================                                                      |  24%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
# get Census Tract of DeKalb
tracts_dekalb <- tigris::tracts(state = 'GA', county = 'DeKalb', year = 2021)

# merge Census Tract of two counties
tracts_fulton_dekalb <- rbind(tracts_fulton, tracts_dekalb)

tracts_fulton_dekalb <- tracts_fulton_dekalb %>% mutate(temp_id = sample(1:n(), n()))

tm_shape(tracts_fulton_dekalb) + 
  tm_borders(lwd = 0.5) +
  tm_polygons(col = 'GEOID', palette = 'inferno', alpha = 0.5, lwd = 1.5) +
  tm_text('NAME') +
  tm_view(set.view = c(-84.37, 33.75, 10))
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
## Warning: Number of levels of the variable "GEOID" is 530, which is larger than
## max.categories (which is 30), so levels are combined. Set
## tmap_options(max.categories = 530) in the layer function to show all levels.

Section 2. OSM, GSV, and computer vision.

Fill out the template to complete the script.

Step 1. Get OSM data and clean it.

  1. Using tidycensus package, download the Census Tract polygon for Fulton and DeKalb counties.
  2. Extract two Census Tracts, which will be your most and least walkable Census Tracts.
  3. Using their bounding boxes, get OSM data.
  4. Convert them into sfnetworks data and clean it.
# TASK ////////////////////////////////////////////////////////////////////////
# 1. Set up your api key here
tidycensus::census_api_key(Sys.getenv("census_api"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
# //TASK //////////////////////////////////////////////////////////////////////



# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Download Census Tract polygon for Fulton and DeKalb
tract <- get_acs("tract", 
                 variables = c('tot_pop' = 'B01001_001'),
                 year = 2020, 
                 state = "GA", 
                 county = c("Fulton", "DeKalb"), 
                 geometry = TRUE)
## Getting data from the 2016-2020 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
# =========== NO MODIFY ZONE ENDS HERE ========================================



# TASK ////////////////////////////////////////////////////////////////////////
# The purpose of this TASK is to create one bounding box for walkable Census Tract and another bounding box for unwalkable Census Tract.
# As long as you generate what's needed for the subsequent codes, you are good. The numbered list of tasks below is to provide some hints.
# 1. Write the GEOID of walkable & unwalkable Census Tracts. e.g., tr1_ID <- c("13121001205", "13121001206")
# 2. Extract the selected Census Tracts using tr1_ID & tr2_ID
# 3. Create their bounding boxes using st_bbox(), and 
# 4. Assign them to tract_1_bb and tract_1_bb, respectively.
# 5. Change the coordinate system to GCS, if necessary.

# For the walkable Census Tract(s)
# 1. 
tr1_ID <- "13121002801" #13121004900

# 2~4
tract_1_bb <- tract %>% 
  filter(GEOID == tr1_ID) %>%
  st_bbox() %>%
  st_as_sfc()

# For the unwalkable Census Tract(s)  
# 1.
tr2_ID <- "13121010402"

# 2~4
tract_2_bb <- tract %>% 
  filter(GEOID == tr2_ID) %>%
  st_bbox() %>%
  st_as_sfc()

# 5
tract_1_bb <- st_transform(tract_1_bb, crs = 4326)
tract_2_bb <- st_transform(tract_2_bb, crs = 4326)
# //TASK //////////////////////////////////////////////////////////////////////

  
  
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Get OSM data for the two bounding box
osm_1 <- opq(bbox = tract_1_bb) %>%
  add_osm_feature(key = 'highway', 
                  value = c("motorway", "trunk", "primary", 
                            "secondary", "tertiary", "unclassified",
                            "residential")) %>%
  osmdata_sf() %>% 
  osm_poly2line()

osm_2 <- opq(bbox = tract_2_bb) %>%
  add_osm_feature(key = 'highway', 
                  value = c("motorway", "trunk", "primary", 
                            "secondary", "tertiary", "unclassified",
                            "residential")) %>%
  osmdata_sf() %>% 
  osm_poly2line()
# =========== NO MODIFY ZONE ENDS HERE ========================================



# TASK ////////////////////////////////////////////////////////////////////////
# 1. Convert osm_1 and osm_2 to sfnetworks objects (set directed = FALSE)
# 2. Clean the network by (1) deleting parallel lines and loops, (2) create missing nodes, and (3) remove pseudo nodes, 
# 3. Add a new column named length using edge_length() function.
net1 <- osm_1$osm_lines %>% 
  select(osm_id, highway) %>% 
  as_sfnetwork(directed = FALSE)

net2 <- osm_2$osm_lines %>%
  select(osm_id, highway) %>% 
  as_sfnetwork(directed = FALSE)

#(1) deleting parallel lines and loops
net1 <- net1 %>%
  activate("edges") %>%
  filter(!edge_is_multiple()) %>%
  filter(!edge_is_loop())%>% 
  mutate(length = edge_length())

#(2) create missing nodes
net1 <- convert(net1, sfnetworks::to_spatial_subdivision)
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
#(3) remove pseudo nodes
net1 <- convert(net1, sfnetworks::to_spatial_smooth)

net1 <- net1 %>% mutate(length = edge_length())


net2 <- net2 %>%
  activate("edges") %>%
  filter(!edge_is_multiple()) %>%
  filter(!edge_is_loop())%>% 
  mutate(length = edge_length())

net2 <- convert(net2, sfnetworks::to_spatial_subdivision)
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
net2 <- convert(net2, sfnetworks::to_spatial_smooth)
# //TASK //////////////////////////////////////////////////////////////////////
  
  
# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# OSM for the walkable part
edges_1 <- net1 %>% 
  # Extract 'edges'
  st_as_sf("edges") %>% 
  # Drop redundant columns 
  select(osm_id, highway, length) %>% 
  # Drop segments that are too short (100m)
  mutate(length = as.vector(length)) %>% 
  filter(length > 100) %>% 
  # Add a unique ID for each edge
  mutate(edge_id = seq(1,nrow(.)),
         is_walkable = "walkable")

# OSM for the unwalkable part
edges_2 <- net2 %>% 
  # Extract 'edges'
  st_as_sf("edges") %>% 
  # Drop redundant columns 
  select(osm_id, highway, length) %>% 
  # Drop segments that are too short (100m)
  mutate(length = as.vector(length)) %>% 
  filter(length > 100) %>% 
  # Add a unique ID for each edge
  mutate(edge_id = seq(1,nrow(.)),
         is_walkable = "unwalkable")

# Merge the two
edges <- bind_rows(edges_1, edges_2)
# =========== NO MODIFY ZONE ENDS HERE ========================================

Step 2. Define a function that performs Step 3.

getAzi <- function(line){
  # This function takes one edge (i.e., a street segment) as an input and
  # outputs a data frame with four points (start, mid1, mid2, and end) and their azimuth.
  
  
  
  # TASK ////////////////////////////////////////////////////////////////////////
  # 1. From `line` object, extract the coordinates using st_coordinates() and extract the first two rows.
  # 2. Use atan2() function to calculate the azimuth in degree. 
  #    Make sure to adjust the value such that 0 is north, 90 is east, 180 is south, and 270 is west.
  # 1
  start_p <- line %>%
    st_coordinates() %>%
    .[1:2,1:2]
  
  # 2
  start_azi <- atan2(start_p[2,"X"] - start_p[1, "X"],
                     start_p[2,"Y"] - start_p[1, "Y"])*180/pi
    # **YOUR CODE HERE..** --> For example, atan2()..
  # //TASK //////////////////////////////////////////////////////////////////////

    
    
  
  # TASK ////////////////////////////////////////////////////////////////////////
  # Repeat what you did above, but for last two rows (instead of the first two rows).
  # Remember to flip the azimuth so that the camera would be looking at the street that's being measured
  end_p <- line %>%
    st_coordinates() %>%
    .[1:2,1:2]
    
  end_azi <- atan2(end_p[2,"X"] - end_p[1, "X"],
                   end_p[2,"Y"] - end_p[1, "Y"])*180/pi
    # **YOUR CODE HERE..** --> For example, atan2()..
    
  end_azi <- if (end_azi < 180) {end_azi + 180} else {end_azi - 180}
  # //TASK //////////////////////////////////////////////////////////////////////
  
  
  
  
  # TASK ////////////////////////////////////////////////////////////////////////
  # 1. From `line` object, use st_line_sample() function to generate points at 45%, 50% and 55% locations. 
  # 2. Use st_cast() function to convert 'MULTIPOINT' object to 'POINT' object.
  # 3. Extract coordinates using st_coordinates().
  # 4. Use the 50% location to define `mid_p` object.
  # 5. Use the 45% and 55% points and atan2() function to calculate azimuth `mid_azi`.
  
  mid_p3 <- line %>%
    st_line_sample(sample = c(0.45, 0.5, 0.55)) %>% 
    st_cast("POINT") %>% 
    st_coordinates()
  
  mid_p <- mid_p3[2,]
  
  mid_azi <- atan2(mid_p3[3,"X"] - mid_p3[1, "X"],
                   mid_p3[3,"Y"] - mid_p3[1, "Y"])*180/pi# **YOUR CODE HERE..** For example, atan2()..
  
  mid_azi2 <- ifelse(mid_azi < 180, mid_azi + 180, mid_azi - 180)
  
  # //TASK //////////////////////////////////////////////////////////////////////
 
    
  
  # =========== NO MODIFICATION ZONE STARTS HERE ===============================
 return(tribble(
    ~type,    ~X,            ~Y,             ~azi,
    "start",   start_p[1,"X"], start_p[1,"Y"], start_azi,
    "mid1",    mid_p["X"],   mid_p["Y"],   mid_azi,
    "mid2",    mid_p["X"],   mid_p["Y"],   mid_azi2,
    "end",     end_p[2,"X"],   end_p[2,"Y"],   end_azi))
  # =========== NO MODIFY ZONE ENDS HERE ========================================

}

Step 3. Apply the function to all street segments

We can apply getAzi() function to the edges object. We finally append edges object to make use of the columns in edges object (e.g., is_walkable column). When you are finished with this code chunk, you will be ready to download GSV images.

# TASK ////////////////////////////////////////////////////////////////////////
# Apply getAzi() function to all edges.
# Remember that you need to pass edges object to st_geometry() before you apply getAzi()
endp_azi <- edges %>%
  st_geometry() %>% 
  map_df(getAzi, .progress = T)

# //TASK //////////////////////////////////////////////////////////////////////

# =========== NO MODIFICATION ZONE STARTS HERE ===============================
endp <- endp_azi %>% 
  bind_cols(edges %>% 
              st_drop_geometry() %>% 
              slice(rep(1:nrow(edges),each=4))) %>% 
  st_as_sf(coords = c("X", "Y"), crs = 4326, remove=FALSE) %>% 
  mutate(node_id = seq(1, nrow(.)))
# =========== NO MODIFY ZONE ENDS HERE ========================================

Step 4. Define a function that formats request URL and download images.

get_image <- function(iterrow){
  # This function takes one row of endp and downloads GSV image using the information from endp.
  
  # TASK ////////////////////////////////////////////////////////////////////////
  # Finish this function definition.
  # 1. Extract required information from the row of endp, including 
  #    type (i.e., start, mid1, mid2, end), location, heading, edge_id, node_id, source (i.e., outdoor vs. default) and key.
  # 2. Format the full URL and store it in furl.
  # 3. Format the full path (including the file name) of the image being downloaded and store it in fpath
  type <- iterrow$type# **YOUR CODE HERE..**
  location <- paste0(iterrow$Y %>% round(5), ",", iterrow$X %>% round(5))# **YOUR CODE HERE..**
  heading <- iterrow$azi %>% round(1)# **YOUR CODE HERE..**
  edge_id <- iterrow$edge_id# **YOUR CODE HERE..**
  node_id <- iterrow$node_id# **YOUR CODE HERE..**
  key <- Sys.getenv("google_api")# **YOUR CODE HERE..**
  
  furl <- glue::glue("https://maps.googleapis.com/maps/api/streetview?size=640x640&location={location}&heading={heading}&fov=90&pitch=0&key={key}")# **YOUR CODE HERE..**
  fname <- glue::glue("GSV-nid_{node_id}-eid_{edge_id}-type_{type}-Location_{location}-heading_{heading}.jpg") # Don't change this code for fname
  fpath <- here("major3", "downloaded_image", fname)
  # //TASK //////////////////////////////////////////////////////////////////////

  
  
  # =========== NO MODIFICATION ZONE STARTS HERE ===============================
  # Download images
  if (!file.exists(fpath)){
    download.file(furl, fpath, mode = 'wb') 
  }
  # =========== NO MODIFY ZONE ENDS HERE ========================================
}

Step 5. Download GSV images

Before you download GSV images, make sure the row number of endp is not too large! The row number of endp will be the number of GSV images you will be downloading. Before you download images, always double-check your Google Cloud Console’s Billing tab to make sure that you will not go above the free credit of $200 each month. The price is $7 per 1000 images.

# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Loop!
for (i in seq(1,nrow(endp))){
  get_image(endp[i,])
}
# =========== NO MODIFY ZONE ENDS HERE ========================================

ZIP THE DOWNLOADED IMAGES AND NAME IT ‘gsv_images.zip’ FOR STEP 6.

Step 6. Apply computer vision

Now, use Google Colab to apply the semantic segmentation model.

  1. Zip your images and upload the images to your Colab session.
  2. Apply the semantic segmentation model to all the images.
  3. Save the segmentation output as csv file and download it.

Step 7. Merging the processed data back to R

Merge the segmentation output to edges.

# Read the downloaded CSV file from Google Drive
seg_output <- read.csv("C:/Users/49765/Desktop/Urban Analytics/major3/seg_output.csv")



# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Join the segmentation result to endp object.
seg_output_nodes <- endp %>% inner_join(seg_output, by=c("node_id"="img_id")) %>% 
  select(type, X, Y, node_id, building, sky, tree, road, sidewalk) %>% 
  mutate(across(c(building, sky, tree, road, sidewalk), function(x) x/(640*640)))
# =========== NO MODIFY ZONE ENDS HERE ========================================

Section 3. Summarise and analyze the results.

At the beginning of this assignment, you defined one Census Tract as walkable and the other as unwalkable. The key to the following analysis is the comparison between walkable/unwalkable Census Tracts.

library(dplyr)

points_sf <- st_as_sf(seg_output_nodes, coords = c("X", "Y"))

# 判断每个点属于哪个边界
data_with_tract1 <- st_intersection(points_sf, tract_1_bb) %>%
  mutate(Tract = "Tract 1")
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
# 对于不属于 Tract 1 的点,再次判断是否属于 Tract 2
data_with_tract2 <- st_intersection(points_sf, tract_2_bb) %>%
  mutate(Tract = "Tract 2") 
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
data_with_tract <- bind_rows(data_with_tract1, data_with_tract2)

Analysis 1 - Create map(s) to visualize the spatial distribution of the streetscape.

Create maps of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. In total, you will have 10 maps.

Below the maps, provide a brief description of your findings from the maps.

# TASK ////////////////////////////////////////////////////////////////////////
# Create map(s) to visualize the `pspnet_nodes` objects. 
# As long as you can deliver the message clearly, you can use any format/package you want.

t1 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract1) + 
  tm_dots(col = "building", style="quantile", palette = 'viridis')

t2 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract2) + 
  tm_dots(col = "building", style="quantile", palette = 'viridis')

t3 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract1) + 
  tm_dots(col = "sky", style="quantile", palette = 'viridis')

t4 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract2) + 
  tm_dots(col = "sky", style="quantile", palette = 'viridis')

t5 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract1) + 
  tm_dots(col = "tree", style="quantile", palette = 'viridis')

t6 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract2) + 
  tm_dots(col = "tree", style="quantile", palette = 'viridis')

t7 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract1) + 
  tm_dots(col = "road", style="quantile", palette = 'viridis')

t8 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract2) + 
  tm_dots(col = "road", style="quantile", palette = 'viridis')

t9 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract1) + 
  tm_dots(col = "sidewalk", style="quantile", palette = 'viridis')

t10 <- tm_basemap("OpenStreetMap")+
  tm_shape(data_with_tract2) + 
  tm_dots(col = "sidewalk", style="quantile", palette = 'viridis')

tmap_arrange(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, ncol = 2)  
# //TASK //////////////////////////////////////////////////////////////////////

Analysis 2 - Compare the means.

Calculate the mean of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. In total, you will have 10 mean values.

After the calculation, provide a brief description of your findings. The main components of the roads in the Unwalkable area are road, sky, and tree, indicating that the area is empty and the roads are all lanes, so pedestrians have no way to rest on the side of the road. The Walkable area is composed of buildings and roads and a sizable sidewalk, and I think the sidewalks on both sides of the road are smaller in the picture due to the way the Street View picture was taken and the angle of view. Based on this assumption, I can conclude that the walkable area has a large number of driveways, but also has buildings for pedestrians to rest and sizable sidewalks.

# TASK ////////////////////////////////////////////////////////////////////////
# Perform the calculation as described above.
# As long as you can deliver the message clearly, you can use any format/package you want.

library(ggplot2)


subset_data1 <- data_with_tract1[, c("building", "sky", "tree", "road", "sidewalk")]
subset_data1 <- st_drop_geometry(subset_data1)
mean_Tract1 <- colMeans(subset_data1)

subset_data2 <- data_with_tract2[, c("building", "sky", "tree", "road", "sidewalk")]
subset_data2 <- st_drop_geometry(subset_data2)
mean_Tract2 <- colMeans(subset_data2)


# 合并平均值数据
mean_data <- data.frame(
  Category = c("building", "sky", "tree", "road", "sidewalk"),
  Tract1 = mean_Tract1,
  Tract2 = mean_Tract2
)

# 使用 ggplot2 创建柱状图
ggplot(mean_data, aes(x = Category)) +
  geom_bar(aes(y = Tract1, fill = "Walkable"), stat = "identity", position = "dodge", width = 0.4) +
  geom_bar(aes(y = Tract2, fill = "Unwalkable"), stat = "identity", position = "dodge", width = 0.4) +
  labs(title = "Comparison of Mean Values",
       y = "Mean Value",
       fill = "") +  # 将图例标签设为空字符串
  scale_fill_manual(values = c("Walkable" = "blue", "Unwalkable" = "red")) +
  theme_minimal() +
  theme(legend.position = "right")

# //TASK //////////////////////////////////////////////////////////////////////

Analysis 3 - Draw boxplots.

Draw box plots comparing the proportion of building, sky, tree, road, and sidewalk between walkable and unwalkable areas. Each plot presents two boxes: one for walkable areas and the other for unwalkable areas. In total, you will have 5 plots.

After the calculation, provide a brief description of your findings. Tract1, Building has a relatively high percentage of Building, which may indicate that there are more built structures in walkable areas, possibly commercial buildings, residential areas. Sidewalk has a high percentage, which is reasonable because walkable areas typically require more sidewalks to support walkers.

Tract2 , Road has a relatively low percentage, which may indicate that the unwalkable area has relatively fewer transportation structures, possibly more areas are suburban and have less human activity.Sky and Tree proportions are relatively high, which may be due to the fact that the UNWALKABLE area may contain more natural landscaping and open space, resulting in an increased proportion of sky and trees.

# TASK ////////////////////////////////////////////////////////////////////////
# Create box plot(s) using geom_boxplot() function from ggplot2 package.
# Use `seg_output_nodes` object to draw the box plots.
# You will find `pivot_longer` function useful.
# Melt the data for better plotting

tract_type <- data_with_tract %>% 
  st_drop_geometry() %>% 
   mutate(Tract = as.factor(Tract),
         Tract = factor(Tract, levels = c('Tract 1', 'Tract 2'))) %>%
  group_by(Tract) %>% 
  summarise(across(c("building", "sky", "tree", "road", "sidewalk"), ~ round(mean(.x), 4)))

tract_type %>% 
  pivot_longer(cols = c(building, sky, tree, road, sidewalk), 
               names_to = 'variable', 
               values_to = "value") %>% 
  ggplot() +
  geom_boxplot(mapping = aes(x = Tract, y = value)) +
  theme_bw() +
  facet_wrap(~variable, scales = "free_y")

# //TASK //////////////////////////////////////////////////////////////////////