1

Using the "Generate XYZ Tiles" command from "Raster tools" it is possible to create a MBTiles format file containing either PNG or JPG images. I would like to create an MBTiles file containing WebP formatted images. The WebP raster driver is already installed, so is this not possible, a missing UI option, or is there something else I need to do to make this possible?

enter image description here enter image description here

3 Answers 3

5

The Generate XYZ Tiles (MBTiles) tool uses GDAL to generate the output (QGIS <= 3.32)

The GDAL MBTiles driver documentation states it only supports PNG and JPG output, so may only support <= version 1.2 of the MBTiles spec. WebP support was added to the MBTiles spec at v. 1.3.

There is an open GDAL enhancement ticket (#3437) requesting WebP support be added.

So in short, doesn't look like you can enable WebP output with the QGIS Generate XYZ Tiles (MBTiles) tool.

However, maptiler engine supports MBTiles and WebP output and has a free desktop version. I haven't used it so can't comment on it other than that.

5
  • Thanks for all the docs links Commented Nov 3, 2022 at 0:11
  • @MatthewHerbst try maptiler.com/engine
    – user2856
    Commented Nov 3, 2022 at 2:05
  • It appears that GDAL was updated to support WebP back in 2023. However, QGIS still doesn't allow you to create WebP images, even in the latest version (3.38.2). I was unable to find anything obvious from the QGIS project about an open issue here. Since you seem to be knowledgeable about this particular corner of GIS, I was hoping you might have additional knowledge about what prevents WebP from existing in QGIS today.
    – Nick2253
    Commented Sep 5 at 20:46
  • I don't really know. Perhaps raise an issue on the qgis github repo. Although the TilesXYZ.py file was removed in Qgis 3.34 so this answer is not relevant to current releases
    – user2856
    Commented Sep 6 at 22:19
  • The tool was rewritten as a C++ native alg for 3.34 github.com/qgis/QGIS/pull/54321
    – user2856
    Commented Sep 6 at 22:30
1

Under the hood MBTiles is SQLite so you can use SQLite and PIL to convert the images to webp. here is a sketch of how i have done it for tiles that start as png8. I am new to SQLite so feel free to tell me how to tighten it up.

import sqlite3 
import io
from PIL import Image

conn = sqlite3.connect(FullPathToFile)
cursor = conn.cursor()
cursor.execute('SELECT rowid,* FROM tiles')
data = cursor.fetchall()
for row in data:
    tile_data = row[tData] 
    im = Image.open(io.BytesIO(tile_data)) #https://pillow.readthedocs.io/en/stable/handbook/tutorial.html#reading-from-binary-data
    im.save(stream, format="WebP", lossless=True, quality=100, method=5) #https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp
    imagebytes = stream.getvalue()
    try:
        sql_update_query = "UPDATE tiles SET tile_data=? WHERE rowid=?"
        cursor.execute(sql_update_query, (imagebytes,rID))
    except sqlite3.Error as error:
        print("Failed to update sqlite table", error)
0

You can first create tms tiles with gdal

gdal2tiles.py --zoom=18 --tiledriver=WEBP --webp-quality=50 inputFiles.vrt tiles

then use https://github.com/mapbox/mbutil and run

mb-util --image_format=webp --scheme=tms tiles yourOutputfile.mbtiles

Btw: if you want to work with multiple input files, start with the following command to build a virtual mosaic: gdalbuildvrt inputFiles.vrt input/*.jpg

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.