[Tutorial] Getting in the Pit with Python in QGIS

[Tutorial] Getting in the Pit with Python in QGIS
5 ( 1 vote )

While it is perfectly possible to create data, perform geospatial analyses, mapping products, etc. just by
using the tools offered by any GIS software suite graphic user interface (GUI), automation will always
enable the user to reach a higher level of sophistication and productivity. It would probably be
somewhat harsh to state that serious GIS work is not possible without programming or scripting, but
the fact is that serious GIS work will always include a healthy dose of it.
This is a beginner-friendly introduction to PyQGIS meant to be hands-on, easy to follow, and seeking
to promote interest and develop user confidence among non-programmers by building and executing a
simple PyQGIS script.
Python is a programming language that runs on all operating systems, it is free and open source.
PyQGIS is the QGIS flavor of Python and the chosen scripting language for this free, open source GIS
QGIS works with Python 2.x. Depending on the operating system, Python may be already installed.
In order to work with PyQGIS, it is necessary to set up the environment, and the requirements vary
depending on the platform. The good news is that the integrated Python Console under the Plugins
menu in QGIS 2.18.7 will do the work without having to install any additional interpreters or editors.

Step 1. Opening and exploring the Python Console:

To open the Python Console, use the menu “Plugins” then Click on “Python Console”


QGIS is written in the C++ language, so a means to pass commands in Python is needed. For this
purpose, QGIS Desktop provides the iface module which loads by default. Iface hosts many methods
which permit to enter commands at the command prompt (the bottom left window in the figure below)
emulating any possible command, a combination thereof, or better that can be found in the GUI.

Add a vector layer from the GUI, select it in the Layers Panel and, at the command prompt, test some
of these functions (press Enter after entering the command):

>>> iface.zoomFull()
>>> iface.zoomToPrevious()
>>> my_layer= iface.activeLayer()
>>> iface.showAttributeTable(my_layer)


Prev1 of 4

Check Also

How to convert Excel to GIS data in QGIS using Spreadsheet Layers plugin

How to convert Excel to GIS data in QGIS using Spreadsheet Layers plugin

How to convert Excel to GIS data in QGIS using Spreadsheet Layers plugin 5 ( ...


  1. This is great! Thanks. One question… I am currently trying to do something similar with the states of Brazil. The issue I have is that in the name column I have letters with accents eg. São Paulo and Pará. How would I go about inserting lines that declares/updates the encoding?

    Many thanks.

  2. Is there something missing in the 3rd line?
    >>> my_layer= iface.activeLayer()

Leave a Reply

Your email address will not be published. Required fields are marked *