
#install.packages("sf")
#install.packages("janitor")
#install.packages("leaflet")
#install.packages("rio")
#install_formats()

library(sf)
library(dplyr)
library(leaflet)
library(rio)

#all counties
counties <- sf::st_read(dsn = "/home/abarrant/Documents/RStudio/cb_2022_us_county_20m_folder/cb_2022_us_county_20m.shp",  #we dont put file extension .shp at the end
                       layer = "cb_2022_us_county_20m") |>
          #convert coordinates to simple feature
          sf::st_transform('+proj=longlat +datum=WGS84') |>
          #reduce file size 
          sf::st_simplify()  |>
          #convert variables to lowercase
          dplyr::rename_all(~janitor::make_clean_names(.x))

#california fips(num state) is 06
ca <- counties |>
    dplyr::filter(grepl("06", statefp))

plot(ca |> dplyr::select(countyfp))


ca_counties <- ca |> 
              dplyr::filter(name %in% c("Los Angeles", "San Francisco", "Napa"))
              leaflet() |>
                fitBounds(-122.6, 32.8, -117.6, 38.8) |>
                addTiles() |>
                addPolygons(data = ca_counties, 
                            popup = ~name)
              
counties_join_by_group <-
                ca |>
                dplyr::mutate(number = 1) |>
                dplyr::group_by(statefp) |>
                dplyr::summarize(tot = sum(number))
                #map 
                leaflet() |> 
                  #fitBounds(-124.4, 32.5, -114.1,  42) |> 
                  setView(-120.52462403790528, 37.74238197955183, zoom = 5) |>
                  addTiles() |> 
                  addPolygons(data = counties_join_by_group)

# join counties into California
counties_join_by_union <- sf::st_union(ca)
                # map
leaflet() %>%
      fitBounds(-124.4, 32.5, -114.1,  42) |> 
      #base groups
      addTiles(group = "OSM (default)") |>
      addPolygons(data = counties_join_by_union)

#calculate centroids
centroids <- sf::st_centroid(ca)
leaflet() |> 
  fitBounds(-122.6, 32.8, -117.6, 38.8) |>
  #base groups
  addTiles(group = "OSM (default)") |>
  #layer groups
  addPolygons(data = sf::st_union(ca),
              group = "State") |>
  addPolygons(data = ca,
              group = "Counties") |>
  addMarkers(lat = unlist(lapply(centroids$geometry, "[[", 2)),
             lng = unlist(lapply(centroids$geometry, "[[", 1)),
             group = "Centroids") |>
  #layer control
  addLayersControl(overlayGroups = c("State", "Counties", "Centroids"),
                   position = "topright",
                   options = layersControlOptions(collapsed =FALSE)) |>
  hideGroup(c("State", "Counties", "Centroids"))
              
file <- 'https://www.ers.usda.gov/webdocs/DataFiles/48747/Unemployment.xlsx'
usda <- rio::import(
  file = file,
  skip = 4
) |> 
  dplyr::rename_all(~janitor::make_clean_names(.x)) |> 
  dplyr::select(fips_code:area_name, unemployment_rate_2020) |> 
  dplyr::filter(state == "CA") |> 
  tidyr::separate(fips_code, into = c("statefp", "countyfp"), sep = 2) |> 
  dplyr::select(statefp, countyfp, unemployment_rate_2020)
    

ca_choropleth <-
  dplyr::left_join(ca, usda, by = c("statefp", "countyfp"))
