## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = nzchar(Sys.getenv("CLOSECITY_KEY"))
)
library(closecity)
library(sf)
close <- closecity::close_client(api_key = Sys.getenv("CLOSECITY_KEY"))

## -----------------------------------------------------------------------------
# # The catalog lists every category with its numeric id. Pull the ids you need.
# amenity_types <- close$destination_types()
# ids <- setNames(amenity_types$dest_type_id, amenity_types$label)
# 
# supermarket_dest_id <- ids[["grocery_stores"]]
# restaurant_dest_id <- ids[["restaurants"]]
# freq_transit_stop_dest_id <- ids[["frequent_transit"]]
# 
# # Turn the city name into a GEOID and pull its boundary for context.
# city <- close$places(q = "Somerville")[1, ]
# city_boundary <- close$place_boundary(geoid = city$geoid)

## -----------------------------------------------------------------------------
# supermarkets <- close$place_pois(geoid = city$geoid, type = supermarket_dest_id)
# restaurants <- close$place_pois(geoid = city$geoid, type = restaurant_dest_id)
# stops <- close$place_pois(geoid = city$geoid, type = freq_transit_stop_dest_id)
# 
# supermarkets$kind <- "Supermarket"
# restaurants$kind <- "Restaurant"
# stops$kind <- "Transit stop"
# around <- rbind(supermarkets, restaurants, stops)
# 
# palette <- c(Supermarket = "#058040", Restaurant = "#c6cbe0", `Transit stop` = "#f36e21")
# closecity::close_map(
#   x = around,
#   color = palette[around$kind],
#   label = "kind",
#   boundary = city_boundary
# )

## -----------------------------------------------------------------------------
# blocks <- close$place_blocks(
#   geoid = city$geoid,
#   mode = "walk",
#   type = c(supermarket_dest_id, restaurant_dest_id, freq_transit_stop_dest_id)
# )

## -----------------------------------------------------------------------------
# city_blocks <- blocks[!duplicated(blocks$geoid), "geoid"]
# time_to <- function(type_id) {
#   sub <- blocks[blocks$dest_type_id == type_id, ]
#   setNames(sub$travel_time, sub$geoid)[city_blocks$geoid]
# }
# city_blocks$supermarket_min <- time_to(supermarket_dest_id)
# city_blocks$restaurant_min <- time_to(restaurant_dest_id)
# city_blocks$transit_min <- time_to(freq_transit_stop_dest_id)
# 
# city_blocks$qualifies <- (city_blocks$supermarket_min <= 10 &
#                           city_blocks$restaurant_min <= 5 &
#                           city_blocks$transit_min <= 20)
# city_blocks$qualifies[is.na(city_blocks$qualifies)] <- FALSE

## -----------------------------------------------------------------------------
# closecity::close_map(
#   x = city_blocks,
#   highlight = "qualifies",
#   color = "#f36e21",
#   boundary = city_boundary
# )

## -----------------------------------------------------------------------------
# work_a <- close$isochrone(
#   lon = -71.0865,
#   lat = 42.3625,
#   mode = "transit",
#   direction = "from",
#   minutes = 20,
#   format = "geojson"
# )
# work_b <- close$isochrone(
#   lon = -71.0589,
#   lat = 42.3555,
#   mode = "transit",
#   direction = "from",
#   minutes = 20,
#   format = "geojson"
# )
# 
# closecity::close_map(
#   x = work_a,
#   color = "#058040",
#   opacity = 0.5,
#   background = work_b,
#   background_color = "#f36e21",
#   background_opacity = 0.5
# )

## -----------------------------------------------------------------------------
# both_commutes <- sf::st_intersection(sf::st_union(work_a), sf::st_union(work_b))
# winners <- city_blocks[city_blocks$qualifies, ]
# winners$shortlist <- sf::st_intersects(winners, both_commutes, sparse = FALSE)[, 1]
# 
# closecity::close_map(
#   x = winners,
#   highlight = "shortlist",
#   color = "#1f78b4",
#   boundary = city_boundary,
#   background = list(work_a, work_b),
#   background_color = c("#058040", "#f36e21"),
#   background_fill = FALSE
# )

