library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(tidycensus)
census_api_key(Sys.getenv("census_api"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
crime.data <- read.csv("Crime_Data.csv")
crime.data %>% head()
##   OBJECTID Report.Number            Report.Date    Offense.Start.Date
## 1    41377     220278026  10/26/2022 4:09:13 PM                      
## 2    52111     221050835  10/28/2022 5:38:34 AM  4/15/2022 3:00:00 AM
## 3    52683     221320696  10/28/2022 1:08:07 PM  5/12/2022 2:29:00 PM
## 4    56853     221671725  10/24/2022 7:00:18 PM  6/16/2022 2:54:00 AM
## 5    59006     221681279 10/27/2022 10:24:22 PM  6/17/2022 6:40:00 PM
## 6    59991     221921099  10/12/2022 2:18:45 AM 10/11/2022 4:00:00 AM
##        Offense.End.Date Day.of.the.week Day.Number Zone Beat
## 1                             Wednesday          4    7  703
## 2 4/15/2022 12:30:00 PM        Thursday          5    3  305
## 3 10/28/2022 1:00:00 PM        Thursday          5    4  405
## 4  6/17/2022 9:30:00 AM       Wednesday          4    6  605
## 5  6/17/2022 8:00:00 PM          Friday          6    6  605
## 6 10/11/2022 4:00:00 AM         Tuesday          3    3  310
##                       Location                          Location.Type
## 1         6000 N TERMINAL PKWY               Air/ Bus/ Train Terminal
## 2             323 DALTON ST SE Highway/ Road/ Alley/ Street/ Sidewalk
## 3 MLK DR NW / WESTLAND BLVD NW Highway/ Road/ Alley/ Street/ Sidewalk
## 4           301 MEMORIAL DR SE    Residence/ Home / Apartment / Condo
## 5            441 CAMERON ST SE    Residence/ Home / Apartment / Condo
## 6         2559 JONESBORO RD SE    Residence/ Home / Apartment / Condo
##   NIBRS.Code                          NIBRS.Code.Name Crime.Against
## 1        13C                             Intimidation        PERSON
## 2        290 Destruction/Damage/Vandalism of Property      PROPERTY
## 3        26C                            Impersonation      PROPERTY
## 4        23F                 Theft From Motor Vehicle      PROPERTY
## 5        290 Destruction/Damage/Vandalism of Property      PROPERTY
## 6        23F                 Theft From Motor Vehicle      PROPERTY
##   Was.a.firearm.involved. Press.Release Social.Media         Watch Longitude
## 1                       N            NA           NA Morning Watch -84.44328
## 2                       N            NA           NA Morning Watch -84.37761
## 3                       N            NA           NA     Day Watch -84.47294
## 4                       N            NA           NA Evening Watch -84.37766
## 5                       N            NA           NA Evening Watch -84.36529
## 6                       N            NA           NA Morning Watch -84.36694
##   Latitude     Neighborhood NPU Council.District UCR.Grouping Victim.Count
## 1 33.64128                                    NA      Part II            1
## 2 33.71991   Chosewood Park   Y                1      Part II            1
## 3 33.75364  Harland Terrace   I               10      Part II            1
## 4 33.74650       Grant Park   W                1       Part I            1
## 5 33.74235       Grant Park   W                1      Part II            1
## 6 33.68364 Browns Mill Park   Z                1       Part I            1
##                               GlobalID         x        y
## 1 3af90926-db32-465a-b805-ace34bf3eb4a -84.44328 33.64128
## 2 4cd37ec7-503a-46d3-9e47-063680f7dd96 -84.37761 33.71991
## 3 857c5c30-2e15-4ab8-a173-493abece6b5b -84.47294 33.75364
## 4 8f196400-ee54-4729-a9e5-201caa9cb109 -84.37766 33.74650
## 5 87e5e48c-1f3e-45ab-be66-760846fc57ab -84.36529 33.74235
## 6 bd67738b-c8a4-4482-aeaa-e6dc1afe6af4 -84.36694 33.68364
unique(crime.data$NIBRS.Code.Name)
##  [1] "Intimidation"                               
##  [2] "Destruction/Damage/Vandalism of Property"   
##  [3] "Impersonation"                              
##  [4] "Theft From Motor Vehicle"                   
##  [5] "Simple Assault"                             
##  [6] "Identity Theft"                             
##  [7] "Burglary/Breaking & Entering"               
##  [8] "All Other Larceny"                          
##  [9] "Motor Vehicle Theft"                        
## [10] "Drug/Narcotic Violations"                   
## [11] "Theft From Building"                        
## [12] "Robbery"                                    
## [13] "Aggravated Assault"                         
## [14] "Rape"                                       
## [15] "Theft of Motor Vehicle Parts or Accessories"
## [16] "False Pretenses/Swindle/Confidence Game"    
## [17] "Drug Equipment Violations"                  
## [18] "Fondling"                                   
## [19] "Murder & Nonnegligent Manslaughter"         
## [20] "Weapon Law Violations"                      
## [21] "Credit Card/Automated Teller Machine Fraud" 
## [22] "Shoplifting"                                
## [23] "Stolen Property Offenses"                   
## [24] "Wire Fraud"                                 
## [25] "Pornography/Obscene Material"               
## [26] "Counterfeiting/Forgery"                     
## [27] "Hacking/Computer Invasion"                  
## [28] "Arson"                                      
## [29] "Sodomy"                                     
## [30] "Embezzlement"                               
## [31] "Welfare Fraud"                              
## [32] "Theft From Coin-Operated Machine or Device" 
## [33] "Extortion/Blackmail"                        
## [34] "Purse-snatching"                            
## [35] "Animal Cruelty"                             
## [36] "Kidnapping/Abduction"                       
## [37] "Statutory Rape"
#Categorize Violent and Nonviolent crime
crime.data <- crime.data %>%
  mutate(violent = ifelse(NIBRS.Code.Name %in% c("Aggravated Assault", 
                                                 "Murder & Nonnegligent Manslaughter",
                                                 "Sodomy", "Animal Cruelty", "Statutory Rape", "Rape",
                                                 "Fondling", "Arson", "Kidnapping/Abduction", "Intimidation", 
                                                 "Simple Assault", "Weapon Law Violations"), 1, 0))
#Make sf Object from Latitude and Longitude
crime.sf <- st_as_sf(crime.data, coords = c("Longitude", "Latitude"), crs=4326)
#Get census data
bg <- get_acs("block_group", 
                      variables = c(
                        'tot_popE' = 'B01001_001E',  # Total Population
                        'male_popE' = 'B01001_002E', # Male Population
                        'female_popE' = 'B01001_026E', # Female Population
                        'median_incomeE' = 'B19013_001E', # Median Income
                        'edu_totalE' = 'B06009_001E', # Total (Educational Attainment)
                        'less_than_hsE' = 'B06009_002E', # Less than High School
                        'race_totalE' = 'B02001_001E', # Race Total
                        'whiteE' = 'B02001_002E', # White alone
                        'blackE' = 'B02001_003E', # Black alone
                        'nativeE' = 'B02001_004E', # American Indian and Alaska Native alone
                        'asianE' = 'B02001_005E', # Asian alone
                        'pacific_islanderE' = 'B02001_006E', # Native Hawaiian and Other Pacific Islander alone
                        'otherE' = 'B02001_007E' # Some Other Race alone
                      ),
                      year = 2022,
                      state = "GA", 
                      county = c("Fulton", "DeKalb", "Clayton", "Cobb"), 
                      survey = "acs5", 
                      geometry = TRUE, 
                      output = "wide",
                      progress = FALSE)
## Getting data from the 2018-2022 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
bg <- bg %>%
  select(GEOID, NAME, geometry, ends_with("E")) %>%
  mutate(
    pop_den = tot_popE / st_area(.),
    white_ratio = whiteE / race_totalE,
    black_ratio = blackE / race_totalE,
    other_ratio = (nativeE + asianE + pacific_islanderE + otherE) / race_totalE,
    less_than_hs_ratio = less_than_hsE / edu_totalE
  )
#Spatial Join
bg <- bg %>% st_transform(crs=4326)
crime.sf <- crime.sf %>% st_transform(crs=4326)
bg_crime <- st_join(bg, crime.sf %>% mutate(count = ifelse(violent == 0, 1, 10000)))

#Group by geoid and summarize the count
bg_crime_count <- bg_crime %>%
  group_by(GEOID) %>%
  summarise(count = sum(count, na.rm = T)) %>% 
  st_drop_geometry()


bg <- bg %>%
  left_join(bg_crime_count, by = "GEOID") %>% 
  mutate(violent_count = count %/% 10000,
         nonviolent_count = count %% 10000)
bg <- bg %>% filter((!count == 0 | GEOID %in% c("131210010022", "130899800001", "131210053001", "131210065001",
                                                "131210098042", "131210099005", "131210100042", "131210100071", 
                                                "131210100033", "131210095014", "131210094081", "131210089073", 
                                                "131210091041", "131210091052", "131210011012", "131210001001", 
                                                "131210102112", "131210080004", "131210053003", "131210068011",
                                                "131210043002")))
tm_basemap(providers$CartoDB.Positron) + 
  tm_shape(bg) + tm_polygons(col = "pop_den", style = "jenks") +
  #tm_shape(atlanta) + tm_polygons(alpha=0, border.col = "red") +
  tm_shape(crime.sf) + tm_dots(size = 0.01) 

t1 <- tm_basemap(providers$CartoDB.Positron) + 
  tm_shape(bg) + tm_polygons(col = "violent_count", style = "jenks", alpha = 0.3) +
  tm_shape(crime.sf %>% filter(violent == 1)) + tm_dots(col = "red", size = 0.01) 
t2 <- tm_basemap(providers$CartoDB.Positron) + 
  tm_shape(bg) + tm_polygons(col = "nonviolent_count", style = "jenks", alpha = 0.3) +
  tm_shape(crime.sf %>% filter(violent == 0)) + tm_dots(col = "grey", size = 0.01) 
tmap_arrange(t1, t2, sync = T)

#st_write(bg, "bg.geojson", driver = "GeoJSON")
zoning <- read.csv("Official_Zoning_Districts_-_Open_Data.csv")
zoning <- zoning %>% sort_by(.,zoning$ZONECLASS)
zoning %>% head()
##    OBJECTID ZONECLASS
## 1     10709       C-1
## 4     10712       C-1
## 5     10713       C-1
## 7     10715       C-1
## 8     10716       C-1
## 28    10739       C-1
##                                                                                                       ZONEDESC
## 1  https://library.municode.com/ga/atlanta/codes/code_of_ordinances?nodeId=PTIIICOORANDECO_PT16ZO_CH11COBUDIRE
## 4  https://library.municode.com/ga/atlanta/codes/code_of_ordinances?nodeId=PTIIICOORANDECO_PT16ZO_CH11COBUDIRE
## 5  https://library.municode.com/ga/atlanta/codes/code_of_ordinances?nodeId=PTIIICOORANDECO_PT16ZO_CH11COBUDIRE
## 7  https://library.municode.com/ga/atlanta/codes/code_of_ordinances?nodeId=PTIIICOORANDECO_PT16ZO_CH11COBUDIRE
## 8  https://library.municode.com/ga/atlanta/codes/code_of_ordinances?nodeId=PTIIICOORANDECO_PT16ZO_CH11COBUDIRE
## 28 https://library.municode.com/ga/atlanta/codes/code_of_ordinances?nodeId=PTIIICOORANDECO_PT16ZO_CH11COBUDIRE
##    BASEELEV HEIGHT LASTUPDATE LASTEDITOR SPI SUBAREA TSA  STATUS CASEIN CASEOUT
## 1         0      0                                       Current               
## 4         0      0                                       Current               
## 5         0      0                                       Current               
## 7         0      0                                       Current               
## 8         0      0                                       Current               
## 28        0      0                                       Current               
##                   SUNRISE                 SUNSET CREATEDATE CREATEDBY
## 1  1900/01/01 00:00:00+00 2999/12/31 00:00:00+00                     
## 4  1900/01/01 00:00:00+00 2999/12/31 00:00:00+00                     
## 5  1900/01/01 00:00:00+00 2999/12/31 00:00:00+00                     
## 7  1900/01/01 00:00:00+00 2999/12/31 00:00:00+00                     
## 8  1900/01/01 00:00:00+00 2999/12/31 00:00:00+00                     
## 28 1900/01/01 00:00:00+00 2999/12/31 00:00:00+00                     
##    CREATED_USER           CREATED_DATE LAST_EDITED_USER       LAST_EDITED_DATE
## 1           GIS 2018/04/19 00:00:00+00              GIS 2020/07/25 00:00:00+00
## 4           GIS 2018/04/19 00:00:00+00              GIS 2020/07/25 00:00:00+00
## 5           GIS 2018/04/19 00:00:00+00              GIS 2020/07/25 00:00:00+00
## 7           GIS 2018/04/19 00:00:00+00              GIS 2020/07/25 00:00:00+00
## 8           GIS 2018/04/19 00:00:00+00              GIS 2020/07/25 00:00:00+00
## 28          GIS 2018/04/19 00:00:00+00              GIS 2020/07/25 00:00:00+00
##                                  GLOBALID      ACRES ZONING EDITTED REFERENCE
## 1  {580C6D97-2A8F-487B-BB3A-67A6D0651D8B}  1.2923615    C-1             R3371
## 4  {D8BA20DC-E1A4-4FFE-B8B0-7DB3B984CA21}  0.8383994    C-1             R1249
## 5  {6E322071-34B4-4817-86DA-DD32D304EB7F}  5.8933400    C-1             R1983
## 7  {8503437E-AE71-489C-A316-BE97EA19FBE3} 21.6859544    C-1              R999
## 8  {3F33761C-5C4D-490B-B21D-42C3FBA80BC6}  0.6387836    C-1              R640
## 28 {8839DAC8-5014-44B7-AA0A-16008F13D6A8}  0.7497545    C-1              R392
##    SHAPE_Area SHAPE_Length
## 1    56295.04    1050.4486
## 4    36520.53    1172.7321
## 5   256712.86    3362.3735
## 7   944636.40    6673.8263
## 8    27825.30     766.6279
## 28   32659.18     730.4584
unique(zoning$ZONECLASS)
##   [1] "C-1"                 "C-1-C"               "C-2"                
##   [4] "C-2-C"               "C-3"                 "C-3-C"              
##   [7] "C-4"                 "C-4-C"               "C-5"                
##  [10] "C-5-C"               "FCR-3"               "HC-20A SA1"         
##  [13] "HC-20A SA2"          "HC-20A SA3"          "HC-20A SA4"         
##  [16] "HC-20A SA4-C"        "HC-20A SA5"          "HC-20B"             
##  [19] "HC-20C SA1"          "HC-20C SA2"          "HC-20C SA3"         
##  [22] "HC-20C SA3-C"        "HC-20C SA4"          "HC-20D"             
##  [25] "HC-20E"              "HC-20N SA1"          "HC-20N SA2"         
##  [28] "I-1"                 "I-1-C"               "I-2"                
##  [31] "I-2-C"               "I-MIX"               "I-Mix-C"            
##  [34] "I-MIX-C"             "LD Briarcliff Plaza" "LD Mean Street"     
##  [37] "LD Pratt- Pullman"   "LW"                  "LW-C"               
##  [40] "MR-1"                "MR-1-C"              "MR-2"               
##  [43] "MR-2-C"              "MR-3"                "MR-3-C"             
##  [46] "MR-3A-C"             "MR-4-C"              "MR-4A"              
##  [49] "MR-4A-C"             "MR-4B"               "MR-4B-C"            
##  [52] "MR-5A-C"             "MR-MU"               "MRC-1"              
##  [55] "MRC-1-C"             "MRC-2"               "MRC-2-C"            
##  [58] "MRC-3"               "MRC-3-C"             "MRC-4B-C"           
##  [61] "NC-1"                "NC-10 SA1"           "NC-10 SA2"          
##  [64] "NC-11"               "NC-12 SA1"           "NC-12 SA2"          
##  [67] "NC-13"               "NC-14"               "NC-15"              
##  [70] "NC-2"                "NC-3"                "NC-4"               
##  [73] "NC-5"                "NC-5-C"              "NC-6"               
##  [76] "NC-7"                "NC-7-C"              "NC-8"               
##  [79] "NC-9"                "O-I"                 "O-I-C"              
##  [82] "PD-H"                "PD-H1"               "PD-H2"              
##  [85] "PD-MU"               "PD-OC"               "Poncey-Highland SA1"
##  [88] "Poncey-Highland SA2" "Poncey-Highland SA3" "Poncey-Highland SA4"
##  [91] "Poncey-Highland SA5" "Poncey-Highland SA6" "Poncey-Highland SA7"
##  [94] "R-1"                 "R-2"                 "R-2A"               
##  [97] "R-2A-C"              "R-2B"                "R-3"                
## [100] "R-3-C"               "R-3A"                "R-4"                
## [103] "R-4-C"               "R-4A"                "R-4A-C"             
## [106] "R-4B"                "R-4B-C"              "R-5"                
## [109] "R-5-C"               "R-LC"                "R-LC-C"             
## [112] "RG-1"                "RG-1-C"              "RG-2"               
## [115] "RG-2-C"              "RG-3"                "RG-3-C"             
## [118] "RG-4"                "RG-4-C"              "RG-5"               
## [121] "RG-5-C"              "SPI-1 SA1"           "SPI-1 SA2"          
## [124] "SPI-1 SA3"           "SPI-1 SA4"           "SPI-1 SA5"          
## [127] "SPI-1 SA6"           "SPI-1 SA7"           "SPI-11 SA2"         
## [130] "SPI-11 SA6"          "SPI-11 SA8"          "SPI-11 SA9"         
## [133] "SPI-12 SA1"          "SPI-12 SA1-C"        "SPI-12 SA2"         
## [136] "SPI-12 SA2-C"        "SPI-12 SA3"          "SPI-12 SA3-C"       
## [139] "SPI-12 SA4"          "SPI-15 SA1"          "SPI-15 SA2"         
## [142] "SPI-15 SA3"          "SPI-15 SA4"          "SPI-15 SA5"         
## [145] "SPI-15 SA6"          "SPI-15 SA7"          "SPI-15 SA8"         
## [148] "SPI-15 SA9"          "SPI-16 SA1"          "SPI-16 SA1C"        
## [151] "SPI-16 SA2"          "SPI-16 SA2 JSTA"     "SPI-16 SA3"         
## [154] "SPI-17 SA1"          "SPI-17 SA2"          "SPI-17 SA3"         
## [157] "SPI-17 SA4"          "SPI-18 SA1"          "SPI-18 SA10"        
## [160] "SPI-18 SA2"          "SPI-18 SA3"          "SPI-18 SA4"         
## [163] "SPI-18 SA5"          "SPI-18 SA6"          "SPI-18 SA7"         
## [166] "SPI-18 SA8"          "SPI-18 SA9"          "SPI-19 SA1"         
## [169] "SPI-19 SA10"         "SPI-19 SA11"         "SPI-19 SA2"         
## [172] "SPI-19 SA3"          "SPI-19 SA4"          "SPI-19 SA5"         
## [175] "SPI-19 SA6"          "SPI-19 SA7"          "SPI-19 SA8"         
## [178] "SPI-2 SA1"           "SPI-2 SA2"           "SPI-2 SA3"          
## [181] "SPI-2 SA4"           "SPI-2 SA5"           "SPI-20 SA1"         
## [184] "SPI-20 SA2"          "SPI-20 SA3"          "SPI-20 SA4"         
## [187] "SPI-20 SA5"          "SPI-20 SA6"          "SPI-21 SA1"         
## [190] "SPI-21 SA10"         "SPI-21 SA2"          "SPI-21 SA3"         
## [193] "SPI-21 SA4"          "SPI-21 SA5"          "SPI-21 SA7"         
## [196] "SPI-21 SA8"          "SPI-21 SA9"          "SPI-22 SA1"         
## [199] "SPI-22 SA2"          "SPI-22 SA3"          "SPI-22 SA4"         
## [202] "SPI-22 TSA"          "SPI-3 SA1"           "SPI-3 SA2"          
## [205] "SPI-3 SA3"           "SPI-3 SA3-C"         "SPI-3 SA4"          
## [208] "SPI-3 SA5"           "SPI-3 SA5-C"         "SPI-3 SA6"          
## [211] "SPI-3 SA7"           "SPI-3 SA8"           "SPI-3 SA9"          
## [214] "SPI-4 SA 11"         "SPI-4 SA1"           "SPI-4 SA10"         
## [217] "SPI-4 SA11"          "SPI-4 SA12"          "SPI-4 SA13"         
## [220] "SPI-4 SA2"           "SPI-4 SA3"           "SPI-4 SA4"          
## [223] "SPI-4 SA5"           "SPI-4 SA6"           "SPI-4 SA7"          
## [226] "SPI-4 SA8"           "SPI-4 SA9"           "SPI-5 SA1"          
## [229] "SPI-5 SA2"           "SPI-5 SA3"           "SPI-7 SA1"          
## [232] "SPI-7 SA2A"          "SPI-7 SA2B"          "SPI-7 SA2C"         
## [235] "SPI-7 SA3"           "SPI-9-C SA1"         "SPI-9 SA1"          
## [238] "SPI-9 SA2"           "SPI-9 SA2-C"         "SPI-9 SA3"          
## [241] "SPI-9 SA4"
zoning <- zoning %>% 
  mutate(class = case_when(
    str_detect(ZONECLASS, "^(R-(1|2|3|4|5)|PD-H|FCR-)") ~ "Low-density Residential",
    str_detect(ZONECLASS, "^(R-LC|MRC-|PD-MU)") ~ "Residential-Commercial",
    str_detect(ZONECLASS, "^(RG-|MR-)") ~ "High-density Residential",
    str_detect(ZONECLASS, "^I-") ~ "Industrial",
    str_detect(ZONECLASS, "^(C-|LD|LW|NC|PD-OC)") ~ "Commercial",
    str_detect(ZONECLASS, "^O-") ~ "Institutional",
    
    #Historical regions
    str_detect(ZONECLASS, "^(HC-20A SA1|HC-20C SA4|HC-20N|Poncey-Highland SA(3|4|5))") ~ "Residential-Commercial",
    str_detect(ZONECLASS, "^(HC-20A SA3|HC-20C SA(1|2)|HC-20B|Poncey-Highland SA(7))") ~ "Low-density Residential",
    str_detect(ZONECLASS, "^(HC-20A SA2|Poncey-Highland SA(1|2|6))") ~ "High-density Residential",
    str_detect(ZONECLASS, "^(HC-20A SA(4|5))") ~ "Commercial",
    str_detect(ZONECLASS, "^(HC-20C SA3|HC-20D|HC-20E)") ~ "Institutional",
    
    #Special Areas
    str_detect(ZONECLASS, "^(SPI-1 SA(1|2|3|7)|SPI-9(-C| SA1)|SPI-11 SA(2|9)|SPI-12 SA1|SPI-15 SA(1|2|3|4|9)|SPI-18 SA3|SPI-19 SA(2|10)|SPI-20 SA(1|2)|SPI-21 SA(1|2|3|4|10)|SPI-22 SA4)") ~ "Commercial",
    str_detect(ZONECLASS, "^(SPI-1 SA(5|6)|SPI-2 SA5|SPI-3 SA(4|5|6|7|8|9)|SPI-4 SA(3|4|7|10|13)|SPI-9 SA(2|3|4)|SPI-16 SA1|SPI-18 SA(1|2|10)|SPI-19 SA(1|4|8|9|11)|SPI-20 SA(3|4)|SPI-22 TSA)") ~ "Residential-Commercial",
    str_detect(ZONECLASS, "^(SPI-1 SA4|SPI-3 SA3|SPI-4 SA(2|5|11| 11)|SPI-5 SA(2|3)|SPI-7 SA3|SPI-11 SA8|SPI-12 SA2|SPI-15 SA(5|6|7|8)|SPI-16 SA(2|3)|SPI-17|SPI-18 SA(4|9)|SPI-19 SA7|SPI-20 SA5|SPI-21 SA5|SPI-22 SA3)") ~ "High-density Residential",
    str_detect(ZONECLASS, "^(SPI-2 SA(1|2|3|4)|SPI-3 SA(1|2)|SPI-4 SA(6|9)|SPI-5 SA1|SPI-7 SA(1|2)|SPI-11 SA6|SPI-12 SA3|SPI-18 SA(5|6)|SPI-19 SA(5|6)|SPI-20 SA6|SPI-21 SA7|SPI-22 SA2)") ~ "Low-density Residential",
    str_detect(ZONECLASS, "^(SPI-4 SA8|SPI-18 SA(7|8)|SPI-21 SA9|SPI-22 SA1)") ~ "Industrial",
    str_detect(ZONECLASS, "^(SPI-4 SA12|SPI-12 SA4|SPI-19 SA3|SPI-21 SA8)") ~ "Institutional",
    str_detect(ZONECLASS, "SPI-4 SA1") ~ "Low-density Residential",
  
    
    TRUE ~ ""  
  ))
zoning.shape <- st_read("Official_Zoning_Data.geojson")
## Reading layer `Official_Zoning_Districts_-_Open_Data' from data source 
##   `C:\Users\benso\Documents\Georgia Tech\CP6025\Final Project\Official_Zoning_Data.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 2856 features and 28 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY, XYZ
## Bounding box:  xmin: -84.55086 ymin: 33.64798 xmax: -84.28962 ymax: 33.88687
## z_range:       zmin: 0 zmax: 0
## Geodetic CRS:  WGS 84
zoning.sf <- zoning.shape %>%
  select(OBJECTID) %>%
  left_join(zoning, by = "OBJECTID") %>%
  st_make_valid()
tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap(providers$CartoDB.Positron) +
  tm_shape(zoning.sf) + tm_polygons(col = "class", 
                                    palette = c("dodgerblue", "forestgreen", "gold", "mediumpurple1", "yellowgreen", "lightseagreen"), 
                                    popup.vars=c("ZONECLASS" = "ZONECLASS", "class" = "class")) + 
  tm_tiles(providers$CartoDB.Positron, alpha=0.4)