Python: How to Loop Functions in QGIS


Shortly after I spent hours in Processing figuring out how to map coordinates onto an image, someone mentioned that what I REALLY want to be using is QGIS.

QGIS is to ArcGIS as Blender is to Maya and Gimp is to Photoshop. It’s open-source. Completely free for us broke folks. And it speaks Python.

So by way of introduction, I’m going to walk through a few basic commands to get me going.

Add a Base Map

You may notice that upon opening QGIS, you will be faced with a completely blank screen. Don’t panic. You’ve just got to add in a map background.


The Ireland QGIS User Group Blog actually posted the code to get ESRI’s ArcGIS Online World Imagery to make it really easy:


Once that’s pasted in, hit return, and you should see this:


Add Some Data

Not so long ago, I posted a script to scrape Airbnb data into a csv file. I haven’t been checking Airbnb’s HTML to make sure it hasn’t gotten screwier since I wrote said scraper. Because I’m weird, but not that weird. I still have the last csv file I scraped, so I’m using that. I was going to upload it here, but apparently WordPress doesn’t support csv uploads (Why WordPress? Are csvs not good enough for you?? And what the heck is a .3g2? Why can I upload that?) for reasons I’m not clear on.

Anyway. Back on topic.


Under Layer, go to Add Layer, and click on Add Delimited Text Layer. Choose your file, make sure you don’t mix up your X and Y values (or your points may end up in Antarctica), and let it fly.

Zooming into your layer, you should see something like this:


Now we’ve got a something to work with.

Throw in Some Python

Say I wanted to buffer those locations. Digital Geography has an awesome tutorial on how to do this which I’m going to summarize here.

First, you have to import the processing library to get access to the fixeddistancebuffer function.

import processing

Now, to keep life easy, we’re going to assign the active layer to a variable called layer.

layer = iface.activeLayer()

Here’s the template for the actual function:

processing.runalg(“qgis:fixeddistancebuffer”, LayerToBuffer, Distance, Segment, Dissolve True/False,”C:/Filepath.shp“)

Since our active layer is stored in the variable layer–we can just type .name() and trust QGIS to fill in the name. For example:


That will save your buffer geometry to a shape file called TestBuffer. If you’re following along at home, you may have noticed that no shapes appeared on the map as a result. Don’t start verbally abusing QGIS yet. There’s two more lines:

newLayer=QgsVectorLayer('C:/Users/Desktop/TestBuffer.shp', "Buffer Layer", "ogr")

That one actually LOADS the layer, which is important, and assigns it to a variable which can be used to add it to the Map Layer Registry.


Presto. Buffer layer.

Run a Loop

Say you want multiple buffer layers at different distances. You can push the lines that create and load the shapefile into a while loop. Then assign the distance value to a variable that increases with each iteration. Like so:

while x < .01:
fp="C:/Users/Desktop/Buffer" + str(x) +".shp"
processing.runalg("qgis:fixeddistancebuffer",, x, 10, False, fp)
newlayer=QgsVectorLayer(fp, "Buffer Layer", "ogr")

You can even use the buffer distance as part of the file nameβ€”if you convert the float value to a string using str().

The result should look something like this:


Which you can then manipulate to get some animation going like the gif in my header or like this one.


More QGIS to come.


8 Replies to “Python: How to Loop Functions in QGIS”

  1. Nice post (… and blog)! You may also have a look at one similar post I wrote about the buffering of vectors. Even if thought for the creation of buffer from fields (it could be easily adapted to your case), it explains how to get buffers without recurring to the processing library: in this way, the execution time of the whole process will be shorter, mainly when dealing with many features.

    Liked by 1 person

Leave a Reply to Lucas Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: