R interface to GeographicLib for precise geodetic calculations on the WGS84 ellipsoid.
library(geographiclib)
# Coordinates to MGRS
pts <- cbind(lon = c(147.32, -74.01, 0.13),
lat = c(-42.88, 40.71, 51.51))
(codes <- mgrs_fwd(pts))
#> [1] "55GEN2613352461" "18TWL8362507036" "31UCT0084710446"
# MGRS back to coordinates with full metadata
mgrs_rev(codes)
#> lon lat x y zone northp precision convergence
#> 1 147.3200014 -42.88 526133.5 5252462 55 FALSE 5 -0.2177510
#> 2 -74.0099941 40.71 583625.5 4507036 18 TRUE 5 0.6457496
#> 3 0.1299966 51.51 300847.5 5710446 31 TRUE 5 -2.2471333
#> scale grid_zone square_100km crs
#> 1 0.9996084 55G EN EPSG:32755
#> 2 0.9996861 18T WL EPSG:32618
#> 3 1.0000870 31U CT EPSG:32631
## arguments are vectorized
rev0 <- mgrs_rev(vcodes <- mgrs_fwd(rbind(pts, pts), precision = c(0L, 1L, 2L, 3L, 4L, 5L)))
cbind(mgrs = vcodes, rev0)
#> mgrs lon lat x y zone northp precision
#> 1 55GEN 147.6124506 -42.90097 550000.0 5250000 55 FALSE 0
#> 2 18TWL80 -73.9940004 40.69152 585000.0 4505000 18 TRUE 1
#> 3 31UCT0010 0.1249653 51.51035 300500.0 5710500 31 TRUE 2
#> 4 55GEN261524 147.3202040 -42.88010 526150.0 5252450 55 FALSE 3
#> 5 18TWL83620703 -74.0100003 40.70999 583625.0 4507035 18 TRUE 4
#> 6 31UCT0084710446 0.1299966 51.51000 300847.5 5710446 31 TRUE 5
#> convergence scale grid_zone square_100km crs
#> 1 -0.4169241 0.9996308 55G EN EPSG:32755
#> 2 0.6559370 0.9996889 18T WL EPSG:32618
#> 3 -2.2510864 1.0000887 31U CT EPSG:32631
#> 4 -0.2178893 0.9996084 55G EN EPSG:32755
#> 5 0.6457454 0.9996861 18T WL EPSG:32618
#> 6 -2.2471333 1.0000870 31U CT EPSG:32631Function reference and vignettes are available at https://hypertidy.github.io/geographiclib/
Articles:
Fast C++ implementation - process tens of thousands of coordinates in milliseconds:
x <- do.call(cbind, maps::world.cities[c("long", "lat")])
system.time(mgrs_fwd(x)) # 43,645 points
#> user system elapsed
#> 0.04 0.00 0.04Please note that the geographiclib project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.