1

I'm having a hard time trying to read (with python) a raster that I've saved in a Geopackage.

I thought there would be a simple like rasterData = rasterio.open(gpkgPath, layer='DEM'), but when I run this code, It doesn't show any error, but I can't use read() method (to furthermore compute man and min values) for the object, when I do so, it returns a message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "rasterio\_io.pyx", line 240, in rasterio._io.DatasetReaderBase.read
ValueError: No indexes to read

Another thing I realized is that the other attributes from rasterio DatasetReader object, such width, height and bounds are returning strange values (different from those I see in QGIS).

PS1: There is nothing wrong with the raster (I've already opened it with QGIS software);
PS2: I don't want to use QGIS python packges (I want to use rasterio, gdal, or another package like these).

1

1 Answer 1

4

If your GPKG contains just a single raster, it's as easy as:

with rasterio.open("path/to/input.gpkg") as src:
    print(src.profile)
    data = src.read()

If your GPKG contains multiple rasters, you can pass the raster name using the table arg:

with rasterio.open("path/to/input.gpkg", table="raster_name") as src:
    print(src.profile)
    data = src.read()

Alternatively, they are exposed as subdatasets:

with rasterio.open("GPKG:path/to/input.gpkg:raster_name") as src:
    print(src.profile)
    data = src.read()

If you want to loop through all rasters in a GPKG:

with rasterio.open("path/to/input.gpkg") as gpkg:

    if gpkg.subdatasets:
        for raster in gpkg.subdatasets:
            with rasterio.open(raster) as src:
                print(src.profile)
    else:  # Just one raster
        print(gpkg.profile)
2
  • OMG! Where did you find this information? I've searched everywhere and I couldn't find it. Thanks a lot, it worked very well for me. If there is any source you have consulted to write this answer, could you share? Apr 20 at 12:46
  • 1
    See also edit. I pulled this answer together based on the gdal geopackage docs gdal.org/drivers/raster/gpkg.html
    – user2856
    Apr 20 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.