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

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

main_bb <- c(
  left   = -76.1435,
  bottom =  43.0330,
  right  = -76.1215,
  top    =  43.0445
)

# Helper: safely handle possibly NULL layers
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 (bounded by bbox; no clipping) ----
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 safely ----
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)

sview_bb <- c(
  left   = -76.1390,
  bottom =  43.035,
  right  = -76.128,
  top    =  43.043
)

# ---- 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
  geom_sf(data = bldg_poly,  fill = "#F76900", color = "#8A3B00", linewidth = 0.18, alpha = 0.95) +
  geom_sf(data = bldg_mpoly, fill = "#F76900", color = "#8A3B00", linewidth = 0.18, alpha = 0.95) +
  # Frame map extent
  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 = "Brochure-style map (buildings, roads, green, parking)")

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
  geom_sf(data = bldg_poly,  fill = "#F76900", color = "#8A3B00", linewidth = 0.18, alpha = 0.95) +
  geom_sf(data = bldg_mpoly, fill = "#F76900", color = "#8A3B00", linewidth = 0.18, alpha = 0.95) +
  # Frame map extent
  coord_sf(xlim = c(sview_bb["left"], sview_bb["right"]),
           ylim = c(sview_bb["bottom"], sview_bb["top"]),
           expand = FALSE) +
  theme(plot.title = element_text(size = 20, family = "lato", face="bold", hjust=.5),
        plot.subtitle = element_text(family = "lato", size = 8, hjust=.5, margin=margin(2, 0, 5, 0))) +
  labs(title = "SYRACUSE UNIVERSITY", subtitle = "CORE CAMPUS")



