Multi-Layer .gdb files in Python?

2020-07-11 09:29发布

问题:

I'm trying to read in some .gdb files(folders?) from here: .

I use GeoPandas and do the following:

# file from local path
mallard = gpd.read_file('./bird-species/E00039600_mallard.gdb/')
# geopandas included map, filtered to just this hemisphere
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
westhem = world[(world['continent'] == 'North America') | 
                (world['continent'] == 'South America')]

# making sure the coordinates line up:
mallard = mallard.to_crs(world.crs)

#establishing figure axes
base = westhem.plot(color='white', edgecolor='black',figsize=(11,11))

# cmap because I'd LIKE the multiple layers to exist
bbw_duck.plot(ax=base, cmap = 'Reds');

The output looks like this:

lousy map - one color

Is there a way in GeoPandas, or Python (Jupyter Notebook) in general to see all the layers?

回答1:

Yes, GeoPandas support layers. As names of your layers are terribly long, I recommend using the order of layers.

mallard_0 = gpd.read_file('./bird-species/E00039600_mallard.gdb/', layer=0)
mallard_1 = gpd.read_file('./bird-species/E00039600_mallard.gdb/', layer=1)

# geopandas included map, filtered to just this hemisphere
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
westhem = world[(world['continent'] == 'North America') | 
                (world['continent'] == 'South America')]

# making sure the coordinates line up:
mallard_0 = mallard_0.to_crs(world.crs)
mallard_1 = mallard_1.to_crs(world.crs)

# establishing figure axes
base = westhem.plot(color='white', edgecolor='black', figsize=(11, 11))

# cmap because I'd LIKE the multiple layers to exist
mallard_0.plot(ax=base, color='red', alpha=.5)
mallard_1.plot(ax=base, color='blue', alpha=.5)

If you have more of them you can then make a loop to go plot them all at once easily.

Edit: Geopandas is using Fiona under the hood for file handling, so if you want to see the list of your layers use

import fiona
fiona.listlayers('./bird-species/E00039600_mallard.gdb')

Edit2: Loop over all layers will then look like this:

import fiona

# geopandas included map, filtered to just this hemisphere
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
westhem = world[(world['continent'] == 'North America') | 
                (world['continent'] == 'South America')]
base = westhem.plot(color='white', edgecolor='black', figsize=(11, 11))

layers = fiona.listlayers('./bird-species/E00039600_mallard.gdb')

for l in layers:
    mallard = gpd.read_file('./bird-species/E00039600_mallard.gdb', layer=l)
    mallard = mallard.to_crs(world.crs)
    mallard.plot(ax=base, color='red', alpha=.1)