Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active January 28, 2021 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/82384d44a10ac7a4844c39ff8035612a to your computer and use it in GitHub Desktop.
Save ThomasG77/82384d44a10ac7a4844c39ff8035612a to your computer and use it in GitHub Desktop.
PyQGIS add WMS layer standalone sample (use capabilities listing from owslib)
# PyQGIS add WMS layer standalone sample
# Before use owslib library (bundled in QGIS) to list WMS layers in capabilities
from owslib.wms import WebMapService
url = 'https://www.gebco.net/data_and_products/gebco_web_services/2020/mapserv'
wms = WebMapService(url, version='1.3.0')
for name, metadata in wms.contents.items():
print(name)
urlWithParams = 'url={}&crs=EPSG:4326&format=image/png&layers=GEBCO_2020_Grid&styles='.format(url)
rlayer = QgsRasterLayer(urlWithParams, 'some layer name', 'wms')
if not rlayer.isValid():
print("Layer failed to load!")
QgsProject.instance().addMapLayer(rlayer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment