问题
I'm trying to read in a .gdb file using rgdal:: readOGR. I finally got it to work a few days ago by removing the trailing "/" at the end of the path and the .gdb at the end of the file name. Then, poof! today my code won't work. As far as I can tell nothing has changed!
After my code failed, I tried updating everything, so I'm now running: Mac OS10.14.4, RStudio v1.2.1335, R v3.6.0, and rgdal v1.4-3. I've checked that rgdal has the OpenFileGDB driver. I've also tried adding the .gdb extension to the filename, and changing the .lyr filename to match that .gdb filename, but nothing works.
Here is the code I'm using...
gdb <- path.expand("Data/GIS/CA_LandUse2014/ds2677")
luca <- readOGR(gdb, "ds2677")
And here is the error message:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open data source
These are the files in the ds2677 folder:
> list.files(path.expand("Data/GIS/CA_LandUse2014/ds2677"))
[1] "ds2677_LandUse2014.lyr" "ds2677.gdb"
And these are the files in 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"
Please help!
回答1:
What does ogrListLayers(src)
tell you? I think src
needs to be the path up to and including the ds2677.gdb
.
Here's what works for me - RI_geodatabase_wetlands.gdb
is the folder with all the a0000001.etc
files in:
> 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
Now I have the layer names I can read a layer at a time:
> 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
Looking at what you tried here:
gdb <- path.expand("Data/GIS/CA_LandUse2014/ds2677")
luca <- readOGR(gdb, "ds2677")
I suspect you should do:
gdb <- path.expand("Data/GIS/CA_LandUse2014/ds2677/ds2677.gdb")
ogrListLayers(gdb)
then choose the layer name you want and do:
luca <- readOGR(gdb, "some_layer_name")
回答2:
I haven't used rgdal but another robust alternative for interacting with spatial data is the sf package. To load from a geodatabase you would use st_read(dsn, layer) e.g.
library(sf)
luca <- st_read("data/mygeodatabase.gdb", layer = "layername")
回答3:
This how I went about this in R 3.60 with rgdal 1.4-4 drivers on Windows 10 with no ARC-GIS licenses. Note that I am converting *.gdbtable files to a shp file eventually for use on Google Earth Pro. Only one of gdbtable files did not have 'null geometry' or fields I was interested in. So the numbered files weren't interesting to me. But ogrInfo proved that "a0000000b.gdbtable" was interesting. Caveat: I am just posting how I hacked this out. Much of the time I find GIS code in R idiosyncratic enough that I don't bother to understand the functionally of the libraries like I should. I just get it to work well enough to give me an ESRI shp file for import.
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))
来源:https://stackoverflow.com/questions/55910398/how-do-i-get-rgdal-to-open-a-geodatabase-gdb-file