16

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?

pngbrianb
  • 193
  • 1
  • 2
  • 6

1 Answers1

22

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. result of the script above

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)

second example using loop

martinfleis
  • 7,124
  • 2
  • 22
  • 30
  • Thanks! This will give me a good start. I've run into other issues that merit their own Questions, but once I get things rolling this will be a much better path forward than what I was doing. – pngbrianb Feb 07 '19 at 18:23
  • Follow-up: now I'm trying to do stuff from GeoDataFrames with lots of _rows_ instead of layers... things got complicated. That question's over here https://gis.stackexchange.com/questions/311646/how-to-make-a-map-based-on-how-many-layers-overlap-at-each-point-in-python if you have any ideas for it... Thanks again! – pngbrianb Feb 09 '19 at 23:44