I'm using the following code to load a WMS layer to QGIS :

layer_name = "RGE ALTI"
provider = "wms"

layers = QgsProject.instance().mapLayersByName('bbox')
layer = layers[0]
extent = layer.extent()
bbox_shp = f'{extent.xMinimum()},{extent.yMinimum()},{extent.xMaximum()},{extent.yMaximum()}'

url += bbox_shp

layer = QgsRasterLayer(url, layer_name, provider)

if layer.isValid():
    print("Layer added successfully!")

    print("Failed to load layer.")

The layer doesn't load, but when I copy paste the url in a browser, I am able to download the corresponding layer. Anyone has had a similar problem ? N.B : There is no problem with the bounding box shapefile

Edit: I've added the "url=" as mentioned in Loading WMS layer to QGIS using Python, however this does not fix the issue

  • 2000 by 2000 is pretty big for a WMS image, maybe it's too big?
    – Ian Turton
    Apr 9 at 9:40
  • I'm not really familiar with adequate resolutions for WMS images, mind explaining?
    – aogino
    Apr 9 at 9:44
  • check what the response from the server is - if it is xml then you need read the error and adjust accordingly. Does it work when you add it via the GUI?
    – Ian Turton
    Apr 9 at 10:47
  • I dont know how to check the server's response, is there a thread that shows how ? It works using the GUI, but only the png format is visible, when i choose tiff the layer is created but doesn't appear in the canvas.
    – aogino
    Apr 9 at 11:53
  • but what is odd is that copy pasting this url : "data.geopf.fr/wms-r/…" directly on the browser makes it download the right layer
    – aogino
    Apr 9 at 11:59


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.