After getting accustomed with vector and raster layer basics, let’s get to a more advanced topic and see how we can use Processing in PyQGIS to perform different spatial analysis tasks.

If you’ve ever looked into the Processing history (Processing | History), you may have noticed that it keeps records of all the Processing tools you’ve executed from the toolbox, including the corresponding code snippet starting with We can copy this code to use it in the Python console. For example, this will run the native buffer algorithm:

uri = "D:/Documents/Geodata/NaturalEarthData/Natural_Earth_quick_start/110m_cultural/ne_110m_populated_places.shp"
result ="native:buffer", {'INPUT':uri,'DISTANCE':10,'SEGMENTS':5,'END_CAP_STYLE':0,'JOIN_STYLE':0,'MITER_LIMIT':2,'DISSOLVE':False,'OUTPUT':'memory:'})

So far, this does not yet load the results. The resulting vector layer can be accessed using result['OUTPUT']. To add it to the current project, we use:


This is the first time we use QgsProject.instance() to access the current QGIS project object. This object has many useful functions, including adding and removing map layers.

Let’s take a step back and examine the code! takes two parameters: the first is the algorithm name. The second parameter are algorithm settings in curly brackets {}. So far, we have only used curly brackets for string formatting. Here, we use them to create an input parameter dictionary for the processing algorithm. Let’s have a closer look at the dictionary (dict for short) with some added line breaks for readability:


The dictionary has multiple items and each item consists of a key (e.g. ‘INPUT’) and a value (e.g. the uri variable). Therefore, items are often referred to as key-value pairs.

The input parameters are not the only dictionary in this example. The result returned by is also a dictionary and it shows how we can access a specific item by providing the corresponding key using square brackets: result['OUTPUT']

A couple of items in the input dictionary are not too self-explanatory. What does 'JOIN_STYLE':0 mean, for example? To answer questions like this, we can use the algorithmHelp() function:


The help tells us that available values for the join style are 0: Round, 1: Miter, and 2: Bevel.

Instead of running the algorithm first and loading the results in a second step, there is also a convenience function called runAndLoadResults():

processing.runAndLoadResults("native:buffer", {'INPUT':uri,'DISTANCE':10,'SEGMENTS':5,'END_CAP_STYLE':0,'JOIN_STYLE':0,'MITER_LIMIT':2,'DISSOLVE':False,'OUTPUT':'memory:'})

These are the basics of using Processing algorithms in PyQGIS. You also saw how to create dictionaries and access dictionary items.

PyQGIS 101 is a work in progress. I’d appreciate any feedback, particularly from beginners!

%d bloggers like this: