0

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.

3
  • 1
    The raster driver of the SpatiaLite family is "rasterlite". Find examples from github.com/OSGeo/gdal/blob/master/autotest/gdrivers/…. GeoPackege raster could be a better choise at the moment, rasterlite is under development and unstable.
    – user30184
    Commented Jun 21, 2018 at 19:38
  • I am sorry but I don't entirely understand what you are suggesting. I don't explicitly use RasterLite, or do I? If I am to use GeoPackage instead, where do I say so? Could you please explain it a little bit more in detail?
    – Jan Pisl
    Commented Jun 21, 2018 at 21:46
  • 3
    SQLite driver is a vector driver and you can't use it for rasters. Select some raster driver that stores data into SQlite: rasterlite, gpkg, or mbtiles.
    – user30184
    Commented Jun 22, 2018 at 6:38

2 Answers 2

2

You can do that with GDAL Compiled binaries (.exe) no python. Just have binaries somewhere and open a CMD there and write command.

CMD for translate to Spatialite:

"C:\path\to\gdal_translate_exe\gdal_translate" -of Rasterlite C:\job\data\MNT_Ombre_32E03SO.tif C:/job/data/test.sqlite"

Example CMD to translate in Geopackage:

"C:\OSGeo4W\bin\gdal_translate" -of GPKG MNT.tif test.gpkg -co APPEND_SUBDATASET=YES -co RASTER_TABLE=MNT_TEST

GDAL binaries can come with OSGEO install: https://trac.osgeo.org/osgeo4w/wiki or can be find here http://www.gisinternals.com/release.php

0

Use gdal_translate to create an mbtiles (tns),or gpkg (XYZ) tiles then use gdaladdo to add ZOOM LEVELS. Or use gdal2tiles_parallel and mbutil

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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