如何让rgdal打开地理数据库(.gdb)文件?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何让rgdal打开地理数据库(.gdb)文件?相关的知识,希望对你有一定的参考价值。
我正在尝试使用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"
请帮忙!
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")
我没有使用rgdal,但是与空间数据交互的另一个强大的替代方法是sf包。要从地理数据库加载,您可以使用st_read(dsn,layer),例如
library(sf)
luca <- st_read("data/mygeodatabase.gdb", layer = "layername")
这就是我在R 3.60中使用rgdal 1.4-4驱动程序在Windows 10上没有ARC-GIS许可证的情况。请注意,我正在将* .gdbtable文件转换为shp文件,最终在Google地球专业版中使用。只有一个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:UsersusernameDownloadsEAP_ENV_AmbientStationsEAP_ENV_AmbientStations.gdba0000000b.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))
以上是关于如何让rgdal打开地理数据库(.gdb)文件?的主要内容,如果未能解决你的问题,请参考以下文章
QGIS入门实战精品教程005:QGIS如何打开ArcGIS创建的文件数据库(GDB)?