When using PyQgis for QGIS 3.4 is it possible to add a new WMS/WMTS Server to the Data Source Manager?

If yes and once created, is it then possible to add a new WMS layer to the map using the new connection within a Python plugin?

I have been trying to add a layer using authentication but my server needs to pre-authenticate so that is not working and in any case the only way it works using the normal GUI is to select the two ignore GetCapabilities options in the Data Source Manager anyway so it is unlikely it would work via QgsRasterLayer anyway.


Whilst I have not yet discovered how to add full server connections I did discover that my issue when trying to add layers individually was because I needed QGIS to ignore the capabilities when constructing URLs.

This is most likely fairly specific to our own server configuration but it might be useful to others to know how to set the two options in the "Create a New WMS/WMTS Connection" dialog via pyqgis.

It turns out they can be added directly as parameters (see QGIS Documentation).

The options you need are:

  • IgnoreGetFeatureInfoUrl
  • IgnoreGetMapUrl

In the format described in the linked documentation page these would be set as follows:

quri.setParam("IgnoreGetFeatureInfoUrl", '1') quri.setParam("IgnoreGetMapUrl", '1')

Using these settings it was possible to add the layers individually as QgsRasterLayer layers.

  • Paul ... may i ask you whether u succeeded in getting a bit further with programmaticaly adding a server connection ? Thanks !
    – Snaileater
    Nov 27 '19 at 15:44
  • It turns out that the next release of GeoServer will include a fix for the capabilities statement. This means it will not put :443 into the URLs in capabilities document and should mean I don't need to use the Ignore options anymore. Dec 3 '19 at 14:57

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.