6

I am trying to import a GDB file database table and convert it to a (Pandas) dataframe. I have been trying to do so using GeoPandas and Fiona. The problem is however that not all columns are being read, and I am not sure why not. I use the following simple code to read the file:

gdbfile = gdp.read_file("path_to_gdb", driver='FileGDB')

Alternatively, I've also used fiona.open() with the same arguments. These two methods do return all of the rows, but do not however return all columns. The 'objectid' column is not read, which I know is in there (when I open the same GDB database table using QGIS, it is included). When I print the GDB database-file's schema with Fiona, it returns:

{'properties': OrderedDict([('CAT_GEWASCATEGORIE', 'str:60'),
              ('GWS_GEWASCODE', 'str:10'),
              ('GEOMETRIE_Length', 'float'),
              ('GEOMETRIE_Area', 'float'),
              ('GWS_GEWAS', 'str:255')]),
 'geometry': 'MultiPolygon'}

and as you can see, 'objectid' is not in it. But when I print gdb.next() using Fiona, it includes 'id' (which content-wise is equal to the objectid):

{'type': 'Feature',
 'id': '1526148',                                               <--the 'objectid'
 'properties': OrderedDict([('CAT_GWS', 'Grassland'),
              ('CODE', '265'),
              ('GEOM_Length', 1362.792),
              ('GEOM_Area', 10739.457),
              ('CROP', 'Grass')]),
 'geometry': {'type': 'MultiPolygon',
  'coordinates': [[[(109397.83300000057, 439262.45899999887),
     (109396.84699999914, 439271.9699999988),
     (109396.09099999815, 439287.80900000036),
 ]]]}}                                                          #I've shortened the poly

When I transform the Fiona's object into a data frame, it is not included. Does anyone know how I can include 'objectid' in the data using GeoPandas (or Fiona)? I've been trying and searching, but can't find anything on this problem with reading GDB database tables. I've been trying to include the 'id' in the schema argument of fiona.open() but that didn't work for me either (though perhaps I did it wrong, I used the schema below and it seemingly ignored it and kept the old schema). I am new to the GDB database filetype.

{'type': 'str:10',
 'id': 'int',
 'properties': {('CAT_GWS', 'str:60'),
               ('CODE', 'str:10'),
               ('GEOM_Length', 'float'),
               ('GEOM_Area', 'float'),
               ('CROP', 'str:255')},
  'geometry': 'MultiPolygon'}
1
  • For me a workaround is to use ogr2ogr to convert the whole file geodatabase to a geopackage then read the individual tables from the geopackage. That way all the columns are included. (ogr2ogr C:\GIS\data\datacopy.gpkg C:\GIS\data\data.fgdb)
    – BERA
    Aug 18 at 12:56

1 Answer 1

4

You could potentially loop over the features yourself and insert that id into the properties, using GeoDataFrame.from_features() to create the dataframe:

def yield_features(path):
    with fiona.open(path, 'r') as f:
        for feature in f:
            feature['properties']['id'] = feature['id']
            yield feature

features = yield_features(r'C:\example.gdb\test')

gdf = GeoDataFrame.from_features(features)

Disclaimer: I haven't tried this and I don't generally use (geo)pandas, so there could be a better way

1
  • Thank you, this worked! It seems that for some reason the combination of gdb being new to me and the way fiona returns files threw me off in this case. Accepted your answer. Mar 3, 2020 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.