如何让 rgdal 打开地理数据库 (.gdb) 文件?

How do I get rgdal to open a geodatabase (.gdb) file?

我正在尝试使用 rgdal::readOGR 读取 .gdb 文件。几天前,我终于通过删除路径末尾的尾随“/”和文件名末尾的 .gdb 使其正常工作。然后,噗!今天我的代码不起作用。据我所知,一切都没有改变!

我的代码失败后,我尝试更新所有内容,所以我现在 运行:Mac OS10.14.4、RStudio v1.2.1335、R v3.6.0 和 rgdal v1.4 -3。我检查过 rgdal 有 OpenFileGDB 驱动程序。我也试过将 .gdb 扩展名添加到文件名,并更改 .lyr 文件名以匹配该 .gdb 文件名,但没有任何效果。

这是我正在使用的代码...

gdb <- path.expand("Data/GIS/CA_LandUse2014/ds2677")
luca <- readOGR(gdb, "ds2677")

这是错误消息:

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open data source

这些是 ds2677 文件夹中的文件:

> list.files(path.expand("Data/GIS/CA_LandUse2014/ds2677"))
[1] "ds2677_LandUse2014.lyr" "ds2677.gdb"        

这些是 ds2677.gdb 中的文件。

> list.files(path.expand("Data/GIS/CA_LandUse2014/ds2677/ds2677.gdb"))
 [1] "a00000001.freelist"                          "a00000001.gdbindexes"                       
 [3] "a00000001.gdbtable"                          "a00000001.gdbtablx"                         
 [5] "a00000001.TablesByName.atx"                  "a00000002.gdbtable"                         
 [7] "a00000002.gdbtablx"                          "a00000003.gdbindexes"                       
 [9] "a00000003.gdbtable"                          "a00000003.gdbtablx"                         
[11] "a00000004.CatItemsByPhysicalName.atx"        "a00000004.CatItemsByType.atx"               
[13] "a00000004.FDO_UUID.atx"                      "a00000004.freelist"                         
[15] "a00000004.gdbindexes"                        "a00000004.gdbtable"                         
[17] "a00000004.gdbtablx"                          "a00000004.spx"                              
[19] "a00000005.CatItemTypesByName.atx"            "a00000005.CatItemTypesByParentTypeID.atx"   
[21] "a00000005.CatItemTypesByUUID.atx"            "a00000005.gdbindexes"                       
[23] "a00000005.gdbtable"                          "a00000005.gdbtablx"                         
[25] "a00000006.CatRelsByDestinationID.atx"        "a00000006.CatRelsByOriginID.atx"            
[27] "a00000006.CatRelsByType.atx"                 "a00000006.FDO_UUID.atx"                     
[29] "a00000006.freelist"                          "a00000006.gdbindexes"                       
[31] "a00000006.gdbtable"                          "a00000006.gdbtablx"                         
[33] "a00000007.CatRelTypesByBackwardLabel.atx"    "a00000007.CatRelTypesByDestItemTypeID.atx"  
[35] "a00000007.CatRelTypesByForwardLabel.atx"     "a00000007.CatRelTypesByName.atx"            
[37] "a00000007.CatRelTypesByOriginItemTypeID.atx" "a00000007.CatRelTypesByUUID.atx"            
[39] "a00000007.gdbindexes"                        "a00000007.gdbtable"                         
[41] "a00000007.gdbtablx"                          "a0000000a.FDO_GlobalID.atx"                 
[43] "a0000000a.gdbindexes"                        "a0000000a.gdbtable"                         
[45] "a0000000a.gdbtablx"                          "a0000000a.spx"                              
[47] "gdb"                                         "timestamps"     

请帮忙!

我没有使用过 rgdal,但与空间数据交互的另一个可靠替代方案是 sf 包。要从地理数据库加载,您可以使用 st_read(dsn, layer) 例如

library(sf)
luca <- st_read("data/mygeodatabase.gdb", layer = "layername")

ogrListLayers(src)告诉你什么?我认为 src 需要成为包括 ds2677.gdb 的路径。

这对我有用 - RI_geodatabase_wetlands.gdb 是包含所有 a0000001.etc 文件的文件夹:

> ogrListLayers("/data/gdb/RI_geodatabase_wetlands.gdb")
[1] "Rhode_Island"                  "RI_Wetlands"                  
[3] "RI_Wetlands_Project_Metadata"  "RI_Wetlands_Historic_Map_Info"
attr(,"driver")
[1] "OpenFileGDB"
attr(,"nlayers")
[1] 4

现在我有了图层名称我可以一次读取一个图层:

> RI = readOGR("/data/gdb/RI_geodatabase_wetlands.gdb","Rhode_Island")
OGR data source with driver: OpenFileGDB 
Source: "/data/gdb/RI_geodatabase_wetlands.gdb", layer: "Rhode_Island"
with 1 features
It has 5 fields

查看您在此处尝试的内容:

gdb <- path.expand("Data/GIS/CA_LandUse2014/ds2677")
luca <- readOGR(gdb, "ds2677")

我想你应该这样做:

gdb <- path.expand("Data/GIS/CA_LandUse2014/ds2677/ds2677.gdb")    
ogrListLayers(gdb)

然后选择你想要的图层名称并做:

luca <- readOGR(gdb, "some_layer_name")

这就是我在 Windows 10 上使用 rgdal 1.4-4 驱动程序在没有 ARC-GIS 许可证的情况下在 R 3.60 中的处理方式。请注意,我正在将 *.gdbtable 文件转换为 shp 文件,最终用于 Google Earth Pro。只有一个 gdbtable 文件没有 'null geometry' 或我感兴趣的字段。所以我对编号的文件不感兴趣。但是 ogrInfo 证明 "a0000000b.gdbtable" 很有趣。警告:我只是发布我是如何解决这个问题的。很多时候,我发现 R 中的 GIS 代码非常特别,以至于我懒得像我应该的那样去理解库的功能。我刚刚让它运行良好,可以给我一个 ESRI shp 文件进行导入。

dir(pattern="*gdbtable")
[1] "a00000001.gdbtable" "a00000002.gdbtable" "a00000003.gdbtable" "a00000004.gdbtable" "a00000005.gdbtable" "a00000006.gdbtable" "a00000007.gdbtable" "a0000000b.gdbtable"


library(data.table)
library(rgdal) #rgdal_1.4-4

sessionInfo()  
nrow(ogrDrivers())
as.data.table(ogrDrivers())[grepl("ESRI",long_name),]
cat('
             name                 long_name write  copy isVector
1: ESRI Shapefile            ESRI Shapefile  TRUE FALSE     TRUE
2:    OpenFileGDB              ESRI FileGDB FALSE FALSE     TRUE
')

 setwd("C:/Users/username/Downloads/EAP_ENV_AmbientStations/EAP_ENV_AmbientStations.gdb")
ogrInfo("a0000000b.gdbtable")
cat('
Source: "C:\Users\username\Downloads\EAP_ENV_AmbientStations\EAP_ENV_AmbientStations.gdb\a0000000b.gdbtable", layer: "EAP_ENV_AmbientStations"
Driver: OpenFileGDB; number of rows: 557 
Feature type: wkbPoint with 2 dimensions
Extent: (623276.8 93472.33) - (2533932 1367270)
CRS: +proj=lcc +lat_1=47.33333333333334 +lat_2=45.83333333333334 +lat_0=45.33333333333334 +lon_0=-120.5 +x_0=500000.0001016001 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs  
Number of fields: 22 
         name type length typeName
1     STATION    4      6   String
2     STANAME    4     35   String
3        WBID    4     10   String
... ')


a0000000b <-  readOGR("a0000000b.gdbtable")
writeOGR(a0000000b,dsn="EAP_ENV_AmbientStations.gdb",layer="EAP_ENV_AmbientStations_b",driver="ESRI Shapefile")

as.data.table(data.frame(a0000000b))[,names(.SD)]
 [1] "STATION"    "STANAME"    "WBID"       "DECOREGN"   "ECORGN"     "WATERSHED"  "GLACIAL"    "SPECIALCRI" "CORE"       "STRUCT"     "SURROUND"   "LOCATIONTY" "ELEV"       "RMI"        "LAT_DD"     "LONG_DD"    "TRS"        "QUAD"       "COUNTY"    
[20] "CONGDIST"   "LEGDIST"    "CLASS"      "coords.x1"  "coords.x2"  "optional" 

bubble(a0000000b['LAT_DD'], col=rgb(0.5,0.5,1,0.5))