Handling Invalid Geometries (QGIS3)

When working with vector data layers, you may encounter geometry errors. These errors often become part of your data after running geoprocessing, digitizing, editing or data conversion. QGIS3 comes with build-in tools and algorithms to detect and fix invalid geometries. This tutorial will show you a typical workflow for handling invalid geometries in your data.

Bemerkung

Different software systems implement different notions of geometry validity. But the leading standard for the geometry model for use in a GIS is the OGC Simple Features Specifications (SFS). In this tutorial, we will use GEOS library to check for geometry validity which uses this standard. This post gives a good overview of common geometry errors as defined by the OGC standard.

Overview of the task

We will work with an admin boundary layer for India and fix a geometry error for a state polygon.

Other skills you will learn

  • Using the Topological Coloring algorithm to apply a coloring scheme to a polygon layer.

Get the data

Datameet provides community-created administrative boundary shapefiles for India.

Download the Datameet Spatial Data Repository [~150 MB] zipfile. The downloaded archive contains multiple folders. Extract the archive and use the files in the States/ folder.

Data Source: [DATAMEET]

Procedure

  1. Browse to the downloaded India-States.zip file in QGIS Browser. Expand it and drag the India-States.shp file to the map canvas.

../../_images/190.png
  1. You will see a new India-States layer loaded in the Layers panel. Go to Processing ‣ Toolbox.

../../_images/260.png
  1. We will attemp to run a processing algorithm on the input layer to demonstrate how invalid geometries can cause problems during geoprocessing operations. Search for and locate the Cartography ‣ Topological coloring algorithm. Double-click to launch it.

../../_images/335.png
  1. In the Topological coloring dialog, select India-States as the Input layer. Keep all other parameters to default and click Run.

../../_images/414.png

Bemerkung

The Topological coloring algorithm implements an algorithm to color a map so that no adjacent polygons have the same color. This is a useful cartography technique and the Four Color Theorem states that 4 colors are enough to achieve this result. There is a graph-theory version of this thorem called Five color theorem. The QGIS algorithm implementation is based on graphs so in practive you will see that complex polygon layers such as this will require upto 5 colors.

  1. As the algorithm runs, you will see a warning displayed in the Log tab. 1 feature in the input layer had invalid geometry and was skipped during the processing. The default setting to handle invalid geometry in the Processing Toolbox is located at Settings ‣ Options ‣ Processing ‣ General ‣ Invalid features filtering and is set to Skip (ignore) features with invalid geometries. This is a good default setting, but if your input is large, you may miss this warning and may not know that an input feature was skipped. You may want to change the value to Stop algorithm execution when a geometry is invalid.

../../_images/513.png
  1. Back in the main QGIS window, you will see a new layer Colored added to the Layers panel. Notice that the new layer is missing a state which had invalid geometry. We now know that this particular state polygon had invalid geometry but we don’t know what was the cause. We can easily find that out. Search for and locate the Vector geometry ‣ Check validity algorithm.

../../_images/613.png
  1. In the Check Validity dialog, select India-States as the Input layer. Select GEOS as the Method. Click Run.

../../_images/712.png
  1. As the algorithm finishes processing, you will see 3 new layers in the Layers panel - Valid output, Invalid output and Error output. The layer Error output contains the locations and description of the geometry errors. Right-click it and select Open Attribute Table.

../../_images/812.png

Bemerkung

The QGIS documentation has a detailed article on Types of error messages and their meanings that explains the causes of all the errors.

  1. You will see that the error message is Ring self-intersection. Select the row and click Zoom map to selected features button. As you zoom in, you will see the root cause of the geometry error.

../../_images/912.png
  1. QGIS comes with a built-in algorithm to fix geometry errors automatically. Search for and locate the Vector geometry ‣ Fix geometries algorithm. Double-click to run it.

../../_images/1018.png
  1. In the Fix Geometries dialog, select India-States as the Input layer and click Run.

../../_images/1121.png
  1. A new layer Fixed Geometries will be added to the Layers panel. At this point, the geometry error is fixed and you can run any processing algorithm on this layer without problems. But we can see that there is still a gap between the adjacent polygons that is unexpected and can cause topological errors down the line. We can fix this too by editing the polygon. Click the Toggle Editing button in the Digitizing Toolbar. Select the Vertex Tool and from the drop-down select Vertex Tool (Current Layer).

../../_images/1219.png
  1. When the vertex tool is active, click on a vertex to select it. You can press Delete key to delete a vertex or drag it to move it. You can move the vertex so that the polygon edge now touches the adjacent polygon.

../../_images/13.gif
  1. Once done, click the Toggle Editing button again and click Save.

../../_images/1415.png
  1. Let’s run the Cartography ‣ Topological coloring algorithm again.

../../_images/1514.png
  1. In the Topological Coloring dialog, make sure you select Fixed Geometries as the Input layer. Click Run.

../../_images/1613.png
  1. You will see the algorithm run without any errors and a new layer Colored will be added to the Layers panel. Note that the algorithm doesn’t color the layer by itself, but works by adding a new column called color_id to each polygon that can be used to assign a unique color that is different than adjacent polygons. Select the Colored layer and click the Open the Layer Styling Panel button.

../../_images/1714.png
  1. Select Categorized renderer and the column color_id as the Value. Click Classify. You will now see the map colored so that adjacent polygons have different colors.

../../_images/1812.png

If you want to give feedback or share your experience with this tutorial, please comment below. (requires GitHub account)