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 of 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.

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 objects, (2) compare the mean of each category between the two Census Tract and (3) draw boxplots to compare the distributions.

Section 1. Choose your Census Tracts.

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

#This walkability study will compare Census Tract 234.23 (Dekalb) & 13.00 (Fulton). Census Tract 234.23 (Dekalb) is located in Decatur, GA. I lived in this area a few years back and I recall that we had little access to public parks and grocery stores, it is near the highway and within an area of the city that is underesourced. Census tract 13.00 is my chosen walkable location. It is near a Piedmont Park and not too far from Midtown MARTA station. This area of the city is also close to the Beltline. I consider this area to have more pedestrian friendly infrastructure.

Section 2. OSM, GSV, and computer vision.

Fill out the template to complete the script.

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

Step 1. Get OSM data and clean it.

The getbb() function, which we used in the class material to download OSM data, isn’t suitable for downloading just two Census Tracts. We will instead use an alternative method.

  1. Using tidycensus package, download the Census Tract polygon for Fulton and DeKalb counties.
  2. Extract two Census Tracts, each of which will be your most walkable 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_KEY"))
## 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%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  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.

# For the walkable Census Tract(s) 
# 1. 
tr1_ID <- c(13121001301)

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

# For the unwalkable Census Tract(s)  
# 1.
tr2_ID <-  c(13089023108)

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

# //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 %>% 
  sfnetworks::as_sfnetwork(directed = FALSE) %>%
  activate("edges") %>%
  filter(!edge_is_multiple()) %>%
  filter(!edge_is_loop()) %>%
  convert(., sfnetworks::to_spatial_subdivision) %>%
  convert(., sfnetworks::to_spatial_smooth) %>%
  mutate(length = edge_length())
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
net2 <- osm_2$osm_lines %>% 
 sfnetworks::as_sfnetwork(directed = FALSE) %>%
  activate("edges") %>%
  filter(!edge_is_multiple()) %>%
  filter(!edge_is_loop()) %>%
  convert(., sfnetworks::to_spatial_subdivision) %>%
  convert(., sfnetworks::to_spatial_smooth) %>%
  mutate(length = edge_length())
## Warning: to_spatial_subdivision assumes attributes are constant over geometries
# //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 > 50) %>%
  # 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 > 50) %>%
  # 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.

get_azi <- 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
    
    
  
  # 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() %>% 
  .[(nrow(.)-1):nrow(.),1:2]
    
  end_azi <- atan2(end_p[2,"X"] - end_p[1, "X"],
                 end_p[2,"Y"] - end_p[1, "Y"])*180/pi

    
  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 0.45 and 0.55 locations. These two points will be used to calculate the azimuth.
  # 2. Use st_case() function to convert 'MULTIPOINT' object to 'POINT' object.
  # 3. Extract coordinates using st_coordinates().
  # 4. Use atan2() functino to Calculate azimuth.
  # 5. Use st_line_sample() again to generate a point at 0.5 location and get its coordinates. This point will be the location at which GSV image will be downloaded.
  
  mid_p <- line %>% 
    st_geometry() %>% 
  .[[1]] %>% 
  st_line_sample(sample = c(0.45, 0.55)) %>% 
  st_cast("POINT") %>% 
  st_coordinates()

  
  mid_azi <- atan2(mid_p[2,"X"] - mid_p[1, "X"],
                 mid_p[2,"Y"] - mid_p[1, "Y"])*180/pi

  
  mid_p <- line  %>% 
    st_geometry() %>% 
  .[[1]] %>% 
  st_line_sample(sample = 0.5) %>% 
  st_coordinates() %>% 
  .[1,1:2]
  # //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"],   ifelse(mid_azi < 180, mid_azi + 180, mid_azi - 180),
    "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 get_azi() 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 get_azi() function to all edges.
# Remember that you need to pass edges object to st_geometry() 
# before you apply get_azi()
endp_azi <- edges %>% 
  st_geometry() %>% 
  map_df(get_azi) 
# //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){ #type = iterrow\(type #location <- paste0(iterrow\)Y %>% round(4), “,”, iterrow\(X %>% round(4)) #heading <- iterrow\)azi %>% round(1) #edge_id <- iterrow\(edge_id #node_id <- iterrow\)node_id #highway <- iterrow$highway #key <- google_api

#furl <- glue::glue(“https://maps.googleapis.com/maps/api/streetview?size=640x640&location={location}&heading={heading}&fov=90&pitch=0&key={key}”) #fname <- glue::glue(“GSV-nid_{node_id}-eid_{edge_id}-type_{type}-Location_{location}-heading_{heading}.jpg”) #fpath <- here(“C:/Users/CP8883/CP8883/Major2/LMDVMajor2”, fname) #download.file(furl, fpath, mode = ‘wb’)

# //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 ========================================

Step 6. Apply computer vision

Now, you need to upload the images you downloaded to Google Drive. You should upload the images to the same folder that we used in class - the ‘demo_images’ folder in the root directory of Google Drive. Then, use Google Colab to apply a semantic segmentation model called Pyramid Scene Parsing Network.

Step 7. Merging the processed data back to R

Once all of the images are processed and saved in your Google Drive as a CSV file, download the CSV file and merge it back to edges.

# Read the downloaded CSV file from Google Drive
pspnet <- read.csv(here("seg_output.csv"))




# =========== NO MODIFICATION ZONE STARTS HERE ===============================
# Join the pspnet object back to endp object using node_id as the join key.
pspnet_nodes <- endp %>% inner_join(pspnet, by="node_id") %>% 
  select(type, X, Y, node_id, building, sky, tree, road, sidewalk,is_walkable) %>% 
  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.

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

You need to create maps of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. In total, you will have 10 maps (two Census Tracts times five categories).

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.

propmean1 <- pspnet_nodes %>% 
  mutate(pct_building = building*100) %>% 
  mutate(pct_sky = sky*100) %>% 
  mutate(pct_tree = tree*100) %>% 
  mutate(pct_road = road*100) %>% 
  mutate(pct_sidewalk = sidewalk*100) 

propmean2 <- propmean1[ , c(
"pct_building"
,
"pct_sky"
,
"pct_tree"
,
"pct_road"
,
"pct_sidewalk",

"is_walkable"
) ]
walk.filter <- propmean2$is_walkable %in% c("walkable")
head(walk.filter)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
walk.filter1 <- propmean2[walk.filter, ]

head(walk.filter1)
## Simple feature collection with 6 features and 6 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -84.37366 ymin: 33.76398 xmax: -84.36828 ymax: 33.77812
## Geodetic CRS:  WGS 84
## # A tibble: 6 × 7
##   pct_building pct_sky pct_t…¹ pct_r…² pct_s…³ is_wa…⁴             geometry
##          <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>            <POINT [°]>
## 1        18.7     31.6    6.89    30.0    6.39 walkab… (-84.36986 33.77812)
## 2         6.56    28.6   17.7     32.7    1.74 walkab… (-84.36907 33.77812)
## 3         4.06    29.7   16.3     39.9    3.01 walkab… (-84.36907 33.77812)
## 4         9.97    31.6   17.0     20.3    2.81 walkab… (-84.36829 33.77812)
## 5         9.03    22.9   20.4     26.0   11.6  walkab… (-84.37366 33.76398)
## 6         8.66    36.8    8.43    35.0    1.32 walkab…  (-84.3731 33.76855)
## # … with abbreviated variable names ¹​pct_tree, ²​pct_road, ³​pct_sidewalk,
## #   ⁴​is_walkable
unwalk.filter <-propmean2$is_walkable %in% c("unwalkable")
head(unwalk.filter)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
unwalk.filter1 <- propmean2[unwalk.filter, ]

head(unwalk.filter1)
## Simple feature collection with 6 features and 6 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -84.24305 ymin: 33.73933 xmax: -84.24109 ymax: 33.74148
## Geodetic CRS:  WGS 84
## # A tibble: 6 × 7
##   pct_building pct_sky pct_t…¹ pct_r…² pct_s…³ is_wa…⁴             geometry
##          <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>            <POINT [°]>
## 1       1.09      9.98    43.1    20.8   0     unwalk… (-84.24109 33.74125)
## 2       1.66     23.1     31.1    26.0   0     unwalk… (-84.24236 33.74148)
## 3       1.58      7.79    36.3    35.1   0     unwalk… (-84.24236 33.74148)
## 4       0.689     9.48    39.5    28.7   0     unwalk… (-84.24305 33.74037)
## 5       0.0864   18.5     35.5    25.3   0.514 unwalk… (-84.24305 33.74037)
## 6       0         8.21    45.5    38.3   2.64  unwalk… (-84.24287 33.73933)
## # … with abbreviated variable names ¹​pct_tree, ²​pct_road, ³​pct_sidewalk,
## #   ⁴​is_walkable
tmap_mode("view")
## tmap mode set to interactive viewing
compwalk <- tm_basemap("OpenStreetMap") +
  tm_shape(pspnet_nodes) + 
  tm_dots(col = "is_walkable", style = "fixed", palette= "Set1")

compwalk
#Comparing Walkability in Census Tract 13.00 (Fulton) vs. Census Tract 234.23 (Dekalb).
treemap <- tm_basemap("OpenStreetMap") +
  tm_shape(pspnet_nodes %>% 
           mutate(pct_tree = tree*100)) + 
  tm_dots(col = "pct_tree", style="jenks", palette="Greens" )

treemap
#Comparing % Tree Coverage in Census Tract 13.00 (Fulton) vs. Census Tract 234.23 (Dekalb).
sky <- tm_basemap("OpenStreetMap") +
  tm_shape(pspnet_nodes %>% 
           mutate(pct_sky = sky*100)) + 
  tm_dots(col = "pct_sky", style="quantile", palette="Blues" )

sky
#Comparing percent Sky in Images From Census Tract 13.00 (Fulton) & Census Tract 234.23 (Dekalb).
build<- tm_basemap("OpenStreetMap") +
  tm_shape(pspnet_nodes %>% 
           mutate(pct_building = building*100)) + 
  tm_dots(col = "pct_building", style="quantile", palette="BuPu" )

build
#Comparing Percent of Buildings in Census Tract 13.00 (Fulton) vs. Census Tract 234.23 (Dekalb).
road<- tm_basemap("OpenStreetMap") +
  tm_shape(pspnet_nodes %>% 
           mutate(pct_road = road*100)) + 
  tm_dots(col = "pct_road", style="quantile", palette= "RdPu")

road
#Comparing Percent of Roads in Census Tract 13.00 (Fulton) vs. Census Tract 234.23 (Dekalb).
sidewlk<- tm_basemap("OpenStreetMap") +
  tm_shape(pspnet_nodes %>% 
           mutate(pct_sidewalk = sidewalk*100)) + 
  tm_dots(col = "pct_sidewalk", style="jenks", palette="Reds" )

sidewlk
#Comparing Percent of Sidewalk in Census Tract 13.00 (Fulton) vs. Census Tract 234.23 (Dekalb).
##As stated previously, this analysis compares Census Tract 13.00 (Fulton)  vs. Census Tract 234.23 (Dekalb). 
##H0: There is no diference in walkability between Census Tract 13.00 (Fulton) and Census Tract 234.23 (Dekalb). 
##HA: Census Tract 13.00 (Fulton) is proportionally more walkable than Census Tract 234.23 (Dekalb). Census tract 13.00 was labeled as walkable, ##while Census Tract 234.23 was labeled as unwalkable. This is shown in Map #1. A total 1740 images from Google Street View were run through computer ##vision analysis to test this hypothesis. 


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


### Analysis 2 - Compare the means.

#You need to calculate the mean of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. For example, you need to calculate the mean of building category for each of walkable and unwalkable Census Tracts. Then, you need to calculate the mean of sky category for each of walkable and unwalkable Census Tracts. In total, you will have 10 mean values. **<font color=pink> After the calculation, provide a brief description of your findings. </font>**

##Calculating mean for "walkable" census tract 13:00 Fulton 
  
walk.filter2 <- walk.filter1[ , c("pct_building","pct_sky","pct_tree","pct_road","pct_sidewalk") ]

  
cat("The summary() of the walkable data frame is", "\n")
## The summary() of the walkable data frame is
summary(walk.filter2)
##   pct_building         pct_sky          pct_tree          pct_road     
##  Min.   : 0.00146   Min.   : 2.501   Min.   : 0.9072   Min.   : 2.254  
##  1st Qu.: 3.70612   1st Qu.:23.252   1st Qu.:11.5082   1st Qu.:31.733  
##  Median : 5.66357   Median :28.388   Median :17.2927   Median :35.237  
##  Mean   : 6.50171   Mean   :27.664   Mean   :17.9971   Mean   :34.415  
##  3rd Qu.: 8.57452   3rd Qu.:32.595   3rd Qu.:23.2232   3rd Qu.:38.665  
##  Max.   :32.90723   Max.   :43.401   Max.   :50.5591   Max.   :49.311  
##   pct_sidewalk             geometry  
##  Min.   : 0.000   POINT        :340  
##  1st Qu.: 0.923   epsg:4326    :  0  
##  Median : 2.087   +proj=long...:  0  
##  Mean   : 2.983                      
##  3rd Qu.: 4.052                      
##  Max.   :26.462
##Calculating mean for "unwalkable" census tract  Census Tract 234.23 (Dekalb)

unwalk.filter2 <- unwalk.filter1[ , c("pct_building","pct_sky","pct_tree","pct_road","pct_sidewalk") ]

  
cat("The summary() of the unwalkable data frame is", "\n")
## The summary() of the unwalkable data frame is
summary(unwalk.filter2)
##   pct_building        pct_sky         pct_tree        pct_road    
##  Min.   : 0.0000   Min.   : 0.00   Min.   : 0.00   Min.   : 0.00  
##  1st Qu.: 0.2776   1st Qu.:18.36   1st Qu.:15.88   1st Qu.:28.85  
##  Median : 1.2231   Median :25.90   Median :24.65   Median :33.23  
##  Mean   : 1.7214   Mean   :25.11   Mean   :24.56   Mean   :31.09  
##  3rd Qu.: 2.2640   3rd Qu.:32.28   3rd Qu.:33.34   3rd Qu.:37.19  
##  Max.   :28.0933   Max.   :96.24   Max.   :69.54   Max.   :47.61  
##   pct_sidewalk              geometry   
##  Min.   : 0.0000   POINT        :1400  
##  1st Qu.: 0.0000   epsg:4326    :   0  
##  Median : 0.0000   +proj=long...:   0  
##  Mean   : 0.5670                       
##  3rd Qu.: 0.2717                       
##  Max.   :29.2107

#The mean difference between the “walkable” and “unwalkable” census tracts is found in percent sidewalk, percent building, and percent tree. Proportionally the walkable census tract has more percent sidewalk (mean pct_sidewalk = 2.983 ) and buildings (mean pct_building = 6.50171 ):

#However, the overall tree coverage mean is greater in the “unwalkable” census tract (mean pct_tree = 24.56).

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

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

Analysis 3 - Draw boxplot

You need to calculate the mean of the proportion of building, sky, tree, road, and sidewalk for walkable and unwalkable areas. For example, you need to calculate the mean of building category for each of walkable and unwalkable Census Tracts. Then, you need to calculate the mean of sky category for each of walkable and unwalkable Census Tracts. In total, you will have 10 mean values. After the calculation, provide a brief description of your findings.

# TASK ////////////////////////////////////////////////////////////////////////
# Create boxplot(s) using geom_boxplot() function from ggplot2 package.
# You may find the code from mini-assignment 4 useful here.

# //TASK //////////////////////////////////////////////////////////////////////
boxwalkcomp<- propmean2 %>% 
  st_drop_geometry() %>% 
  pivot_longer(cols = pct_building:pct_sidewalk, names_to = "variable", values_to = "values") %>% 
  ggplot() +
  ggtitle("Comparing Proportion for All Variables", "Census Tract 234.23 (Dekalb) vs. Census Tract 13.00 (Fulton)" )+
  geom_boxplot(aes (x=variable, y=values))+
  facet_wrap(~is_walkable)

boxwalkcomp

#Conclusion: Overall, the two census tracts are more similar to one another than they are different. The walkable vs. unwalkable mean distributions are similar between both locations. Though, mean values for percent sidewalk and percent building are slightly larger in Census Tract 13.00 (Fulton County). Further, Census Tract 234.23 (Dekalb) tract has proportionally more tree coverage in comparison to census tract 13.00 (Fulton).

#Limitations: Overall the two census tracts are more similar to one another than they are different. This study is not exhaustive. Our data sets have outliers skewing the metrics. Perhaps, an ANOVA analysis would be useful in comparing walkability versus our other variables. It may be necessary to test different statistical models and account for control variables that relate to walkability in an urban setting.