[GIS] Creating geopackage from multiple raster files

gdalgeopackagegeotiff-tiffqgis

I've got a bunch of geotiffs' that I would like to combine into a single geopackage. Using QGIS, I've only managed to export one layer as a geopackage. I've tried merging the layers and created a virtual layer but then QGIS messes up the colors.
The raster files are also arranged in a Arcgis Mosaic dataset (file geodatabase) if that is of any help.

Currently, I do the following (which probably isnt the fastest or most efficient way):

  1. Generate a .txt-file containing the filenames of all the geotiffs (dir /b /s *.tif > list.txt)
  2. Use gdal_merge to merge all the geotiffs from list.txt into one large geotiff (gdal_merge -pct –optfile list.txt -o new_huge_geotiff.tif)
  3. Use gdal_translate to convert my new, huge, geotiff into a geopackage (gdal_translate -of GPKG new_huge_geotiff.tif new_tiny_geopackage.gpkg)
  4. use gdaladdo to build pyramids in my new geopackage, which I guess also could be done in the previous step when creating the geopackage. I'm just such a newbie. (gdaladdo -r cubic -oo tile_format=JPEG new_tiny_geopackage.gpkg 2 4 6 8 16 32 64)

The process of merging all small geotiffs into one huge takes a lot of time – I wish I could just add them all to a geopackage at once. I've tried using –optfile and the generated txt-list but gdal_translate doesnt like that command.

Best Answer

New raster tables must be added into existing GeoPackage as subdatasets as documented in the GDAL driver manual page http://www.gdal.org/drv_geopackage_raster.html. Have a look at the fifth example

Addition of a new subdataset to an existing GeoPackage, and choose a non default name for the raster table.

gdal_translate -of GPKG new.tif existing.gpkg -co APPEND_SUBDATASET=YES -co RASTER_TABLE=new_table

There is no user interface in QGIS for giving the extra parameters so you must edit the automatically generated gdal_translate command manually. For example if you have an existing GeoPackage "test.gpkg" and you want to add image "Img_Sample.tif" as a new raster table named as "Img_Sample_2" edit the command to look like this:

enter image description here

If you have lots of images to convert i would suggest to forget QGIS and graphical user interface and use gdal_translate directly from command line with a little script.