The chart below details the current countour areas that are taken up by the radio stations in the mid-state area. This shows the general coverage area of these particular stations and gives us a baseline understanding that Nashville is getting a heightened amount of coverage at the moment.The code that got this chart will also be demonstrated below.
#################################################
# STEP 1. LOAD REQUIRED PACKAGES
#################################################
required_packages <- c(
"tidyverse",
"sf",
"tigris",
"jsonlite"
)
missing_packages <- required_packages[
!required_packages %in% installed.packages()[, "Package"]
]
if(length(missing_packages) > 0) {
install.packages(missing_packages)
}
library(tidyverse)
library(sf)
library(tigris)
library(jsonlite)
#################################################
# STEP 2. DOWNLOAD STATION LIST
#################################################
url <- "https://raw.githubusercontent.com/dks4a/Middle-Tennessee-Radio-Station-Data/refs/heads/main/middle_tennessee_radio_stations.csv"
Population <- read_csv(url)
#################################################
# STEP 3. DEFINE MIDDLE TENNESSEE COUNTIES
#################################################
middle_tn <- c(
"Bedford","Cannon","Cheatham","Clay","Coffee","Davidson",
"DeKalb","Dickson","Fentress","Franklin","Giles","Grundy",
"Hickman","Houston","Humphreys","Jackson","Lawrence","Lewis",
"Lincoln","Macon","Marshall","Maury","Montgomery","Moore",
"Overton","Perry","Pickett","Putnam","Robertson","Rutherford",
"Smith","Stewart","Sumner","Trousdale","Van Buren","Warren",
"Wayne","White","Williamson","Wilson"
)
#################################################
# STEP 4. DOWNLOAD TENNESSEE COUNTY BOUNDARIES
#################################################
tn_counties <- counties(
state = "TN",
cb = TRUE,
year = 2024
) |>
st_transform(4326)
middle_tn_counties <- tn_counties |>
filter(NAME %in% middle_tn)
#################################################
# STEP 5. CREATE LIST OF UNIQUE CALL SIGNS
#################################################
working_calls <- unique(
Population$CallSign
)
#################################################
# STEP 6. FCC CONTOUR DOWNLOAD FUNCTION
#################################################
get_station_sf <- function(callsign) {
attempts <- expand.grid(
service = c("fm", "am"),
call = c(
callsign,
paste0(callsign, "-FM")
),
stringsAsFactors = FALSE
)
for(i in seq_len(nrow(attempts))) {
service <- attempts$service[i]
cs <- attempts$call[i]
url <- paste0(
"https://geo.fcc.gov/api/contours/entity.json?",
"serviceType=", service,
"&callsign=", cs,
"&nradial=360",
"&field=60",
"&curve=0",
"&pop=false",
"&area=false"
)
result <- suppressWarnings(
try(
fromJSON(url),
silent = TRUE
)
)
if(inherits(result, "try-error")) {
next
}
if(is.null(result$features) ||
length(result$features) == 0) {
next
}
coords <- try(
result$features$geometry$coordinates[[1]][1,1,,],
silent = TRUE
)
if(inherits(coords, "try-error")) {
next
}
message(
"Success: ",
callsign,
" -> ",
service,
" / ",
cs
)
return(
st_sf(
CallSign = callsign,
Service = toupper(service),
FCC_CallSign = cs,
geometry = st_sfc(
st_polygon(list(coords)),
crs = 4326
)
)
)
}
stop(
paste(
"No contour found for",
callsign
)
)
}
#################################################
# STEP 7. DOWNLOAD FCC CONTOURS
#################################################
contour_list <- vector(
"list",
length(working_calls)
)
for(i in seq_along(working_calls)) {
cat(
"Downloading:",
working_calls[i],
"\n"
)
contour_list[[i]] <- try(
get_station_sf(
working_calls[i]
),
silent = TRUE
)
}
#################################################
# STEP 8. IDENTIFY FAILED STATIONS
#################################################
failed <- working_calls[
sapply(
contour_list,
inherits,
"try-error"
)
]
#################################################
# STEP 9. REPORT FAILED STATIONS
#################################################
if(length(failed) > 0) {
warning(
paste(
"No FCC contour available for:",
paste(failed, collapse = ", ")
)
)
}
#################################################
# STEP 10. KEEP SUCCESSFUL DOWNLOADS ONLY
#################################################
successful_contours <- contour_list[
!sapply(
contour_list,
inherits,
"try-error"
)
]
#################################################
# STEP 11. COMBINE ALL CONTOURS
#################################################
fm_contours <- do.call(
rbind,
successful_contours
)
#################################################
# STEP 12. VERIFY RESULTS
#################################################
cat(
"\nUnique Stations:",
length(unique(Population$CallSign)),
"\nContours Downloaded:",
nrow(fm_contours),
"\nFailed Stations:",
if(length(failed) == 0) "None" else paste(failed, collapse = ", "),
"\n"
)
#################################################
# STEP 13. MAP OF MIDDLE TENNESSEE RADIO COVERAGE
#################################################
plot(
st_geometry(middle_tn_counties),
col = "gray95",
border = "gray70",
main = "Middle Tennessee Radio Station Contours"
)
plot(
st_geometry(fm_contours),
add = TRUE,
border = "blue"
)
#################################################
# STEP 14. OPTIONAL: SAVE CONTOURS TO FILE
#################################################
st_write(
fm_contours,
"middle_tennessee_radio_contours.geojson",
delete_dsn = TRUE
)