Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created November 26, 2025 13:53
Show Gist options
  • Select an option

  • Save walkerke/a214c8aabec0e87ac8044085254eef19 to your computer and use it in GitHub Desktop.

Select an option

Save walkerke/a214c8aabec0e87ac8044085254eef19 to your computer and use it in GitHub Desktop.
# Day 26: Transport - US Electric Power Transmission Lines
# Visualizing America's invisible highway: 700,000+ miles of power lines
# Dynamic line widths by voltage class, glowing effect for the energy flow
library(mapgl)
library(sf)
library(dplyr)
# Load transmission lines from HIFLD (Homeland Infrastructure Foundation-Level Data)
# This is a large dataset - we'll use direct URL for the GeoJSON
transmission_url <- paste0(
"https://services2.arcgis.com/FiaPA4ga0iQKduv3/arcgis/rest/services/",
"US_Electric_Power_Transmission_Lines/FeatureServer/0/query?",
"outFields=*&where=1%3D1&f=geojson"
)
# Read and process
lines <- st_read(transmission_url)
# Clean up voltage classes for visualization
lines <- lines |>
mutate(
voltage_category = case_when(
VOLT_CLASS == "UNDER 100" ~ "Under 100 kV",
VOLT_CLASS == "100-161" ~ "100-161 kV",
VOLT_CLASS == "220-287" ~ "220-287 kV",
VOLT_CLASS == "345" ~ "345 kV",
VOLT_CLASS == "500" ~ "500 kV",
VOLT_CLASS == "735 AND ABOVE" ~ "735+ kV",
VOLT_CLASS == "DC" ~ "DC",
TRUE ~ "Unknown"
),
# Numeric for sizing
voltage_rank = case_when(
VOLT_CLASS == "UNDER 100" ~ 1,
VOLT_CLASS == "100-161" ~ 2,
VOLT_CLASS == "220-287" ~ 3,
VOLT_CLASS == "345" ~ 4,
VOLT_CLASS == "500" ~ 5,
VOLT_CLASS == "735 AND ABOVE" ~ 6,
VOLT_CLASS == "DC" ~ 5,
TRUE ~ 1
)
)
# Electric color palette - from dim to blazing
electric_colors <- c(
"#1a1a2e", # dim - unknown/low
"#4a00e0", # violet - under 100
"#8e2de2", # purple - 100-161
"#00d4ff", # cyan - 220-287
"#00ff88", # green - 345
"#ffdd00", # yellow - 500
"#ff6600"
)
# Build the map with glowing transmission lines
maplibre(
style = maptiler_style("backdrop", variant = "dark"),
center = c(-98.5, 39.5),
zoom = 4,
pitch = 0
) |>
# Outer glow layer - creates the electric aura
# Glow scales with zoom to avoid artifacts when zoomed out
add_line_layer(
id = "transmission-glow",
source = lines,
line_color = interpolate(
column = "voltage_rank",
values = c(1, 2, 3, 4, 5, 6),
stops = c("#4a00e0", "#8e2de2", "#00d4ff", "#00ff88", "#ffdd00", "#ff6600")
),
line_width = interpolate(
property = "zoom",
values = c(3, 6, 10),
stops = c(2, 6, 14)
),
line_blur = interpolate(
property = "zoom",
values = c(3, 6, 10),
stops = c(1, 3, 6)
),
line_opacity = interpolate(
property = "zoom",
values = c(3, 5, 7),
stops = c(0.2, 0.3, 0.4)
)
) |>
# Core line layer - the actual transmission line
add_line_layer(
id = "transmission-lines",
source = lines,
line_color = interpolate(
column = "voltage_rank",
values = c(1, 2, 3, 4, 5, 6),
stops = c("#6366f1", "#a855f7", "#22d3ee", "#4ade80", "#facc15", "#fb923c")
),
line_width = interpolate(
property = "zoom",
values = c(3, 6, 10),
stops = c(0.5, 1.5, 4)
),
line_opacity = 0.9,
tooltip = concat(
"<div style='background: #0a0a14; color: #e0e0e0; padding: 12px 16px; ",
"border-radius: 8px; border: 1px solid rgba(0, 212, 255, 0.4); ",
"box-shadow: 0 0 15px rgba(0, 212, 255, 0.3); font-family: system-ui;'>",
"<div style='font-size: 15px; font-weight: 700; color: #00d4ff; margin-bottom: 8px; ",
"text-shadow: 0 0 8px rgba(0, 212, 255, 0.5);'>",
get_column("voltage_category"),
"</div>",
"<div style='font-size: 12px; line-height: 1.6;'>",
"<span style='color: #888;'>Type:</span> ",
get_column("TYPE"),
"<br><span style='color: #888;'>Status:</span> ",
get_column("STATUS"),
"<br><span style='color: #888;'>Owner:</span> ",
get_column("OWNER"),
"</div></div>"
)
) |>
# Title control
add_control(
html = paste0(
"<div style='font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif; ",
"background: rgba(10, 10, 20, 0.9); padding: 16px 20px; border-radius: 8px; ",
"border: 1px solid rgba(100, 200, 255, 0.3); box-shadow: 0 0 20px rgba(0, 200, 255, 0.2); ",
"max-width: 320px;'>",
"<div style='font-size: 20px; font-weight: 700; color: #00d4ff; margin-bottom: 8px; ",
"text-shadow: 0 0 10px rgba(0, 212, 255, 0.5);'>",
"U.S. Power Grid</div>",
"<div style='font-size: 13px; color: #a0a0a0; line-height: 1.5;'>",
"Over 700,000 miles of high-voltage transmission lines power America. ",
"Line brightness and width indicate voltage class.</div>",
"<div style='font-size: 10px; color: #606060; margin-top: 10px; padding-top: 8px; ",
"border-top: 1px solid rgba(100, 200, 255, 0.2);'>",
"Source: Homeland Infrastructure Foundation-Level Data (HIFLD)</div>",
"</div>"
),
position = "top-left"
) |>
# Voltage legend
add_legend(
legend_title = "Voltage Class",
values = c("Under 100 kV", "100-161 kV", "220-287 kV", "345 kV", "500 kV", "735+ kV"),
colors = c("#6366f1", "#a855f7", "#22d3ee", "#4ade80", "#facc15", "#fb923c"),
type = "categorical",
position = "bottom-left",
width = "160px"
) |>
add_navigation_control(position = "top-right") |>
add_fullscreen_control(position = "top-right")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment