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.
>>> my_layer= iface.activeLayer()