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

如何在R中读取gdb文件?readOGR报错、st_read卡顿如何解决?

R读取.gdb格式文件常见报错解决方案

问题背景

首先运行以下代码查询gdb文件包含的图层:

ogrListLayers("my_data.gdb")

查询结果显示my_data.gdb包含两个大图层,多次尝试读取均失败,已尝试的操作及对应报错如下:

  • 运行以下代码,测试是否添加dropNULLGeometries参数、读取两个不同图层的场景,均返回报错:
Wont_open <- readOGR(dsn = "D:/my_data.gdb", layer =  "layer_1", dropNULLGeometries = F)

报错信息:

Error in readOGR(dsn = "D:/my_data.gdb",  : 
Unsupported field type: Binary
  • 运行以下代码读取图层,程序运行约1小时后无响应:
Wont_open <- st_read(dsn="D:/my_data.gdb", layer =  "layer_1")
  • 自定义读取函数尝试转换格式后读取,分别测试了.gdbtable和.dbf后缀的文件,仍读取失败,收到GDAL未安装的警告:
read_GDB_Layer <- function(dsn, layerName, overwrite = T){
conversionDir <- tempdir() 

gdalUtils:: ogr2ogr(src_datasource_name = dsn, dst_datasource_name = conversionDir, f = "ESRI Shapefile", layer + layerName, verbose = T, overwrite = overwrite) 

df <- read.dbf(file.path(conversionDir, paste0(layerName, ".gdbtable"))) 

return(df)}

调用代码:

Wont_open <- read_GDB_Layer(dsn = "D:/my_data.gdb", layerName = "layer_1")

警告信息:

1: In gdal_setInstallation(search_path = NULL, rescan = FALSE, ignore.full_scan = TRUE,  :
  No GDAL installation found. Please install 'gdal' before continuing:
    - www.gdal.org (no HDF4 support!)
    - trac.osgeo.org/osgeo4w/ (with HDF4 support RECOMMENDED)
    - www.fwtools.maptools.org (with HDF4 support)

2: In gdal_setInstallation(search_path = NULL, rescan = FALSE, ignore.full_scan = TRUE,  :
  If you think GDAL is installed, please run:
gdal_setInstallation(ignore.full_scan=FALSE)

解决方案

基础环境修复(解决GDAL未找到警告)

  • 无需额外安装系统级GDAL,优先使用sf包内置的GDAL组件,可完全替代rgdal和gdalUtils的所有读取功能,规避依赖识别问题。
  • 如必须使用gdalUtils,安装对应系统的GDAL组件后,运行gdalUtils::gdal_setInstallation(ignore.full_scan=FALSE)即可自动识别安装路径。

解决二进制字段不支持报错

  • rgdal::readOGR已停止维护,原生不支持FileGDB中的二进制字段类型,直接弃用该函数即可,改用sf包的读取逻辑。

解决st_read无响应问题

无响应通常由数据量过大、默认读取全量字段(包含大体积二进制字段)、自动几何校验耗时过长导致,通过参数优化即可解决:

# 按需读取指定字段,排除二进制字段,关闭不必要的校验提升速度
Wont_open <- st_read(
  dsn = "D:/my_data.gdb", 
  layer = "layer_1",
  # 替换下方字段名为你实际需要的字段,SHAPE为几何字段默认名,不需要几何数据可删除
  query = "SELECT 字段1, 字段2, SHAPE FROM layer_1", 
  check_ring_dir = FALSE
)
  • 仅需要属性表不需要几何数据时,添加read_geometry = FALSE参数,读取速度可提升数倍:
attr_df <- st_read(
  dsn = "D:/my_data.gdb", 
  layer = "layer_1",
  read_geometry = FALSE
)

格式转换替代方案

无需自定义转换函数,直接用sf内置的导出功能即可转成shp等其他格式:

# 读取完成后直接导出为shp
st_write(Wont_open, "D:/layer_1.shp", delete_layer = TRUE)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 07:24:00