A profile shows elevation variation along a line in two dimensional space. We use it for many applications such as: to know the elevation variation for a street, getting to know a landscape height variation between two points, pipe line or sewer planning, and so on.
Figure 1. Elevation Profile
For this tutorial we are using QGIS processing and matplotlib library. Therefore we need to import those two libraries as following.
Define Input/Output Data
Next, we need to define input and output data. The input data which are required to create elevation profile are elevation in raster format and a line in vector format. Both input data must be in the same projection and preferred in a projected coordinate system. The output data are coming from processing result that involved in the process which are points along the path line and points with extracted elevation data from elevation raster data. In this tutorial I'm using elevation data from LiDAR (If you are interested how to make DEM from LiDAR check out this tutorial) and a line path as shown in figure 2.
|Figure 2. Elevation and Line Data|
The following code is used to define input and output data. For input data, I used DEM data from lidar and a line path in geopackage file type. The last two lines are the output data, which are actually temporary data, that can be removed after the process is done.
Processing and Data Conversion
There are two processing steps that involved in this process. The first one is to convert the path line into a series of points along the line with a specified interval. This process is done using v.to.points tool from GRASS module. The second one is to extract elevation information from elevation data for each points. For this, the Add raster values to points tool from SAGA is used. The output for each process is stored in respected output file that already defined before.
The code below shows the processing steps using each tool. To generate points
along the path line with an interval, we need an interval value. Actually we
can define any positive value for interval as long as it's not exceed the
length of the line. But for this tutorial, I used the x resolution of the
elevation data. To get the x resolution can be done with
rasterUnitPerPixel method from a Qgs raster layer. Therefore firstly we
need to define a raster layer using QgsRasterLayer class.
Extract Elevation Data and Calculate Distance
After we get the points with it's elevation data. Next we we will extract each elevation value and calculate the distance for each vertex from start node to end node. The distance and elevation data will be dumped into a list which is called z_list and list_distance. The process will be done in a loop for each point starting at line 22 in the following code. But before that, we need to set up some pre-processing steps such as define a vector layer and getting it's first feature to get start node coordinate and it's elevation. Then Initialize list and dictionary to store two points (start and to point) and initialize a class variable using QgsDistanceArea class and measureLine method with two points in the dictionary for distance calculation. Keep in mind to define a datum or ellipsoid in the distance calculation, with the datum as in your input data. For this case I'm using elevation data with North American Datum 1983(NAD83).
Plotting The Profile
After getting a distance and elevation for each point along the line. Now we
are ready to plot it. For plotting we are using matplotlib library which
already imported in the first step. The following code is using for profile
That's all the tutorial how to create elevation profile using python in QGIS
3. If you want to explore more tutorials about QGIS Python programming, please
QGIS Python Programming Tutorial Series
for other interesting topics. Thanks for reading and happy coding!