Tuesday 25 December 2018

pyqgis - Adding WMS layer in QGIS using Python


I'm trying to write a python script to load the Environment Agency (EA) Flood Zones WMS layers into QGIS (2.14). I've provided a link to the EA's data catalogue at the bottom of this question. The layers I'm hoping to use from the catalogue are "Flood Map for Planning (Rivers and Sea) - Flood Zone 2" and "Flood Map for Planning (Rivers and Sea) - Flood Zone 3".


I have used the code below, copied from a similar question (link at the bottom), but rlayer.isValid() is always returned as False no matter how I edit the url. I can take the url in the example code below and manually add the WMS layers with no issues.



Am I missing something in my url? Or is the issue somewhere else?


Extract from my code:


urlWithParams = 'url=http://www.geostore.com/OGC/OGCInterface?SESSIONID=-2049535737&INTERFACE=ENVIRONMENT&version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=eainspire2011-wms-nat_floodzone2_inspire&format=image/png&STYLE=default'
rlayer = QgsRasterLayer(urlWithParams, 'EA Flood Zone 2', 'wms')
rlayer.isValid()
QgsMapLayerRegistry.instance().addMapLayer(rlayer)

EA Spatial Data Catalogue


Similar question - "WMS layer in a QGIS 2.4 stand alone Python script"





For anyone wondering the correct working links are below:


EA Flood Zone for Planning - Zone 2: http://environment.data.gov.uk/ds/wms?INTERFACE%3DENVIRONMENT--86ec354f-d465-11e4-b09e-f0def148f590


EA Flood Zone for Planning - Zone 3: http://environment.data.gov.uk/ds/wms?INTERFACE%3DENVIRONMENT--87446770-d465-11e4-b97a-f0def148f590



Answer



Your issue is in the URL. I retrieved the "right" one from https://data.gov.uk/dataset/flood-map-for-planning-rivers-and-sea-flood-zone-2


INTERFACE%3DENVIRONMENT--86ec354f-d465-11e4-b09e-f0def148f590 seems to act as a token for authentication to WMS service.


Try below code (tested on my machine)


urlWithParams = 'contextuaWMSLegend=0&crs=EPSG:27700&dpiMode=7&featureCount=10&format=image/png&layers=eainspire2011-wms-nat_floodzone2_inspire&styles=&url=http://environment.data.gov.uk/ds/wms?INTERFACE%3DENVIRONMENT--86ec354f-d465-11e4-b09e-f0def148f590' rlayer = QgsRasterLayer(urlWithParams, 'EA Flood Zone 2', 'wms') rlayer.isValid() QgsMapLayerRegistry.instance().addMapLayer(rlayer)


The result Flood zone 2 EA UK


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...