13

I want to use geopandas read a geopackage file, It can read the first layer or specific layer with layer='' parameter. But how can it read all layers? May be like:

all_layers = gp.read_xxxx('xxx.gpkg')
for layer in layers:
    # fetch this layer
Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27
wsf1990
  • 195
  • 2
  • 12
  • 1
    That is currently not possible (a single `read_file` call results in a single GeoDataFrame). It would be nice to have some functionality for this, though. Feel free to open an issue on github: https://github.com/geopandas/geopandas. One option might also be to be able to list the layers, so you can loop through those and use read_file within the loop. – joris May 16 '19 at 09:42
  • 1
    Yes, Now I use osgeo list all layers , Then use read_file loop all layers. – wsf1990 May 16 '19 at 10:30
  • 2
    This answer might help as well https://stackoverflow.com/questions/54562069/multi-layer-gdb-files-in-python/54563846#54563846 – martinfleis May 16 '19 at 12:45

1 Answers1

21

You can do this by fiona and itter through the layernames https://fiona.readthedocs.io/en/latest/README.html#reading-multilayer-data

for layername in fiona.listlayers('tests/data.gpkg'):
    with fiona.open('tests/data.gpkg', layer=layername) as src:
        print(layername, len(src))

If you pass it to geopandas:

for layername in fiona.listlayers('test/data.gpkg'):
    geopkg = gpd.read_file(r'test/data.gpkg', layer=layername)
    i = 0
    for name in geopkg.columns:
        print(name)
        i += 1
Xeppit
  • 311
  • 2
  • 7
  • 6
    Is this still the best solution? Or is there a solution with pure geopandas ( I know, fiona is part of geopandas)? – MartinT May 02 '22 at 04:41