5

I loaded a GeoPackage into Python and I can't figure out how to list all the layers in the file. How do I do that?

Input: GeoPackage loaded into Python.

Desired output: A list of the layers in the GeoPackage.

Code for connecting to GeoPackage:

import geopandas as gpd    
gpkg = 'path/to.gpkg'
# Missing: list layers
Zorro
  • 85
  • 1
  • 9
  • 1
    What code did you use to load the Geopackage into Python? – PolyGeo Oct 04 '21 at 04:10
  • Is was less efficient than the code in the answer lol – Zorro Oct 04 '21 at 04:57
  • 1
    That looks like it just sets a string variable to a full path name representing the GeoPackage file. I thought you must have been creating a GeoPackage object using something, and just didn't know how to access a property with its layers . – PolyGeo Oct 04 '21 at 05:30
  • 1
    https://gis.stackexchange.com/a/255232/2856 – user2856 Oct 04 '21 at 07:04

2 Answers2

10

It's simple to do this using Fiona. I prefer using GeoPandas to read the Geopackage, and then Fiona to list the layers:

import geopandas as gpd
import fiona

gpkg = 'path/to.gpkg' layers = fiona.listlayers(gpkg)

You'll end up with the layer names as a list.

Encomium
  • 3,133
  • 2
  • 14
  • 41
2

Using the GeoDjango API you can do the following:

from django.contrib.gis.gdal import DataSource

gpkg = DataSource('path/to.gpkg') layers = [layer.name for layer in gpkg]

StefanBrand_EOX
  • 3,721
  • 13
  • 33