How to create custom geometries based on vector shapes to use on the map legend or symbology on QGIS

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.

This week, I am showing you how to create custom geometries to enhance your legends on QGIS Print Layout. Specifically, how to create these geometries based on vector files that you already have.

The example I will provide is how to create the Legend Patch Shape for the state of Rio Grande do Sul, in Brazil.

For this tutorial, I am using QGIS 3.20 “Odense”, although, as far as I read, any QGIS from version 3.14 has the Patch Shape editor and can be used.

First step: simplify your original vector. The more, the better.

Remember it’s going to look tiny in your symbology box, so no need to have overly heavy and complicated geometries – we are aiming for an eye-catching symbol, your map itself should provide the reader with the extra details.

What I am doing here regards polygon simplification is not far off from what I showed in this post, so you will see a couple of references to it.

In my example: first, I open the shapefile for the Rio Grande do Sul (RS) state (by IBGE).

Original shapefile of RS

If your file has a MultiPolygon type or is made of several polygons, I suggest you generate a version of it with only one polygon. If you don’t know how to do that, I explained it step-by-step on this post.

Now, let’s simplify this polygon. To do this, use the tool “Simplify”:

QGIS Simplify

Choose the distance tolerance to use on the simplification. The larger the tolerance, the more simplified your polygon will become.

QGIS Simplify

Save it and Run.

This is the resulting geometry:

QGIS Simplify RS state

It kept the general shape of the state with none of the details.

Open the simplified polygon file

Open your newly created file on QGIS, if that did not happen automatically.

Now, what we want to do is to take the geometry coordinates and put them in a format which is compatible to the Legend Patch Shape editor. The format which we seek is ((LONG LAT, LONG LAT, LONG LAT)), in which each pair of longitude and latitude is one polygon vertex.

To transform my geometry into the needed format, I will export is as a .csv.

QGIS save feature as

Choose “Comma Separated Value [CSV]”. I don’t need the metadata for the next part, so I unmarked it. I chose the “Geometry type” as “Polygon” and marked the “Extent” checkbox.

QGIS save feature as

In the Layer Options, I selected “Geometry: AS_WKT”, and saved the file.

QGIS save feature as

Open the csv in a text editor

Now, I will open the CSV file generated in a text editor. Opening it on Vim, I see this:

WKT,CD_UF,NM_UF,SIGLA_UF,NM_REGIAO
"POLYGON ((-56.323271271 -28.924725248,-54.809787122 -27.530047424,-53.373410477 -27.091175734,-51.630409521 -27.489141496,-50.624951476 -28.392588311,-49.691351788 -28.620891398,-50.177509124 -29.248521292
[...]
-55.577531011 -30.833411138,-56.011123407 -31.081577829,-56.808075013 -30.103240694,-57.649742824 -30.189320897,-56.323271271 -28.924725248))","43",Rio Grande do Sul,RS,Sul

Minimize this window to use it later.

Open the QGIS Print Layout

Open the Print Layout and Add a map on it. Then, add a Legend.

QGIS print layout

On the right panel, on Legend Items, uncheck “auto-update” and double-click any of the legend items.

QGIS print layout

In the section “Patch”, click the rectangle besides “Shape”

QGIS print layout

The shape will be filled with

Polygon ((0 5, 10 5, 10 0, 0 0, 0 5))

Add the coordinates of your vector shape to the Legend Patch Shape editor

Copy the coordinates from your CSV file and paste them inside the “(())”. Check the “Preserve Aspect Ratio” checkbox.

QGIS legend patch shape

Zoom in the legend and check if your custom symbol looks like expected.

QGIS legend patch shape

Mine does!

Save for future use

To use and re-use your geometry in future legends and symbology, save the Patch Shape you just created.

Enter the Patch Shape editor again

QGIS legend patch shape editor

QGIS legend patch shape editor

Scroll down and click “Save Legend Patch Shape”

QGIS legend patch shape editor save patch

Write some useful tags to your geometry and save it.

QGIS legend patch shape editor tags

Now you will be able to use it in any QGIS project.

QGIS legend patch shape editor tags

Luísa Vieira Lucchese
Luísa Vieira Lucchese
Postdoctoral Researcher

Postdoc at University of Pittsburgh

Related