Loading raster data

Let's build another script to load raster data. Copy the following code:

Hillshade_layer = iface.addRasterLayer('D:/QGIS_quickstart/qgis_sample_data/raster/SR_50M_alaska_nad.tif','Hillshade')

Landcover_layer = iface.addRasterLayer('D:/QGIS_quickstart/qgis_sample_data/raster/landcover.img','Landcover')

## print the layer names

print (Hillshade_layer.name())

print (Landcover_layer.name())

## print the image dimensions

print (Hillshade_layer.width(), Hillshade_layer.height())

print (Landcover_layer.width(), Landcover_layer.height())

The code in this script is loading in two layers, Hillshade and Landcover, printing the names to the console, and printing the image dimensions.

You can download data from here: https://github.com/PacktPublishing/QGIS-Quick-Start-Guide/blob/master/Chapter07/02_Loading_data_raster.py. Be sure to repoint to your data location if different to the script. Click on the Save As button in the editor to save the file to your project as 02_Loading_data_raster.py. Then, click Run (the green triangle). The result should look like the following screenshot:

The script in the Python Console
To explore more about the Raster class, have a look at the documentation here: https://qgis.org/api/classQgsRasterLayer.html.

We can write scripts to automate many processes in QGIS from rendering, map creation, and processing. In fact, all parts of this book could be automated. Let's finish this section on Python and scripting with one last spatial processing script. This time, we will buffer the airports layers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset