Created
June 25, 2020 18:14
-
-
Save dnanto/f37a80c5498c6a32ae8f72d7372415f3 to your computer and use it in GitHub Desktop.
Revisions
-
dnanto created this gist
Jun 25, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ 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))) })