I've found tons of topics about how to load a WMS layer in PyQGIS, but nothing about WMTS. In the GUI, adding a WMS or a WMTS layer is done by clicking the same button so I expect the syntax to be close, but I failed to figure it out entirely.

So I took this url (from this post) to make my testing :


It works if you try to add layers from this source using the GUI. I tried to copy the syntax that allows to add a WMS layer (so I added needed parameters like layers=, format=, style=) but my layer is never valid.

layer = QgsRasterLayer('url=http://opencache.statkart.no/gatekeeper/gk/gk.open_wmts?&layers=barentswatch_stedsnavn&Version=1.0.0&service=wmts&format=image/jpeg&styles=', 'anytitle', 'wms')
>> False

Does somebody see the mistake / know the trick (or where I can find documentation about it) ?

  • In geoserver the url looks like this: serverName/geoserver.com/gwc/service/wms?tiled=true Maybe yours is something like this: opencache.statkart.no/gatekeeper/gk/wms?tiled=true – artwork21 Aug 23 '16 at 12:13
  • Each software has got his own syntax regarding OGC services. PyQGIS is quite capricious and only tolerates specific syntaxes (at least, that is what I observed from working with WFS and WMS). So it is normal that the url I built doesn't look like a geoserver one. Nevertheless, I tried to add the "tiled=true" and switch the 'wmts' parameter to 'wms' but without success. Thanks anyway. – El Theo Aug 25 '16 at 15:10

AS WMTS standard (p59) says (or here in French), tileMatrixSet is a required parameter, at least for a client like QGIS which manage the rest of things.

Among things that are good to know, there is no "wmts" option in QgsRasterLayer method. You've have to set "wms". And the request to pass to QGIS is not GetTile, but GetCapabilities. QGIS does its own soup after that.

To perform my tests, I wrote a short script that can be executed in QGIS Python console. Tested on 2.14.x and 2.18.x. I can't guarantee that's work with every WMTS service. Some GetCapabilities return bad encoding XML.

  • the link to the script doesn't exist anymore, please provide code directly in the answer – Denis Rouzaud Oct 9 '19 at 14:00
  • I just updated the missing link. – J. Monticolo Feb 7 '20 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.