Storing WMTS rasters into GPKG database🔗

Posted by Médéric Ribreux 🗓 In blog/ Qgis/



WMTS is well supported under QGIS. But sometimes you want to use it offline. Why (ab)using online resources while you can download it and use it later ? Furthermore, since about 10 years, we can store raster data into a gpkg file. What is cool with this format is that it is (nearly) universally supported by any modern GIS software. And you can store multiple raster and vector data into a single file. Pretty convenient for exchange and use it as a centralized database (for local stuff of course).

This article introduces a method to capture WMTS raster data into a gpkg file.

Find WMTS stuff

First things first, you got to find a WMTS source of data. Be careful to read the conditions about accessing this data: most of the time, you will have to use it with a fair usage. It means that if you try to download all of tiles at high level zooms, you can be expelled from the service (most of the time IPv4 banning) for abusing it.

You have to be careful.

Build a WMTS definition file

Once data found, you need to build a GDAL WMTS definition file. It is an XML file which does handle WMTS parameters for requesting tiles. Here is a sample for the official SIA OACI maps stored on the french Geoportail web site:

  <Projection>EPSG:3857</Projection >
  <UserAgent>Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.7113.93 Safari/537.36</UserAgent>

You need to provide the following:

Import into gpkg

Once written, you simply use the XML file as a datasource under gdal_translate. Every parameters is set in the XML file so gdal_translate parameters are not so complicated to use:

gdal_translate -of GPKG ./geoportail_oaci.xml ./vfr.gpkg -co RASTER_TABLE=sia_oaci -co TILE_FORMAT=JPEG -co APPEND_SUBDATASET=YES
gdaladdo -r cubic -oo TILE_FORMAT=JPEG -oo TABLE=sia_oaci vfr.gpkg 2 4 8 16 32 64

Then you just open the gpkg file in QGIS and choose a layer.