4

How do I add a WMS layer on a map, in a standalone Python script with QGIS 2.4?

The methodology explained at the bottom of the official QGIS documentation page seems to be outdated and does not work: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html

1 Answer 1

6

As far as I can see, the example in the documentation is working, but not with the NASA JPL service. It says that NASA has stopped providing WMS support. You just have to look for another WMS service and fill in the required parameters:

urlWithParams = 'url=http://kaart.maaamet.ee/wms/alus&format=image/png&layers=MA-ALUS&styles=&crs=EPSG:3301'
rlayer = QgsRasterLayer(urlWithParams, 'MA-ALUS', 'wms')
QgsMapLayerRegistry.instance().addMapLayer(rlayer)

Be cautious, as if you don't fill out one of these parameters, because your WMS doesn't have one, QGIS will crash. In my example, the MA-ALUS layer doesn't have a style attribute, but I had to include it as an empty parameter.

Result:

Eesti_wms

UPDATE:

It works with QGIS 2.6.0, yaay!

from qgis.core import *

urlWithParams = 'url=http://kaart.maaamet.ee/wms/alus&format=image/png&layers=MA-ALUS&styles=&crs=EPSG:3301'
rlayer = QgsRasterLayer(urlWithParams, 'MA-ALUS', 'wms')
QgsMapLayerRegistry.instance().addMapLayer(rlayer)

UPDATE for QGIS 3:

It works with QGIS 3.2, after accounting for a https://qgis.org/api/api_break.html -- yaay!

from qgis.core import *

urlWithParams = 'url=http://kaart.maaamet.ee/wms/alus&format=image/png&layers=MA-ALUS&styles=&crs=EPSG:3301'
rlayer = QgsRasterLayer(urlWithParams, 'MA-ALUS', 'wms')
QgsProject.instance().addMapLayer(rlayer)
5
  • Thanks Gabor! It works if I run it in the QGIS python console, but if I do the same on a stand alone script I get this error message: QThreadStorage: Thread 0x20a4d40 exited after QThreadStorage 5 destroyed. Then, the output map displays all the layers I want, except the WMS.
    – Andreampa
    Oct 6, 2014 at 10:02
  • 1
    This might be an issue with the Qt implementation in QGIS. I'll try to look at it later, but you might need an answer from someone, who is more familiar with Qt. Oct 6, 2014 at 12:30
  • The Styles parameter is required for all WMS GetMap requests, unless you supply an SLD parameter. styles& is sufficient to get the default style for the layer.
    – nmtoken
    Mar 28, 2018 at 17:32
  • Your URL with params seems to be missing core WMS parameters, like service=WMS&, request=GetMap&, version=1.3.0&, width=[num]&, height=[num]. Are these added on the fly by QGIS?
    – nmtoken
    Mar 28, 2018 at 17:38
  • Also your service endpoint should be separated from the params by a question mark not an ampersand like: http://kaart.maaamet.ee/wms/alus?format=image/png&, note too that the WMS specification tells us that the Ampersand follows the key/value pair so we should always expect an Ampersand at the end of a list of params.
    – nmtoken
    Mar 28, 2018 at 17:41

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