From 3d005fdc82585a0272105ffd3dcf0b79aa5aab03 Mon Sep 17 00:00:00 2001 From: mattbk Date: Sun, 1 Mar 2026 09:51:13 -0600 Subject: [PATCH] Init. --- .gitignore | 4 +++ farpn-map.Rproj | 13 ++++++++ src/farpn-map.R | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 farpn-map.Rproj create mode 100644 src/farpn-map.R diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/farpn-map.Rproj b/farpn-map.Rproj new file mode 100644 index 0000000..066341e --- /dev/null +++ b/farpn-map.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 4 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/src/farpn-map.R b/src/farpn-map.R new file mode 100644 index 0000000..a71580d --- /dev/null +++ b/src/farpn-map.R @@ -0,0 +1,88 @@ + +library(jsonlite) +library(sf) +library(dplyr) +library(tidyr) +library(ggplot2) +library(geosphere) +library(rnaturalearth) + +# API URL +api_spots <- "http://[300:6f6a:b087:4dda::8]/api/v1/spots" + +# Get the spots +spots_raw <- fromJSON(api_spots) %>% mutate(across(c(de_longitude, de_latitude, + dx_longitude, dx_latitude), + as.numeric)) + +# Convert to spatial +spots <- spots_raw %>% st_as_sf(coords = c("dx_longitude", "dx_latitude"), + crs = "EPSG:4326") +spotters <- spots_raw %>% st_as_sf(coords = c("de_longitude", "de_latitude"), + crs = "EPSG:4326") +# Make lines +# https://stackoverflow.com/a/76197010 +lines <- bind_rows(spots, spotters) %>% + group_by(id) %>% + dplyr::summarize(do_union=FALSE) %>% # do_union=FALSE doesn't work as well + st_cast("LINESTRING") + +# Make geodesic lines +great_circles_list <- list() +for(i in 1:nrow(spots_raw)){ + great_circles_list[[i]] <- as.data.frame(gcIntermediate(c(spots_raw[i,"dx_longitude"], spots_raw[i,"dx_latitude"]), + c(spots_raw[i,"de_longitude"], spots_raw[i,"de_latitude"]))) + names(great_circles_list)[[i]] <- spots_raw[i,"id"] +} +great_circles <- bind_rows(great_circles_list, + .id = "id") %>% + st_as_sf(coords = c("lon", "lat"), + crs = "EPSG:4326") %>% + group_by(id) %>% + dplyr::summarize(do_union=FALSE) %>% # do_union=FALSE doesn't work as well + st_cast("LINESTRING") %>% + left_join(spots_raw, by = "id") + +station_bbox <- matrix(c(st_bbox(spots), + st_bbox(spotters)), + nrow = 4, + ncol = 2) %>% + t() %>% + as.data.frame() %>% + summarize(xmin = min(V1), + ymin = min(V2), + xmax = max(V3), + ymax = max(V4)) + + +# Pull in GIS basemaps etc. +countries <- ne_download( + scale = 50, + type = "admin_0_countries", + category = "cultural", + returnclass = "sf" + ) +borders <- ne_download( + scale = 50, + type = "admin_1_states_provinces", + category = "cultural", + returnclass = "sf" + ) + +# Make simple map +ggplot() + + geom_sf(data = countries) + + geom_sf(data = borders) + + geom_sf(data = great_circles, + aes(color = band)) + + geom_sf(data = spots) + + geom_sf(data = spotters, + color = "green") + + geom_sf_text(data = spotters, + aes(label = de_call)) + + coord_sf(crs = "EPSG:4269", + xlim = c(station_bbox$xmin, station_bbox$xmax), + ylim = c(station_bbox$ymin, station_bbox$ymax)) + + theme_bw() + + \ No newline at end of file