rasterqgispyqgisgeopackage

List all raster layers in Geopackage using pyqgis


With the following code it is super easy to list all vector layers in a geopackage:

my_gpkg = r'PATH_TO_GEOPACKAGE'
gpkg_layers = [l.GetName() for l in ogr.Open(my_gpkg )]

Is there also a way to list all raster layers in a geopackage?


Solution

  • Could solve my problem with the help of this post: https://gis.stackexchange.com/questions/287997/counting-the-number-of-layers-in-a-geopackage

    And here is my solution:

    import sqlite3
    
    my_gpkg = r'PATH_TO_GEOPACKAGE'
                
    sqliteConnection = sqlite3.connect(my_gpkg)
    cursor = sqliteConnection.cursor()
    
    # the table gpkg_contents is mandatory in every geopackage
    sqlite_select_query = """Select table_name from gpkg_contents where data_type = 'tiles'"""
    cursor.execute(sqlite_select_query)
    records = cursor.fetchall()
    raster_layers = []
    for row in records:
        layer_name = row[0]
        raster_layers.append(layer_name)
                
    print('These are the raster layers in your geopackage: {}'.format(raster_layers))
    
    cursor.close()