GEOG 588 with Marcela Suárez, Penn State

Part B

Load/Set Up Required Data for Part B

library(sf)
library(tidyverse)
library(leaflet)

# Read and convert trail data to spatial format
trails <- read_csv('Traildata.csv') %>% 
  st_as_sf(coords = c("StartX", "StartY"), crs = 4326) %>% 
  select(Trail_Name, Difficulty, Access, Miles)

# Round length to two decimal places
trails$Miles <- round(trails$Miles, 2)

Display all trail start locations in San Luis Obispo, California based on Trail Difficulty

In this project, I wanted to take my City’s trail data and visualize key information at the start of the trail. Included is the trail name (also labeled), trail distance in miles, trail access (i.e Hiking only, Hiking/Biking allowed), and trail difficulty, which was also used as the symbology. Because our trail difficulty system does not take into consideration local flat trails, I needed to develop a way to return a “No Rating Other” yellow circle for the trails that are not considered easy, moderate, or difficult.

# Define difficulty colors as a named list
difficulty_colors <- list("green" = "green", "blue" = "blue", "black" = "black")

# Write function to get color based on difficulty, return yellow if it does not fit in list
get_color <- function(difficulty) {
  if (difficulty %in% names(difficulty_colors)) {
    return(difficulty_colors[[difficulty]])
  } else {
    return("yellow")  # Default color for all other values
  }
}

# Ensure the Color column is a standard character vector (not named)
trails$Color <- unname(sapply(trails$Difficulty, get_color))

# Create the Leaflet map and add circle markers with color based on difficulty
leaflet(trails) %>% 
  setView(lng = -120.66, lat = 35.279, zoom = 13) %>% 
  addProviderTiles(providers$Esri.WorldImagery, options = providerTileOptions(opacity = 0.6)) %>% 
  addCircleMarkers(
    radius = 6, 
    color = ~Color,
    fillOpacity = 0.8,
    popup = ~paste("<b>Trail:</b>", Trail_Name, "<br><b>Difficulty:</b>", Difficulty,"<br><b>Access:</b>", Access, "<br><b>Trail Length (mi):</b>", Miles),
    label = ~Trail_Name
  ) %>%
  # Add a legend
  addLegend(
    position = "bottomright",
    title = "Trail Difficulty",
    colors = c("green", "blue", "black", "yellow"),
    labels = c("Green (Easy)", "Blue (Moderate)", "Black (Difficult)", "Other (No Rating)"),
    opacity = 0.8
  )