You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中将USNG/MGRS网格坐标转换为经纬度的方法

解析USNG格式位置并转换为经纬度/sf对象的解决方案

解决mgrs包安装失败的问题

USNG(美国国家网格)与MGRS(军事网格参考系统)格式兼容,mgrs包是处理这类坐标的常用工具。若安装失败,可按以下步骤排查:

  1. 先安装devtools(用于从GitHub安装R包):
install.packages("devtools")
  1. 安装系统依赖(根据你的操作系统):

    • Windows:安装Rtools,确保勾选"Add Rtools to system PATH"选项。
    • macOS:通过Homebrew安装GDAL和PROJ:
      brew install gdal proj
      
    • Ubuntu/Debian:安装系统级依赖库:
      sudo apt-get install libgdal-dev libproj-dev libgeos-dev
      
  2. 重新安装mgrs包:

devtools::install_github("hrbrmstr/mgrs")

转换USNG到经纬度并生成sf对象

安装完成后,可按以下步骤处理你的坐标数据:

步骤1:加载所需包

library(mgrs)
library(sf)

步骤2:预处理USNG字符串

USNG格式包含空格,需先去除空格以适配mgrs包的函数要求:

locations <- c("17T PH 8482 7766", "18T XL 6257 1639", "18T XL 6244 1636", "18T XL 6267 1673", "18T XL 6272 1671", "18T XL 6272 1674")
locations_clean <- gsub(" ", "", locations) # 去除所有空格

步骤3:转换为经纬度

使用mgrs_to_latlng函数将MGRS/USNG坐标转换为WGS84经纬度:

latlng_df <- mgrs_to_latlng(locations_clean)
# 查看转换后的经纬度数据框
print(latlng_df)

步骤4:转换为sf对象

将经纬度数据框转换为sf点对象(坐标系为WGS84,EPSG:4326):

sf_points <- st_as_sf(latlng_df, coords = c("lng", "lat"), crs = 4326)
# 查看sf对象
print(sf_points)

替代方案(无需mgrs包)

若仍无法安装mgrs包,可直接使用sf包结合PROJ的MGRS解析功能(需较新版本的sf和PROJ支持):

library(sf)

# 定义转换函数
usng_to_sf <- function(usng_str) {
  mgrs_clean <- gsub(" ", "", usng_str)
  # 使用PROJ的MGRS文本格式创建点
  point <- st_point_from_text(paste0("MGRS:", mgrs_clean), crs = 4326)
  return(st_sfc(point))
}

# 批量转换并生成sf对象
sf_points <- lapply(locations, usng_to_sf) %>%
  st_as_sf() %>%
  mutate(usng = locations) # 添加原始USNG标识

print(sf_points)

内容的提问来源于stack exchange,提问作者Sebastiano De Bona

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 18:05:28