Writing raster to GeoPackage layer


Ryan
 

I'm trying to add a raster layer to an existing GPKG with rasterio:

properties = {
        'driver':'GPKG',
        'width':array.shape[1],
        'height':array.shape[0],
        'count':1,
        'crs':crs,
        'transform':transform,
        'dtype':np.float32,
        'nodata':-9999,
        'layer':'rasterlayer'}
with rasterio.open('testing.gpkg', 'r+', **properties) as dst_dataset:
    dst_dataset.write(array)

However I get the following error:

Traceback (most recent call last):
  File "rasterio/_base.pyx", line 76, in rasterio._base.get_dataset_driver
  File "rasterio/_err.pyx", line 205, in rasterio._err.exc_wrap_pointer
rasterio._err.CPLE_OpenFailedError: 'testing.gpkg' not recognized as a supported file format.

I can do this with "gdal_translate" so I at least know the GPKG and rasters are all properly formatted:

gdal_translate -of GPKG raster.tiff testing.gpkg -co APPEND_SUBDATASET=YES -co RASTER_TABLE=rasterlayer


Side Question: Is there a way to format code in groups.io? I can't seem to find anything on the edit toolbar :-/


Sean Gillies
 

Hi,

Can you tell us how you installed rasterio? If you installed one of the binary wheels from PyPI (which I built), you may be out of luck. I haven't paid any attention to making sure that they have support for geopackage rasters. I hope someone else can report whether they do or do not.

Also note that when you open an existing dataset in r+ mode, all of the keyword arguments you provide will be ignored, so the properties dict you've made in your code is unnecessary.


On Thu, Jun 20, 2019 at 12:48 PM Ryan <code@...> wrote:
I'm trying to add a raster layer to an existing GPKG with rasterio:

properties = {
        'driver':'GPKG',
        'width':array.shape[1],
        'height':array.shape[0],
        'count':1,
        'crs':crs,
        'transform':transform,
        'dtype':np.float32,
        'nodata':-9999,
        'layer':'rasterlayer'}
with rasterio.open('testing.gpkg', 'r+', **properties) as dst_dataset:
    dst_dataset.write(array)

However I get the following error:

Traceback (most recent call last):
  File "rasterio/_base.pyx", line 76, in rasterio._base.get_dataset_driver
  File "rasterio/_err.pyx", line 205, in rasterio._err.exc_wrap_pointer
rasterio._err.CPLE_OpenFailedError: 'testing.gpkg' not recognized as a supported file format.

I can do this with "gdal_translate" so I at least know the GPKG and rasters are all properly formatted:

gdal_translate -of GPKG raster.tiff testing.gpkg -co APPEND_SUBDATASET=YES -co RASTER_TABLE=rasterlayer


Side Question: Is there a way to format code in groups.io? I can't seem to find anything on the edit toolbar :-/



--
Sean Gillies


Ryan
 

Installed rasterio through conda:

conda create -n gpkg-test -c conda-forge python=3.7 rasterio


Sean Gillies
 

It may be that rasterio doesn't support multilayer raster datasets. I'll make a note to open an issue in the tracker about this. 


On Sat, Jun 29, 2019, 10:19 AM Ryan <code@...> wrote:
Installed rasterio through conda:

conda create -n gpkg-test -c conda-forge python=3.7 rasterio


Sean Gillies
 

Ryan,

I don't use geopackage rasters and haven't tested the code I'm about to suggest, but I think this is worth a try:

with rasterio.open("testing.gpkg", "r+", raster_table="new_table", append_subdataset=True) as dst_dataset:
    dst_dataset.write(array)

The format driver has a big set of options and it's possible that the ones above are not entirely sufficient for your purposes. See

On Sat, Jun 29, 2019 at 1:11 PM Sean Gillies via Groups.Io <sean.gillies=gmail.com@groups.io> wrote:
It may be that rasterio doesn't support multilayer raster datasets. I'll make a note to open an issue in the tracker about this. 

On Sat, Jun 29, 2019, 10:19 AM Ryan <code@...> wrote:
Installed rasterio through conda:

conda create -n gpkg-test -c conda-forge python=3.7 rasterio

--
Sean Gillies


Ryan
 

Thanks Sean! Here is a full example, using your suggestion, but still getting the same file format error:

import rasterio
import fiona
from shapely.geometry import Polygon, mapping
raster_fn = 'rasterio/tests/data/RGB.byte.tif'
out_fn = '/tmp/vector-and-raster.gpkg'
with rasterio.open(raster_fn) as r_ds:
    array = r_ds.read()
    l,b,r,t = r_ds.bounds
    crs = r_ds.crs
poly = Polygon([(l,b),(r,b),(r,t),(l,t)])
fiona_config = {
    'schema':{
        'geometry':'Polygon',
        'properties': {('name','str')}
    },
    'driver':'GPKG',
    'crs':crs,
}
with fiona.open(out_fn, 'w', **fiona_config) as ds:
    rec = {
        'properties': {'name':raster_fn},
        'geometry': mapping(poly)
    }
    ds.write(rec)
with rasterio.open(out_fn, "r+", raster_table="new_table", append_subdataset=True) as dst_dataset:
    dst_dataset.write(array)

The GPKG is created and valid. Just fails on the rasterio.open call.