##Title: DWVGIS - 6. Maxwell Eggers Hall.R
#Author: Jack Reilly
#Purpose: Syracuse University/Maxwell Street Map with OSM
#Requires: no external data; must install osmdata within script


library(osmdata)
library(sf)
library(dplyr)
library(ggplot2)

#The bounding box for our query
main_bb <- c(
  left   = -76.1435,
  bottom =  43.0330,
  right  = -76.1215,
  top    =  43.0445
)

#The bounding box for our final view
sview_bb <- c(
  left   = -76.1410,
  bottom =  43.0335,
  right  = -76.1225,
  top    =  43.0438
)

safe_sf <- function(x, crs = 4326) {
  if (is.null(x)) st_sf(geometry = st_sfc(crs = crs))[0, ] 
  else suppressWarnings(st_make_valid(x))
}

# ---- Queries ----
is_big_streets <- opq(bbox = main_bb) |>
  add_osm_feature("highway", c("primary","trunk","primary_link","trunk_link")) |>
  osmdata_sf()
is_med_streets <- opq(bbox = main_bb) |>
  add_osm_feature("highway", c("secondary","tertiary","secondary_link","tertiary_link")) |>
  osmdata_sf()
is_small_streets <- opq(bbox = main_bb) |>
  add_osm_feature("highway", c("residential","service","unclassified")) |>
  osmdata_sf()
is_ped <- opq(bbox = main_bb) |>
  add_osm_feature("highway", c("footway","path","pedestrian","steps")) |>
  osmdata_sf()
is_buildings <- opq(bbox = main_bb) |>
  add_osm_feature("building") |>
  osmdata_sf()
is_green <- opq(bbox = main_bb) |>
  add_osm_feature("landuse", c("grass","park","recreation_ground")) |>
  osmdata_sf()
is_parking <- opq(bbox = main_bb) |>
  add_osm_feature("amenity", "parking") |>
  osmdata_sf()

# ---- Extract layers ----
bldg_poly  <- safe_sf(is_buildings$osm_polygons)
bldg_mpoly <- safe_sf(is_buildings$osm_multipolygons)

green_poly  <- safe_sf(is_green$osm_polygons)
green_mpoly <- safe_sf(is_green$osm_multipolygons)
park_poly   <- safe_sf(is_parking$osm_polygons)
park_mpoly  <- safe_sf(is_parking$osm_multipolygons)

ln_big   <- safe_sf(is_big_streets$osm_lines)
ln_med   <- safe_sf(is_med_streets$osm_lines)
ln_small <- safe_sf(is_small_streets$osm_lines)
ln_ped   <- safe_sf(is_ped$osm_lines)

# ---- Identify Maxwell & Eggers ----
# Combine polygons and multipolygons for search
bldgs_all <- bind_rows(bldg_poly, bldg_mpoly)

# Check what OSM calls them (optional):
# unique(bldgs_all$name)

bldgs_all <- bldgs_all |>
  mutate(
    name_lower = tolower(name),
    highlight = ifelse(
      str_detect(name_lower, "maxwell") | str_detect(name_lower, "eggers"),
      "highlight", "normal"
    )
  )

# ---- Plot ----
ggplot() +
  # Greenspace
  geom_sf(data = green_poly,  fill = "#DDEBD2", color = NA, alpha = 0.7) +
  geom_sf(data = green_mpoly, fill = "#DDEBD2", color = NA, alpha = 0.7) +
  # Parking
  geom_sf(data = park_poly,  fill = "#EFE9E4", color = NA, alpha = 0.9) +
  geom_sf(data = park_mpoly, fill = "#EFE9E4", color = NA, alpha = 0.9) +
  # Roads
  geom_sf(data = ln_ped,   color = "#A7ADB4", linewidth = 0.25, alpha = 0.8) +
  geom_sf(data = ln_small, color = "#5C636A", linewidth = 0.35, alpha = 0.6) +
  geom_sf(data = ln_med,   color = "#5C636A", linewidth = 0.6,  alpha = 0.8) +
  geom_sf(data = ln_big,   color = "#0D2D6C", linewidth = 0.9,  alpha = 0.9) +
  # Buildings — blue for all, orange for Maxwell/Eggers
  geom_sf(data = bldgs_all |> filter(highlight == "normal"),
          fill = "#90CAF9", color = "#234E70", linewidth = 0.18, alpha = 0.95) +
  geom_sf(data = bldgs_all |> filter(highlight == "highlight"),
          fill = "#F76900", color = "#8A3B00", linewidth = 0.25, alpha = 0.95) +
  coord_sf(xlim = c(sview_bb["left"], sview_bb["right"]),
           ylim = c(sview_bb["bottom"], sview_bb["top"]),
           expand = FALSE) +
  theme_void() +
  labs(
    title = "Syracuse University — Main Campus",
    subtitle = "Maxwell & Eggers Hall highlighted in orange"
  )