Trick to fix broken polygon geometries in QGIS 3.X

Fellow researchers and open-source GIS enthusiasts,

Welcome to my blog!

I’d like to start with a disclaimer – I may be a researcher of this very area but that doesn’t mean everything I do or write here will work for you, in your own desktop configurations and package versions. I have no responsibility if you lose data or mess up your installation. I also do not authorize any copies of my content.

Today, I am showing you 1) one thing to look out for when drawing a polygon, and 2) how to fix stubborn invalid polygon geometries.

Most of the time, we can just use the functionality created to fix broken geometries, called “Fix geometries” but if it doesn’t work, there is a workaround for that, too!

First, I will create a purposefully invalid geometry to apply the fix. Then, I will show you two ways to fix it. In one of them, I simply apply the “Fix geometries” native tool of QGIS. The second option I will show here is using the buffer tool. In this simple trick, I will show you how to create an infinitesimal buffer around my broken geometry, which ends up fixing it but has its perks as well.

My friend Iporã Possanti, who also writes about GIS was the one that taught me this trick on QGIS 2, back in the old, pre-pandemic days, saving me of a headache because of a stubborn polygon!

For the examples here shown, I will be using QGIS 3.16, the current stable version.

How to draw a polygon in a way you’ll end up with broken geometries

Create a new vector layer and Toggle Editing

QGIS create new vector

With the edit mode turned on, click in “Add Polygon Feature”.

QGIS add polygon vector

Start drawing your polygon.

QGIS add polygon vector

Oops! You double-clicked on a vertex. No problem, right? RIGHT?

QGIS add polygon vector

Let’s say that you ignored this little mistake and continued to draw the polygon. Been there, done that.

QGIS add polygon vector

Here is your polygon!

Now, the next step is to generate random points for an analysis on the Atlantic Ocean. Using the tool “Random Points inside Polygons” I try to create 50 random points within this area.

QGIS add random points

QGIS add random points

But when I click Run, I get an error:

Feature (-28) from “bad_polygon” has invalid geometry. Please fix the geometry or change the Processing setting to the “Ignore invalid input features” option.
Execution failed after 0.02 seconds

My knee-jerk reaction would be to check “Ignore invalid input features”, but then, the algorithm will ignore the only feature there is. There are better solutions to this issue.

OK, how to avoid this error, then?

When generating your polygon, avoid creating intersections between the lines. This is an intersection:

QGIS polygon intersection

This is (mainly) what causes issues on the geometries.

And how to fix my geometry with this error?

Well, I have two options for you. Tentatively, you should start with the regular algorithm “Fix geometries”. If that does not solve the problem, or if you want to skip to the other solution, the alternative is using the “Buffer”. Both are described in detail below.

Using the QGIS tool “Fix geometries”

QGIS fix geometries

QGIS fix geometries

Quick and painless, “Fix geometries” will fix most of the geometry issues you may encounter. When applied to my previously created Polygon, it generates this:

QGIS fix geometries result

It is still a single polygon, as can be seen on the attribute table:

QGIS fix geometries result

But, when I Toggle Editing and try to edit the vertices, a new vertex was created on the intersection between the lines. This vertex did not previously exist.

QGIS fix geometries result

After doing this, I was able to run “Random Points inside polygons” without issues.

QGIS fix geometries result

Buffer with distance=0

You can also solve this issue using the native tool “Buffer”.

QGIS buffer tool

The only property that I altered was the distance, that I filled with “0” degrees.

QGIS buffer tool

After clicking “Run”, I got this:

QGIS fixed geometry

The second part of the polygon, which was on the right side of the intersection, was eliminated by this pipeline.

Then, I could run “Random Points inside polygons”, also without issues.

QGIS fixed geometry

The use of the “Buffer” algorithm can fundamentally alter the geometry of the Polygon but is a valid alternative to “Fix geometries”. In my experience, using a buffer with 0m distance always worked to fix even the most stubborn geometries.

Luísa Vieira Lucchese
Luísa Vieira Lucchese
PhD Student

MSc., Eng.

Related