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

R 4.2.3中使用rgdal包将sp对象写入Shapefile失败的问题

问题描述

rgdal包无法正常工作。我使用了writeOGR函数:

writeOGR(occ_sp, species_set[i], driver = "ESRI Shapefile", dsn = dsn, overwrite = TRUE)

但出现如下错误:

Error in writeOGR(occ_sp, species_set[i], driver = "ESRI Shapefile", dsn = dsn, :
could not find function "writeOGR"

我尝试使用sf::st_write()保存我的SpatialPointsDataFrame为Shapefile:

st_write(occ_sp,"occ_sp.shp")

但又出现错误:

Error in UseMethod("st_write") :
no applicable method for 'st_write' applied to an object of class "c('SpatialPointsDataFrame', 'SpatialPoints', 'Spatial', 'SpatialVector')"

解决方案

解决「找不到writeOGR」的问题

writeOGR是rgdal包的函数,需先安装并加载rgdal:

install.packages("rgdal")
library(rgdal)

注意:rgdal已被官方弃用,后续不再维护,优先推荐使用sf包替代。

解决「st_write不支持SpatialPointsDataFrame」的问题

st_write仅支持sf类对象,你的数据是sp包的旧类型SpatialPointsDataFrame,需先转换为sf对象再保存:

# 安装并加载sf包
install.packages("sf")
library(sf)

# 转换数据类型
occ_sf <- st_as_sf(occ_sp)

# 保存为Shapefile
st_write(occ_sf, "occ_sp.shp", driver = "ESRI Shapefile", overwrite = TRUE)

额外提示

如果坚持使用rgdal,除了加载包之外,还要确保本地环境正确配置了GDAL依赖,但由于rgdal已停止维护,sf方案在兼容性和后续更新上更有保障。

内容的提问来源于stack exchange,提问作者A. Aldow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 13:05:00