Storing WMTS rasters into GPKG database 🔗

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

#GIS

Introduction

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:

<GDAL_WMTS>
  <GetCapabilitiesUrl>https://wxs.ign.fr/an7nvfzojv5wa96dsga5nk8w/geoportail/wmts?Service=WMTS&amp;Request=GetCapabilities&amp;VERSION=1.0.0</GetCapabilitiesUrl>
  <Layer>GEOGRAPHICALGRIDSYSTEMS.MAPS.SCAN-OACI</Layer>
  <Style>normal</Style>
  <TileMatrixSet>PM</TileMatrixSet>
  <ZoomLevel>11</ZoomLevel>
  <Format>image/jpeg</Format>
  <DataWindow>
    <UpperLeftX>-592403.0</UpperLeftX>
    <UpperLeftY>6645000.0</UpperLeftY>
    <LowerRightX>1470650.0</LowerRightX>
    <LowerRightY>5050000.0</LowerRightY>
  </DataWindow>
  <Projection>EPSG:3857</Projection >
  <BandsCount>3</BandsCount>
  <DataType>Byte</DataType>
  <Cache>
    <Path>/home/mribreux/gdalwmscache/</Path>
  </Cache>
  <UnsafeSSL>true</UnsafeSSL>
  <ZeroBlockHttpCodes>204,404</ZeroBlockHttpCodes>
  <ZeroBlockOnServerException>true</ZeroBlockOnServerException>
  <UserAgent>Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.7113.93 Safari/537.36</UserAgent>
</GDAL_WMTS>

You need to provide the following:

This XML definition can also be opened and used directly under QGIS. But most of the time it is much more convenient to reference the WMTS server under QGIS interface directly because QGIS can explore all of the layers of the service and much more information (metadata).

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.