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

closed as off-topic by PolyGeo Oct 25 at 8:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

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)
  • 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 '14 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. – Gabor Farkas Oct 6 '14 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 '18 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 '18 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 '18 at 17:41

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