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

如何在R中解压.zip文件并读取.shp文件?报错解决

问题解决方法

错误原因分析

  1. 参数类型不匹配:readOGR的dsn参数要求传入字符型的文件路径/目录,而unz()返回的是文件连接对象,不符合参数要求,导致报错is.character(dsn) is not TRUE。
  2. 下载链接错误:你使用的GitHub链接是网页展示链接,并非直接的文件下载链接,下载的内容是网页HTML而非有效zip文件,后续解压和读取都会失败。

解决方案

方案一:使用rgdal包(原工具链)

library(rgdal)

# 替换为GitHub raw文件下载链接(去掉blob/路径)
zip_url <- "https://github.com/JovaniSouza/JovaniSouza5/raw/ac61a729b3856112bcb100f4dfb2c900565fdb54/gasoduto1.zip"
temp_zip <- tempfile(fileext = ".zip")
# 以二进制模式下载zip文件
download.file(zip_url, temp_zip, mode = "wb")

# 创建临时目录存放解压文件
temp_dir <- tempdir()
unzip(temp_zip, exdir = temp_dir)

# 读取shp文件:dsn为文件所在目录,layer为shp文件名(不带.shp后缀)
gasoduto <- readOGR(dsn = temp_dir, layer = "gasoduto_SIRGAS_2000")

# 验证结果
plot(gasoduto)

方案二:使用sf包(现代空间数据处理推荐工具)

sf是当前R生态中更便捷的空间数据处理包,支持直接读取压缩包内的shp文件:

library(sf)

zip_url <- "https://github.com/JovaniSouza/JovaniSouza5/raw/ac61a729b3856112bcb100f4dfb2c900565fdb54/gasoduto1.zip"
temp_zip <- tempfile(fileext = ".zip")
download.file(zip_url, temp_zip, mode = "wb")

# 直接读取压缩包内的shp文件,无需手动解压
gasoduto <- st_read(unz(temp_zip, "gasoduto_SIRGAS_2000.shp"))

# 验证结果
plot(st_geometry(gasoduto))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 04:15:29