Introduction to PyQGIS

2. Calculate the volume below a specific elevation

Before we're going to use a QGIS Processing Tool with PyQGIS, we're going to use the tool first through the interface to get familiar with it. In this tutorial we'll use the Raster surface volume tool tool to calculate the volume below a specific elevation in a DTM.

1. Start the Anaconda prompt, activate the Tutorials environment

conda activate tutorials

2. Run QGIS from the command line, by typing


3. In the main menu go to Project | Open From | GeoPackage.

4. In the Load project from GeoPackage dialogue browse to the provided data_stagevolume.gpkg GeoPackage and open the StageVolume project.

This project has an SRTM 1-Arc Second DEM reprojected to UTM Zone 32N and clipped to the area around the open pit mine. The DEM has been styled with a colour ramp and is blended with a rendered hillshade.

5. Go to Processing | Toolbox to open the Processing Toolbox panel.

6. In the Processing Toolbox choose Raster analysis | Raster surface volume.

7. In the Raster Surface Volume dialogue choose DTM as Input layer, keep Band number as it is and here we keep Base level 0 m, because we want to calculate the volume that is below 0 m. We do that by changing the Method to Count Only Below Base Level. The Surface volume report is an html file. Give it the name volumebelowzero.html. The Surface volume table is a dbf file. We can't choose a dbf file so we save it as volumebelowzero.shp.

8. Click Run.

The Log tab gives the result: 'VOLUME' : -1155480399.7627096

It also gives an error in red. This is because it doesn't create a shapefile but a dbf file. So you can ignore this error.

9. Click Close to close the dialogue.

At the bottom of the Processing Toolbox you can find the Results Viewer.

10. Double click on the Surface volume report or the html link (it links to the same). It will open in your browser. Check the results.

11. Drag the volumebelowzero.dbf file from the Browser panel to the map canvas.

12. Click right on the volumebelowzero table in the Layers panel and choose Open Attribute Table to check the result.

13. Repeat from step 5 for some other elevations.

In the next section we're going to calculate the volumes for many elevations and store the values in one table.

Watch here the result until this step: