This tutorial aims to help GIS users to get started with Python programming for QGIS 3. In contrast to many tutorials out there, the idea is to not assume any previous programming knowledge.
If you found this tutorial on your own, you probably won’t require much external motivation to give this programming thing a go. (If you do, check out “Benefits of Learning Python”.) Python is a great versatile programming language that is widely used in the GIS world and beyond. Knowing how to program will enable you to be more efficient by automating workflows and making your work reproducible.
Unlike many introductions to Python, this one does not start with an installation and setup guide. Why? Because if you have installed QGIS, you already have a working installation of Python on your system. All you need to get started is to launch QGIS and open the Python console (Plugins | Python console).
If you are looking for a more classical introduction, I strongly recommend Gary Sherman’s PyQGIS Programmer’s Guide.
Contents
- Hello world!
- Loading a vector layer
- Viewing vector layer attributes
- Filtering features
- Styling vector layers
a. Advanced point layer styling - Loading a raster layer
- Running Processing tools
- Exporting layouts
- Creating & editing a new vector layer
- Chaining Processing tools
- Managing project layers (renaming & removing)
- Using expressions to compute new field values
- Creating functions to load GeoPackage layers
- Writing a Processing script
This is a work in progress. I’d appreciate any feedback, particularly from beginners!
Looks very promising
Thank you for your feedback!
More, more, more…
Thank you for your feedback! If you have any specific questions or ideas, let me know.
A great start and really useful for a total beginner like me. More please!
Thank you for your feedback! If you have any specific questions or ideas, let me know.
This is great!
Just a few ideas…
Do one for writing an entire script that loads a few layers and does some sort of processing on it. The individual tasks you complete are an important foundation, this adds even more practicality.
And then maybe write one explaining commonly used structures/logic of scripts, such as looping through features in a layer, creating new calculated columns. This enables learners to apply pyqgis to numerous problems.
Lastly I’d love to see one for writing a script and then setting up a UI/Dialog Box for user input and how to add it to the processing toolbox.
Your work is awesome!
Thank you Matt! More loops are certainly on my todo list, as is using Processing tools from code. Creating a Processing script from scratch will probably be a bit further down the line.
I was looking for some like this just to start working with pyqgis. I like this who much!!!!
Also I agree with the third idea by Matt…
Thank you for this work!!!
Thank you for your feedback Raúl!
Anita, Pretty much anything you do I will check out. Just try an remember, most of us are not even in the same realm of brilliance as you. Keep us dullards in mind.
Thank you for your feedback John! Please let me know in the comments if anything remains unclear.
Anita, super good idea!
Thank you for your feedback Paul!
Hi Anita,
A few Raster examples can also be nice.
Hi Harel, I’ve added a first raster example now.
Hi Anita,
Will you cover using python to change layout properties if I were to automate map creation or something of that sort?
These posts have been helpful for me as I begin learning PyQGIS. Thanks again!
Hi Kevin,
Thank you for the suggestion. It’s probably an advanced topic but I’ll keep it in mind.
Thanks for taking it into consideration. And what you’ve done already has been helpful to me!
wow! am from Kenya and this tutorial is amazing creating an impact in me, looking forward for more
Thank you for your feedback!
Such a great initiative ! Thanks a lot.
I’m learning programming and one of my “goal” is to implement in a way or another median filter for rasters in QGIS.
Numpy can already do it so i guess an easy way to achieve my objective would be to “call” numpy within QGIS.
Do you think it’s achievable in a reasonable time for a true beginner ?
I think the part of using numpy should be rather straightforward. I have some Python2 numpy code that I could adapt for this tutorial. There might be a simpler way to achieve a median filter though but I don’t have a solution ready.
I’m not looking for a solution ready but for a practical objective to motivate me to learn code. =)
So if you can already help with “how to use numpy within QGIS” that would be great already !
Bonjour,
It’s now several weeks that I attend to learn QGis and PyQgis. Up to now, the tutorials were not efficient for different reasons : too short, too quick, too hard, and/or too olds (methods that don’t work any more with the latest version of QGis)… for beginners like me. Your course is very practical. And the progression of difficulties is very good.
I do like it
Thank you for your feedback Bruno!
Hi Anita,
I’m a beginner in PyQgis and this is a great tutorial, very useful! Thank you for posting it. I’ll be on the lookout for more! :-) I’m especially interested in how to automate map making (using a Layout template and exporting it to a PDF file, for example).
Hi Geert, an example for exporting a layout to PDF is published now.
Hola muchas gracias, voy a iniciar a revisar, soy neofito en el tema!
Am I correct in saying all the tools in the toolboxs are written in Python? GRASS and GDAL ?
In QGIS3, a significant part of the Processing toolbox core has been ported to C++. In the case of GRASS and GDAL, the Python code parts are mostly “plumbing” and the actual algorithm intelligence is implemented in the GRASS or GDAL core in C++ as well.
Thank you very much for the introduction course. It is very helpful. If I am looking for a more advanced course on the same topic, where can I find it?
The main source for more advanced material (not tutorial style though) is https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/index.html
Thanks for great and friendly explanation. I have been studying GIS for quite sometime. i would like to see GIS project from scratch that shows how to create shapefiles from other Shapefiles and how to to collect GPS data and digitize it. Also vector and raster combined.
Thanks for your great turtorial ,following your steps,I have done a IDW process with a cvslayer programmaticly ,but it is gray. I can colorize the IDW result layer by specifying classified value ranges and corrsponding color in the symbology tab of Qgis layer properties form.Hower I am pretty new with both GIS and Python , I do not know how to colorize the result raster layer by python program. Can you do me a favor ,thanks.(set point color by its value,for example: 0<=value<=50-red;50<=value<=100-green;100<=value<=150-yellow;……)
I was looking a free simple tutorial to begin with pyqgis scripting. I like this very much!
this is very very helpful can you please make more
Hi underdark, Thank you very much for the wonderful tutorial! I am from Japan and first I looked for some tutorials like this in Japanese but I could not find any. So lucky to get to know this tutorial. I love it!
Looks and sound good, i cant wait to start the course to learn about pyQGIS. My question is are operations preformed on python console of qgis, are the codes and syntaxes the same as ArcMap python console?!
Yes the code is meant to be executed in the QGIS python console. And no, the Syntax is not the same as ArcPy.
Definitely on the right track for people who are beginners to programming. I appreciate content like this!!