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

寻求获取ISO国家代码对应经纬度的R语言技术方案

解决方案

方案1:使用rnaturalearth包获取官方地理数据

这个包基于自然地球的公开地理数据集,覆盖几乎所有ISO 3166-1 alpha-3编码的地区(包括开曼群岛这类自治领地),无需额外注册,数据直接内置,完全适配leaflet开发需求。

代码示例:

# 安装依赖包
install.packages(c("rnaturalearth", "rnaturalearthdata", "dplyr", "readr"))

# 加载包
library(rnaturalearth)
library(dplyr)
library(readr)

# 获取包含ISO3代码与经纬度的地区数据
ne_locations <- ne_countries(returnclass = "sf", scale = "medium") %>%
  select(iso_a3, name, lat = latitude, lon = longitude)

# 读取你的数据集并合并
country_all_codes <- read_csv(url("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv"))
final_data <- country_all_codes %>%
  left_join(ne_locations, by = c("alpha-3" = "iso_a3"))

scale参数可选small/medium/large,对应不同精度的地理数据,按需选择即可。

方案2:用tidygeocoder补充缺失经纬度

如果现有数据集仅部分地区缺失坐标,可使用tidygeocoder调用免费的OpenStreetMap Nominatim服务(无需注册账号),批量补充经纬度。

代码示例:

# 安装依赖包
install.packages(c("tidygeocoder", "dplyr", "readr"))

# 加载包
library(tidygeocoder)
library(dplyr)
library(readr)

# 读取你的数据集
country_all_codes <- read_csv(url("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv"))

# 筛选缺失坐标的地区,调用Nominatim获取数据并合并
filled_data <- country_all_codes %>%
  filter(is.na(latitude) | is.na(longitude)) %>%
  geocode(address = name, method = "osm", lat = latitude, long = longitude) %>%
  bind_rows(country_all_codes %>% filter(!is.na(latitude) & !is.na(longitude)))

# 整理列顺序,确保ISO3代码对应
filled_data <- filled_data %>%
  select(`alpha-3`, name, latitude, longitude, everything())

注意:Nominatim有请求频率限制,批量处理时建议避免短时间内发送大量请求,可适当添加延迟(比如用Sys.sleep(1)循环处理)。

方案3:手动合并自然地球CSV数据

如果不想依赖R包,可直接下载自然地球的公开CSV数据集(包含ISO3代码、地区名称、中心经纬度),读取后与你的现有数据按ISO3代码合并即可。

以上方案均无需谷歌账号,处理后的坐标数据可直接用于leaflet地图开发。

内容的提问来源于stack exchange,提问作者Marie Veit

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 04:55:13