QGIS Python Tutorial: Adding Raster Layer

In another post we already discussed how to add vector layer into QGIS with python. In this post we we will talk about how to add raster layer into QGIS. As usual, for this tutorial I'm using QGIS 3.0.

There are several data sources of raster layer that can be added into QGIS such as raster file in local machine and online resources from a server with a Web Mapping Service (WMS) access. Therefore we will discuss how to add a raster layer from both data sources.

Adding Raster File From Local Machine

Firstly let's talking how to add a raster directly from a file that is stored in a local machine.  To add a raster layer into QGIS map canvas can be used iface class with addRasterLayer method. The general expression is as follow.

iface.addRasterLayer(path:string,layer_name:string)

For example, the following code is adding a file with ecw format into QGIS. Other raster type like TIF, JPEG, etc also can be added to QGIS with the same expression. In figure 1 can be seen the raster file that already loaded into QGIS map canvas.

raster_file="E:/drone_UAV.ecw"
iface.addRasterLayer(raster_file,"aerial photo")

Figure 1. A Raster layer added to QGIS map canvas

Adding Online Raster Data Source

Web Map Service (WMS) Layer

To add a raster layer from a WMS service we have to know the address or URL of the layer. After knowing the URL then we need to specified some parameters like layer name to be added, image format and coordinate system in EPSG.

For example, I want to add a layer from Center for International Earth Science Information Network (CIESIN) Columbia University, with the following parameters:
  • URL: https://sedac.ciesin.columbia.edu:443/geoserver/ows?SERVICE=WMS
  • layer name:ipcc:ipcc-synthetic-vulnerability-climate-2005-2050-2100
  • format:image/png
  • crs:EPSG:4326
  • styles
Then we combine the URL and those parameters with ampersand logogram (&) to be: url=https://sedac.ciesin.columbia.edu:443/geoserver/ows?SERVICE=WMS&layers=ipcc:ipcc-synthetic-vulnerability-climate-2005-2050-2100&format=image/png&crs=EPSG:4326&styles=

Next we define it as raster layer with QgsRasterLayer class and set some arguments such as: the url with parameters itself, layer name and data type. To test if the layer can be loaded or not we are using isValid method. This method will return True if the layer can be loaded otherwise False. Then the layer is added to map canvas using addMapLayer method with an instance of QgsProject class.  The complete code can be seen in the following code and the result is shown in figure 2.

#ADDING WMS
urlWithParams = 'url=https://sedac.ciesin.columbia.edu:443/geoserver/ows?SERVICE=WMS&layers=ipcc:ipcc-synthetic-vulnerability-climate-2005-2050-2100&format=image/png&crs=EPSG:4326&styles='
raster_layer = QgsRasterLayer(urlWithParams, 'Climate Vulnerability', 'wms')
if not raster_layer.isValid():
  print("Layer failed to load!")
QgsProject.instance().addMapLayer(raster_layer)

Figure 2

Tile Map Service (TMS)/XYZ Tile

Second part of adding raster layer from online data source is to add a raster layer from a TMS service. TMS or xyz layer type is frequently used to add a basemap such as google maps, Open Street Map (OSM), ESRI, etc. To add a xyz tile is the same with adding a WMS layer, we need a layer address (url) but with different parameters. Those parameters are: zmax(maximum zoom level) and zmin(minimum zoom level).

For example let's add OSM basemap with url: http://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png. maximum zoom level 19 and minimum zoom level 0. So the complete url will be:
http://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png&zmax=19&zmin=0&type=xyz. You can find other TMS URL at NextGIS QMS.

Then we define it as raster layer with QgsRasterLayer class, test it if valid or not and add to QGIS map canvas using addMapLayer method of an QgsProject instance. The complete code can be seen below. In the figure 3 can be seen the OSM basemap layer was added into QGIS map canvas.

#ADDING MTS/XYZ TILE
uri="url=http://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png&zmax=19&zmin=0&type=xyz"
mts_layer=QgsRasterLayer(uri,'OSM','wms')
if not wts_layer.isValid():
    print ("Layer failed to load!")
QgsProject.instance().addMapLayer(mts_layer)
Figure 3
That's all the tutorial how to add raster layer into QGIS map canvas using python. If you want to explore more tutorials about QGIS Python programming, please visit QGIS Python Programming Tutorial Series.

Related Posts

Disqus Comments