使用R的sf与rnaturalearth包无法拉取湖泊几何数据求助
解决rnaturalearth下载五大湖数据的404错误
我刚测试了你的代码,确实能复现这个问题——核心原因是rnaturalearth包生成的下载URL出现了重复拼接的错误,导致请求的地址是http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_lakes.zip这种无效路径,自然会返回404 NotFound。
下面是几个可行的解决办法:
1. 手动下载并加载数据(临时快速修复)
直接使用Natural Earth的正确URL手动下载数据,再导入R中处理:
# 手动下载正确的湖泊数据压缩包 download.file( url = "https://www.naturalearthdata.com/download/110m/physical/ne_110m_lakes.zip", destfile = "ne_110m_lakes.zip" ) # 解压文件 unzip("ne_110m_lakes.zip") # 读取为sf对象并过滤五大湖 lakes <- sf::st_read("ne_110m_lakes.shp", crs = 4269) %>% dplyr::filter(name_alt == "Great Lakes") # 清理临时文件(可选) file.remove("ne_110m_lakes.zip", "ne_110m_lakes.shp", "ne_110m_lakes.shx", "ne_110m_lakes.dbf", "ne_110m_lakes.prj")
2. 安装rnaturalearth的开发版
这个URL拼接的bug大概率已经在包的开发分支被修复了,你可以尝试安装GitHub上的最新版本:
# 如果没安装remotes先安装 install.packages("remotes") # 安装开发版rnaturalearth remotes::install_github("ropensci/rnaturalearth") # 重试你的原代码(记得先加载包) library(rnaturalearth) library(sf) library(dplyr) lakes <- ne_download(scale = 110, type = 'lakes', category = 'physical') %>% st_as_sf(crs = 4269) %>% filter(name_alt == "Great Lakes")
3. 替代方案:使用geodata包获取湖泊数据
如果上述方法都暂时无法生效,你可以用geodata包作为替代,它同样提供Natural Earth的地理数据,且URL配置正常:
# 安装并加载geodata install.packages("geodata") library(geodata) library(sf) library(dplyr) # 获取110m分辨率的全球湖泊数据 lakes <- geodata::lakes(scale = 110, path = tempdir()) %>% st_transform(crs = 4269) %>% filter(name_alt == "Great Lakes")
你在Natural Earth仓库提交的issue确实反映了这个问题,目前官方还未回复,不过上面的方法应该能帮你暂时绕过这个障碍。
内容的提问来源于stack exchange,提问作者Quinterpret
相关产品推荐
相关产品推荐

