Skip to main content
3 of 3
fix a typo
Jan Pisl
  • 746
  • 7
  • 22

Copying raster to SQLite using GDAL?

I want to copy a raster file into SQLite database. Previously, I did the same with vector data like this:

def store_vector_output(self, file_name, identifier):
    from osgeo import ogr
    drv = ogr.GetDriverByName("SQLite")
    dsc_out = drv.CreateDataSource(self.dblocation)
    layer = dsc_out.CopyLayer(dsc_in.GetLayer(), identifier,
                              ['OVERWRITE=YES'])
    
    dsc_out.Destroy()
    dsc_in.Destroy()

With raster data, I expected it to be similar:

def store_raster_output(self, file_name, identifier):
    from osgeo import gdal

    dsc_in = gdal.Open(file_name)
    drv = gdal.GetDriverByName("SQLite")
    dsc_out = drv.Create(self.dblocation, xsize=30, ysize=30)
    layer = dsc_out.CopyLayer(dsc_in.GetLayer(), identifier,
                              ['OVERWRITE=YES'])
 
    dsc_out.Destroy()
    dsc_in.Destroy()

However, in store_raster_output, dsc_in.GetLayer() is None. There are no layers in dsc_in (its datatype is <class 'osgeo.gdal.Dataset'>)

So, I tried to use dsc_in.CreateLayer("some_name") but that resulted in this:

ERROR 6: CreateLayer() not supported by this dataset.

Therer is also a method ReadRaster() but since I am using CopyLayer the argument must be of datatype layer.

How do I write a raster file to SQLite?

Perhaps using a different command than CopyLayer?

I didn't find any such command here but maybe I just didn't know where to look.

Jan Pisl
  • 746
  • 7
  • 22