Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active November 4, 2023 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmarwick/2237a297a33c8cb26053a526a192333e to your computer and use it in GitHub Desktop.
Save benmarwick/2237a297a33c8cb26053a526a192333e to your computer and use it in GitHub Desktop.
What time of the day do people post their SOTA spots?
library(httr2)
library(tidyverse)
library(sf)
library(lutz)
library(hms)
req <- request("https://api2.sota.org.uk/api/")
# get associations so we can get their time zones via lat-long
resp <- req %>%
req_url_path_append("associations") %>%
req_perform() %>%
resp_body_json()
assoc_tbl <-
resp %>%
tibble(resp = resp) %>%
unnest_wider(resp)
# find the timezones for each association
assoc_tbl_tz <-
assoc_tbl %>%
mutate(activeFrom = str_replace_all(activeFrom, "T", " ")) %>%
mutate(date = as_datetime(activeFrom,
format = "%Y-%m-%d %H:%M:%S",
tz = "GMT"),
timezone = tz_lookup_coords(lat = minLat,
lon = minLong,
method = "accurate"))
# are all timezones in the set available at my locale?
zz <- unique(assoc_tbl_tz$timezone)
cbind(zz ,(zz %in% OlsonNames()))
# get 1000 or so of the most recent spots
resp <- req %>%
req_url_path_append("spots") %>%
req_url_path_append("-1000") %>%
# Add query parameters
req_url_query(
`filter` = "all") %>%
req_perform() %>%
resp_body_json()
spot_tbl <-
resp %>%
tibble(resp = resp) %>%
unnest_wider(resp) %>%
select(-.)
# find the time zone for each spot, via the association that the spot is in
spot_tbl_tz <-
spot_tbl %>%
left_join(assoc_tbl_tz %>%
select(associationCode,
associationName,
timezone),
join_by("associationCode")) %>%
mutate(timeStamp = str_replace_all(timeStamp, "T", " ")) %>%
mutate(timeStamp1 = as_datetime(timeStamp,
format = "%Y-%m-%d %H:%M:%OS",
tz = "GMT"))
# convert UTC times from the API to local times
# we cannot have multiple time zones in one vector
# so this stays as a list
tzs <- vector("list", length = nrow(spot_tbl_tz))
for(i in 1:nrow(spot_tbl_tz)){
tzs[[i]] <- with_tz(spot_tbl_tz$timeStamp1[i],
tzone = spot_tbl_tz$timezone[i])
}
# convert time to HMS only
spot_local_time <-
map_vec(tzs,
~as_hms(.x))
# 50% of spots occur between these local times
fifty_perc <- quantile(spot_local_time, c(0.25, 0.75))
# plot
spot_tbl_tz %>%
mutate(local_time = spot_local_time) %>%
ggplot() +
aes(local_time) +
geom_histogram() +
geom_vline(xintercept = fifty_perc, colour = "red") +
theme_minimal() +
xlab("Local time\n50% of spots occur between the red lines")
@benmarwick
Copy link
Author

benmarwick commented Nov 4, 2023

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment