Search code examples
rrgdal

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


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"     

Solution

  • 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")