Rendering is an action to draw a data (usually geometric data which consists of point, line and polygon) both 2D and 3D by means of a computer program. It occurs when we want to view a data on a screen. In simple way when a data is added to a screen for example in QGIS map canvas, the rendering process is taking place to draw the data on the screen.
There are many types of data rendering/visualization can be used in QGIS to get more insight or meaning from a data like graduated, rule based, heatmap, categorized, 3D, etc. In this tutorial we will see how to visualize a data in 2.5 dimension (2.5D) like figure 1 below.
Figure 1. 2.5D Data Visualization
To make a 2.5D data visualization is quite simple using Symbology menu in the data properties. Because this is about PyQGIS tutorial, I won't discuss how to do it manually and I'm sure you can make it. But we will see how to do it using Python code.
The code for 2.5D rendering consist of three parts:
- Define a vector layer.
- Set 2.5D symbol renderer including it's options/methods.
- Add the vector data to the map.
Let's get started!
Define Vector Layer
2.5D visualization works with vector data. Therefore firstly we need to define a vector layer using QgsVectorLayer class as following code. For this tutorial I used a vector in Geopackage format which is called building_level.gpkg.
Next we determine the 2.5D symbol renderer with Qgs25DRenderer class. Set the building vector layer symbol to it and set some methods like roof color and wall color. For rendering we also need the height in map unit and angle of features (0 degree is west and rotates in counter clockwise). It can be defined using QgsExpressionContextUtils class wiht setLayerVariable method as in line 8-10 in the following code.
In the code I only used two renderer methods (roof and wall color). In fact there are many methods available such as: setShadowColor, shadowEnabled, shadowSpred, startRender, stopRender, and many more. You can find the complete 2.5D renderer methods in PyQGIS Documentation.
After finishing the 2.5D symbol rendering and it's methods, now let visualize it into QGIS map canvas using addMapLayer.
That's it. After running the code. The building vector layer will be rendered as shown in figure 1.
2.5D Multi Layers Rendering
We had discussed how to make 2.5D rendering with a single layer. In this section we will see how to render multiple layers. In this case, I'd like to render different roof color and height for building based on it's level that stored in attribute table as seen in figure 2. Each building's level is multiplied by 10, thus level 1 is 10 in map unit, level 2 is 20 and so forth.
Figure 2. Vector Layer Attribute Table
Rendering process takes place for a layer (a group of features). Because of that we need to filter buildings based on it's level. The filtering can be done using a Structured Query Language (SQL) syntax as follow:
SELECT 'buildings_level'.'geom', 'buildings_level'.'building:levels' FROM 'buildings_level' WHERE 'buildings_level'.'building:levels=??
The building level starts from 1 to 3. For that we need to make a loop for filtering each level, set 2.5D symbol renderer, determine it's roof color including height and add it into QGIS map canvas.
Below is the code for multiple 2.5D layers rendering.
After executed the code, the building will be rendered in different roof color and height as seen in figure 3.
That's all this tutorial how to make 2.5D data rendering/visualization for vector layer. Hope this tutorial could help you in learning QGIS with Python. For other interesting PyQGIS tutorials please visit PyQGIS Tutorial Series. Thanks for reading!