setwd("/Users/lutherhoy/Downloads/Informatics/Lab8")
options(osmdata.overpass_url = "https://overpass.kumi.systems/api/interpreter")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── 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.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(tmap)
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
# Set interactive mode for web export
tmap_mode("view")
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
# Define the UMich North Campus Bounding Box
north_bbox <- c(-83.725706, 42.287305, -83.704518, 42.302508)
# 1. Retrieve Buildings (Polygons)
building_polygons <- opq(bbox = north_bbox) %>%
  add_osm_feature(key = "building") %>%
  osmdata_sf() %>%
  .[["osm_polygons"]] %>%
  st_make_valid()

# 2. Retrieve Roads (Lines)
roads_lines <- opq(bbox = north_bbox) %>%
  add_osm_feature(key = "highway") %>%
  osmdata_sf() %>%
  .[["osm_lines"]]

# 3. Retrieve Amenities (Points + Polygons converted to centroids)
amenity_data <- opq(bbox = north_bbox) %>%
  add_osm_features(features = c(
    "amenity" = "restaurant",
    "amenity" = "cafe"
  )) %>%
  osmdata_sf()

# Combine point data and polygon centers so no cafes are missed
amenity_points <- bind_rows(
  amenity_data$osm_points,
  st_centroid(amenity_data$osm_polygons)
)
## Warning: st_centroid assumes attributes are constant over geometries
# 4. Retrieve Bus Stops (Points)
bus_stops <- opq(bbox = north_bbox) %>%
  add_osm_feature(key = "highway", value = "bus_stop") %>%
  osmdata_sf() %>%
  .[["osm_points"]]
# Create the interactive map
# Layer order: Roads (bottom) -> Buildings -> Amenities -> Bus Stops (top)
map <- tm_shape(roads_lines, name = "Roads") +
  tm_lines(col = "gray70", lwd = 1.5) +
  
  tm_shape(building_polygons, name = "Buildings") +
  tm_polygons(fill = "gray40", fill_alpha = 0.4, col = "white", lwd = 0.5) +
  
  tm_shape(amenity_points, name = "Food & Drink") +
  tm_dots(fill = "red", 
          size = 0.05, 
          fill.legend = tm_legend(title = "Eats & Coffee"),
          hover = "name",
          popup.vars = c("Name" = "name", "Type" = "amenity")) +
  
  tm_shape(bus_stops, name = "Transportation") +
  tm_dots(fill = "blue", 
          size = 0.05, 
          fill.legend = tm_legend(title = "Bus Stops"),
          hover = "name",
          popup.vars = c("Stop Name" = "name")) +
  
  # tmap 4.2 Title Syntax
  tm_title("UMich North Campus Amenities") +
  
  # Optional: Add layer control for the user
  tm_view(set.view = c(-83.715, 42.295, 15)) 
## [v3->v4] `tm_view()`: use set_view instead of set.view
# Display the map
map
## Registered S3 method overwritten by 'jsonify':
##   method     from    
##   print.json jsonlite

```