Skip to main content
added 234 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

I have investigated this code an create a script like it works very well for exporting raster layer to geopackageGeoPackage:

ds = ogr.Open(self.projectGpkg, True)
source = QgsRasterLayer(layerSource, 'rasterLayer', 'gdal')
if source.isValid():
    provider = source.dataProvider()
    fw = QgsRasterFileWriter(self.projectGpkg)
    fw.setOutputFormat('gpkg')
    fw.setCreateOptions(["RASTER_TABLE=" + str(tableName), 'APPEND_SUBDATASET=YES'])

    pipe = QgsRasterPipe()
    if pipe.set(provider.clone()) is True:
        projector = QgsRasterProjector()
        projector.setCrs(provider.crs(), provider.crs())
        if pipe.insert(2, projector) is True:
            if fw.writeRaster(pipe, provider.xSize(),provider.ySize(),provider.extent(),provider.crs()) == 0:
                print("ok")
            else:
                print("error")
ds = None
from osgeo import ogr
from qgis.core import QgsRasterLayer, QgsRasterFileWriter, QgsRasterPipe, QgsRasterProjector

ds = ogr.Open(self.projectGpkg, True)
source = QgsRasterLayer(layerSource, 'rasterLayer', 'gdal')

if source.isValid():
    provider = source.dataProvider()
    fw = QgsRasterFileWriter(self.projectGpkg)
    fw.setOutputFormat('gpkg')
    fw.setCreateOptions(["RASTER_TABLE=" + str(tableName), 'APPEND_SUBDATASET=YES'])

    pipe = QgsRasterPipe()
    if pipe.set(provider.clone()) is True:
        projector = QgsRasterProjector()
        projector.setCrs(provider.crs(), provider.crs())
        if pipe.insert(2, projector) is True:
            if fw.writeRaster(pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs()) == 0:
                print("ok")
            else:
                print("error")
ds = None

For reading raster layer in geopackageGeoPackage:

rlayer = QgsRasterLayer('GPKG:' + str(self.projectGpkg) + ':tableName', "layerName")
from qgis.core import QgsRasterLayer

rlayer = QgsRasterLayer('GPKG:' + str(self.projectGpkg) + ':tableName', "layerName")

I have investigated this code an create a script like it works very well for exporting raster layer to geopackage:

ds = ogr.Open(self.projectGpkg, True)
source = QgsRasterLayer(layerSource, 'rasterLayer', 'gdal')
if source.isValid():
    provider = source.dataProvider()
    fw = QgsRasterFileWriter(self.projectGpkg)
    fw.setOutputFormat('gpkg')
    fw.setCreateOptions(["RASTER_TABLE=" + str(tableName), 'APPEND_SUBDATASET=YES'])

    pipe = QgsRasterPipe()
    if pipe.set(provider.clone()) is True:
        projector = QgsRasterProjector()
        projector.setCrs(provider.crs(), provider.crs())
        if pipe.insert(2, projector) is True:
            if fw.writeRaster(pipe, provider.xSize(),provider.ySize(),provider.extent(),provider.crs()) == 0:
                print("ok")
            else:
                print("error")
ds = None

For reading raster layer in geopackage:

rlayer = QgsRasterLayer('GPKG:' + str(self.projectGpkg) + ':tableName', "layerName")

I have investigated this code an create a script like it works very well for exporting raster layer to GeoPackage:

from osgeo import ogr
from qgis.core import QgsRasterLayer, QgsRasterFileWriter, QgsRasterPipe, QgsRasterProjector

ds = ogr.Open(self.projectGpkg, True)
source = QgsRasterLayer(layerSource, 'rasterLayer', 'gdal')

if source.isValid():
    provider = source.dataProvider()
    fw = QgsRasterFileWriter(self.projectGpkg)
    fw.setOutputFormat('gpkg')
    fw.setCreateOptions(["RASTER_TABLE=" + str(tableName), 'APPEND_SUBDATASET=YES'])

    pipe = QgsRasterPipe()
    if pipe.set(provider.clone()) is True:
        projector = QgsRasterProjector()
        projector.setCrs(provider.crs(), provider.crs())
        if pipe.insert(2, projector) is True:
            if fw.writeRaster(pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs()) == 0:
                print("ok")
            else:
                print("error")
ds = None

For reading raster layer in GeoPackage:

from qgis.core import QgsRasterLayer

rlayer = QgsRasterLayer('GPKG:' + str(self.projectGpkg) + ':tableName', "layerName")
Source Link
Mustafa Uçar
  • 1.6k
  • 18
  • 29

I have investigated this code an create a script like it works very well for exporting raster layer to geopackage:

ds = ogr.Open(self.projectGpkg, True)
source = QgsRasterLayer(layerSource, 'rasterLayer', 'gdal')
if source.isValid():
    provider = source.dataProvider()
    fw = QgsRasterFileWriter(self.projectGpkg)
    fw.setOutputFormat('gpkg')
    fw.setCreateOptions(["RASTER_TABLE=" + str(tableName), 'APPEND_SUBDATASET=YES'])

    pipe = QgsRasterPipe()
    if pipe.set(provider.clone()) is True:
        projector = QgsRasterProjector()
        projector.setCrs(provider.crs(), provider.crs())
        if pipe.insert(2, projector) is True:
            if fw.writeRaster(pipe, provider.xSize(),provider.ySize(),provider.extent(),provider.crs()) == 0:
                print("ok")
            else:
                print("error")
ds = None

For reading raster layer in geopackage:

rlayer = QgsRasterLayer('GPKG:' + str(self.projectGpkg) + ':tableName', "layerName")