Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-Layer .gdb files in Python?

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?

like image 415
pngbrianb Avatar asked Feb 06 '19 20:02

pngbrianb


People also ask

Can GeoPandas read GDB?

Yes, GeoPandas support layers.

What is .GDB folder?

The Esri File Geodatabase (FileGDB) is a file-based database for vector and raster data. It can be identified as folder with the suffix .gdb. For example, myDatabase.gdb. It is a file-based database with support for many GIS data types such as points, lines, polygons, 3D geometry (multipatch), raster and more.

What are file geodatabases?

A file geodatabase is a collection of files in a folder on disk that can store, query, and manage spatial and nonspatial data. Create a file geodatabase in a project folder or using the Create File Geodatabase geoprocessing tool.


1 Answers

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

like image 98
martinfleis Avatar answered Oct 18 '22 07:10

martinfleis