##### Map-Histogram Poster of City Streets for a Selected City #####

# Load libraries ----------------------------------------------------------
require(osmdata)
require(sf)
require(geosphere)
require(tidyverse)
require(cowplot)
require(extrafont)
require(Cairo)

# Load/Create data ---------------------------------------------------------------
city_oi <- ""
cityOI_prov <- ""
cityOI_selected <- str_replace(as.character(city_oi), " Canada", "")

# Create a data frame that contains the limits for the city
get_limits <- function(city) {
        
        df <- getbb(city) %>%
                as_tibble(rownames = NA) %>%
                rownames_to_column(var = "dimension") %>%
                mutate(City = cityOI_selected) %>%
                pivot_longer(cols = c(min, max), 
                             names_to = "Range", 
                             values_to = "Value") %>%
                select(City, everything())
        
        return(df)
}

cityOI_limits <- get_limits(city_oi)

# Create a data frame that contains the street-level data for the city
get_streets <- function(city) {
        
        city_name <- cityOI_selected
        
        df <- getbb(city) %>%
                opq() %>%
                add_osm_feature(key = "highway",
                                value = c("motorway", "trunk", "primary", "secondary", "tertiary",
                                          "residential", "living_street", "unclassified", "service")) %>%
                osmdata_sf() %>%
                .$osm_lines %>%
                mutate(City = city_name,
                       Road_type = map_chr(highway, ~ ifelse(.x %in% c("motorway", "trunk", "primary", "secondary", "tertiary"),
                                                             "major",
                                                             "minor"))) %>%
                select(City, osm_id, name, Road_type, geometry) %>%
                filter(!is.na(name))
        
        return(df)
}

cityOI_streets 