
You can find this information online, especially here PyQGIS Cookbook, but I would also like to document the basics of PyQGIS as I grok it.
Sometimes we would like to bypass the “Add WMS/WMTS” utility and simply use Python to get where we want to go. Opening up the Python editor in QGIS, we can create a script to access the WMS we are looking for.
WMS is an Open Geospatial Consortium (OGS) web service standard that stands for Web Map Service. Every time you run a query, a server is called and produces maps dynamically from georeferenced data. WMTS stands for Web Map Tiles Service. This service uses tiled map data for increased speed of delivery.
In the PyQGIS API, we have to know which layers we want to load, as we can’t access the GetCapabilities in the current API.
To connect we have to first create a variable that includes the parameters we want:
URL_with_parameters = “crs=CRS:84&format=image/png&layers=3DEPElevation:GreyHillshade_elevationFill&styles&url=https://elevation.nationalmap.gov/arcgis/services/3DEPElevation/ImageServer/WMSServer”
Let’s look at that line of code for a second. We have the variable name, then we have the parameters. First parameter is the CRS (84 in this case), separated by the “&” symbol from the other parameters. Then we have the format. If we look at the WMS server page, we can see that the available formats are:
image/tiff
image/png
image/png24
image/png32
image/bmp
image/gif
image/jpeg
image/svg
image/bil
And so we choose image/png. Then we have to choose our layers, which are also shown on the WMS server page.
In this case we are choosing to access the layer 3DEPElevation:GreyHillshade_elevationFill. The styles parameter is empty, and then the url.
In order to load this layer we have to create another variable:
rlayer = QgsRasterLayer(URL_with_parameters, ‘3DEP Elevation’, ‘wms’)
Let’s look at that variable – we have the variable name, and then the QgsRasterLayer() class. The essential form is this QgsRasterLayer(uri: str, baseName: str = ‘’, providerKey: str = ‘’) This is the constructor for the RasterLayer class. It is instantiated using GDAL to provide QGIS with the ability to render raster datasets onto the mapcanvas, supporting any GDAL format, as well as a variable “uri” and name.
In this case uri is our URL_with_parameters variable that includes the parameters and the url, then our name, and then the provider ‘wms’. And then load that variable using:
QgsProject.instance().addMapLayer(rlayer)
This adds the aforementioned maplayer to our project. So then we can save this script to a folder and reuse it as needed.
WMS is RGB data and thus it is unhelpful for analysis. But it can be very useful for visualization purposes, for instance when it comes to visualizing weather or precipitation. You can connect to a WMS server such as The National Weather Service current weather map:
BaseReflectivity = “crs=CRS:84&dpiMode=7&format=image/png&layers=1&styles&url=https://idpgis.ncep.noaa.gov/arcgis/services/NWS_Observations/radar_base_reflectivity/MapServer/WMSServer”
rlayer = QgsRasterLayer(BaseReflectivity, ‘Reflectivity’, ‘wms’)
QgsProject.instance().addMapLayer(rlayer)
Next time I will get into loading vector layers with PyQGIS.
