
Checking for topological errors and fixing them
Sometimes, the data that we receive from different sources or data that results from a chain of spatial processing steps can have problems. Topological errors can be particularly annoying, since they can lead to a multitude of different problems when using the data for analysis and further spatial processing. Therefore, it is important to have tools that can check data for topological errors and to know ways to fix discovered errors.
Finding errors with the Topology Checker
In QGIS, we can use the Topology Checker plugin; it is installed by default and is accessible via the Vector menu Topology Checker entry (if you cannot find the menu entry, you might have to enable the plugin in Plugin Manager). When the plugin is activated, it adds a Topology Checker Panel to the QGIS window. This panel can be used to configure and run different topology checks and will list the detected errors.
To see the Topology Checker in action, we create a temporary scratch layer with polygon geometries and digitize some polygons, as shown in the following screenshot. Make sure you use snapping to create polygons that touch but don't overlap. These could, for example, represent a group of row houses. When the polygons are ready, we can set up the topology rules we want to check for. Click on the Configure button in Topology Checker Panel to open the Topology Rule Settings dialog. Here, we can manage all the topology rules for our project data. For example, in the following screenshot, you can see the rules we might want to configure for our polygon layer, including these:
- Polygons must not overlap each other
- There must not be gaps between polygons
- There shouldn't be any duplicate geometries
Once the rules are set up, we can close the settings dialog and click on the Validate All button in Topology Checker Panel to start running the topology rule checks. If you have been careful while creating the polygons, the checker will not find any errors and the status at the bottom of Topology Checker Panel will display this message: 0 errors were found. Let's change that by introducing some topology errors.
For example, if we move one vertex so that two polygons end up overlapping each other and then click on Validate All, we get the error shown in the next screenshot. Note that the error type and the affected layer and feature are displayed in Topology Checker Panel. Additionally, since the Show errors option is enabled, the problematic geometry part is highlighted in red on the map:

Of course, it is also possible to create rules that describe the relationship between features in different layers. For example, the following screenshot shows a point and a polygon layer where the rules state that each point should be inside a polygon and each polygon should contain a point:

Selecting an error from the list of errors in the panel centres the map on the problematic location so that we can start fixing it, for example, by moving the lone point into the empty polygon.
Fixing invalid geometry errors
Sometimes, fixing all errors manually can be a lot of work. Luckily, certain errors can be addressed automatically. For example, the common error of self-intersecting polygons, which cause invalid geometry errors (as illustrated in the following screenshot), is often the result of intersecting polygon nodes or edges. These issues can often be resolved using a buffer tool (for example, Fixed distance buffer in the Processing Toolbox, which we will discuss in more detail in Chapter 4, Spatial Analysis) with the buffer Distance set to 0
. Buffering will, for example, fix the self-intersecting polygon on the left-hand side of the following screenshot by removing the self-intersecting nodes and constructing a valid polygon with a hole (as depicted on the right-hand side):

Another common issue that can be fixed automatically is so-called sliver polygons. These are small, and often quite thin, polygons that can be the result of spatial processes such as intersection operations. To get rid of these sliver polygons, we can use the v.clean tool with the Cleaning tool option set to rmarea (meaning "remove area"), which is also available through the Processing Toolbox. In the example shown in this screenshot, the Threshold value of 10000
tells the tool to remove all polygons with an area less than 10,000 square meters by merging them with the neighboring polygon with the longest common boundary:

Note
For a thorough introduction and more details on the Processing Toolbox, refer to Chapter 4, Spatial Analysis.