Skip to content

Instantly share code, notes, and snippets.

@kkpan11
Forked from dnanto/geomid.R
Created February 13, 2024 02:06
Show Gist options
  • Save kkpan11/a18eae8eb3042ce55fe5fa354c5ed4b0 to your computer and use it in GitHub Desktop.
Save kkpan11/a18eae8eb3042ce55fe5fa354c5ed4b0 to your computer and use it in GitHub Desktop.
Calculate the geographic midpoint given a list of geographic coordinates in R.
library(tidyverse)
geomid <- function(lat, lon)
{
# http://www.geomidpoint.com/calculation.html
lat <- lat * pi / 180
lon <- lon * pi / 180
x <- mean(sum(cos(lat) * cos(lon)))
y <- mean(sum(cos(lat) * sin(lon)))
z <- mean(sum(sin(lat)))
lat <- atan2(y, x)
hyp <- sqrt(x * x + y * y)
lon <- atan2(z, hyp)
c(lat = lat * 180 / pi, lon = lon * 180 / pi)
}
# calculate the geographic midpoint for each county in the US
map_data("county") %>%
mutate(polyname = str_c(region, subregion, sep = ",")) %>%
group_by(polyname) %>%
group_modify(~ {
as.data.frame(t(geomid(.x$lat, .x$long)))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment