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

R中下载的SF多边形数据在绘制地图时不可见问题求助

R语言BIEN树种分布地图无显示问题排查

我正尝试在R语言中创建展示特定树种(Quercus cerris)全球分布范围的地图,使用BIEN数据库及BIEN_ranges_species()函数。已成功绘制世界地图,但树种分布的标记无法显示,推测是分布图层存在异常。

我的代码:

library(BIEN)
library(maps)
library(sf)

species_vector <- c("Quercus_cerris")
BIEN_ranges_species(species_vector)
BIEN_ranges_species(species_vector, match_names_only = TRUE)
temp_dir <- file.path("C:/Users/Nele/Documents/R_Karten")
BIEN_ranges_species(species = species_vector,
                directory = temp_dir)
BIEN_ranges_species("Quercus_cerris")
BIEN_ranges_species("Quercus_cerris",
                directory = temp_dir)

Quercus_test <- st_read(dsn = temp_dir,
                    layer = "Quercus_cerris")

plot(Quercus_test[1])
map('world', fill = TRUE, col = "grey")

plot(Quercus_test[1],
 col = "red",
 add = TRUE)

此外,单独执行以下代码绘制Quercus cerris的数据时,也无可见内容:

plot(Quercus_test[1],
 col = "red",
 add = TRUE)

排查与解决方法

  • 确认数据加载有效性:
    运行str(Quercus_test)查看数据结构,检查geometry列是否存在有效空间数据。再用st_is_empty(Quercus_test)判断是否为空几何对象,若为空则说明下载或读取失败,需重新调用BIEN_ranges_species()。
  • 统一坐标系:
    maps包绘制的世界地图默认采用WGS84(EPSG:4326)坐标系,而BIEN下载的分布数据可能使用其他CRS。执行以下代码转换坐标系后再绘制:
    # 查看当前坐标系
    st_crs(Quercus_test)
    # 转换为WGS84
    Quercus_test <- st_transform(Quercus_test, crs = 4326)
    
  • 简化下载逻辑:
    重复调用BIEN_ranges_species()可能导致文件损坏或覆盖,保留一次下载代码即可:
    temp_dir <- file.path("C:/Users/Nele/Documents/R_Karten")
    # 仅执行一次下载
    BIEN_ranges_species(species = "Quercus_cerris", directory = temp_dir)
    
  • 直接调用几何图层:
    避免用Quercus_test[1]这种索引方式,直接提取几何对象绘制:
    map('world', fill = TRUE, col = "grey")
    plot(st_geometry(Quercus_test), col = "red", add = TRUE)
    
  • 检查本地文件完整性:
    打开temp_dir目录,确认Quercus_cerris.shp、.shx、.dbf、.prj四个核心文件齐全,若有缺失则删除现有文件后重新下载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 21:57:35