Skip to content

Instantly share code, notes, and snippets.

@paleolimbot
Created May 23, 2020 03:37
Show Gist options
  • Save paleolimbot/93023ffde9398005780f772b6b6341f4 to your computer and use it in GitHub Desktop.
Save paleolimbot/93023ffde9398005780f772b6b6341f4 to your computer and use it in GitHub Desktop.
library(libs2)
make_hemisphere <- function(lon = 0, detail = 100, epsilon = 1) {
lat_values <- seq(90, -90, length.out = detail)
coord_df <- unique(
data.frame(
lon = c(rep(lon - 90 + 1, detail), rep(lon + 90 - 1, detail)),
lat = c(lat_values, rev(lat_values))
)
)
coords <- paste0(coord_df$lon, " ", coord_df$lat, collapse = ", ")
sprintf("POLYGON ((%s))", coords)
}
plot_hemisphere <- function(geog, lon = 0) {
hemisphere <- s2geography(make_hemisphere(lon))
geog_hemisphere <- s2_intersection(geog, hemisphere)
geog_hemisphere_wkb <- wk::new_wk_wkb(s2_asbinary(geog_hemisphere))
geog_hemisphere_coords <- wk::wkb_coords(geog_hemisphere_wkb, sep_na = TRUE)
geog_hemisphere_proj <- mapproj::mapproject(
geog_hemisphere_coords$x,
geog_hemisphere_coords$y,
projection = "orthographic",
orientation = c(0, lon, 0)
)
withr::with_par(list(mai = c(0, 0, 0, 0), omi = c(0, 0, 0, 0)), {
plot(
double(), double(),
xlim = c(-1, 1),
ylim = c(-1, 1),
asp = 1
)
polypath(geog_hemisphere_proj)
})
}
countries <- s2data_countries()
ragg::agg_png()
for (x in seq(0, 360, length.out = 99)) {
message(x)
try(plot_hemisphere(countries, x))
}
dev.off()
gifski::gifski(list.files(pattern = "Rplot"), width = 480, height = 480, loop = TRUE, delay = 1/25)
@paleolimbot
Copy link
Author

animation

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