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

  1. Hello world!
  2. Loading a vector layer
  3. Viewing vector layer attributes
  4. Filtering features
  5. Styling vector layers
    a. Advanced point layer styling
  6. Loading a raster layer
  7. Running Processing tools
  8. Exporting layouts
  9. Creating & editing a new vector layer
  10. Chaining Processing tools
  11. Managing project layers (renaming & removing)
  12. Using expressions to compute new field values
  13. Creating functions to load GeoPackage layers
  14. Writing a Processing script

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


External resources

42 comments
  1. Adrian said:

    Looks very promising

    • Thank you for your feedback! If you have any specific questions or ideas, let me know.

    • Thank you for your feedback! If you have any specific questions or ideas, let me know.

  2. Matt M said:

    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.

  3. Raúl said:

    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!!!

  4. John said:

    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.

  5. Hi Anita,
    A few Raster examples can also be nice.

    • Hi Harel, I’ve added a first raster example now.

  6. Kevin said:

    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.

      • Kevin said:

        Thanks for taking it into consideration. And what you’ve done already has been helpful to me!

  7. wow! am from Kenya and this tutorial is amazing creating an impact in me, looking forward for more

  8. Chris said:

    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.

      • Chris said:

        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 !

  9. DELPORTE Bruno said:

    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

  10. Geert said:

    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.

  11. Edd said:

    Hola muchas gracias, voy a iniciar a revisar, soy neofito en el tema!

  12. driansmith1 said:

    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.

  13. Shyam said:

    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?

  14. Abdiasis said:

    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.

  15. Jamie said:

    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;……)

  16. Venerio said:

    I was looking a free simple tutorial to begin with pyqgis scripting. I like this very much!

  17. kenneth said:

    this is very very helpful can you please make more

  18. Yuhei said:

    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!

  19. Ahmed shihab said:

    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.

  20. Daryl Suen said:

    Definitely on the right track for people who are beginners to programming. I appreciate content like this!!

  21. mbc said:

    Hello, thank you for this very informative resource. Do you know of any reliable/easy way to connect a Python IDE (such as PyScripter or similar) to the Python installation that comes with QGIS? I’ve usually coded in PyScripter with ArcPy, and while I enjoy using PyQGIS the Python console included in QGIS is just very lacking in features. Thanks!

Leave a reply to underdark Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.