Can geopandas get a geopackage's (or other vector file) all layers?
Asked Answered
A

1

14

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
Aerostatics answered 16/5, 2019 at 9:19 Comment(3)
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: 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.Consumable
Yes, Now I use osgeo list all layers , Then use read_file loop all layers.Aerostatics
This answer might help as well #54562569Finedraw
Z
22

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
Zoomorphism answered 17/9, 2019 at 9:32 Comment(2)
Is this still the best solution? Or is there a solution with pure geopandas ( I know, fiona is part of geopandas)?Kaon
If you DO know the layernames, geopandas has the option to open them by name through geopandas.read_file("package.gpkg", layer='layername'). geopandas.org/en/stable/docs/user_guide/io.htmlGorlicki

© 2022 - 2024 — McMap. All rights reserved.