Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By : Joel Lawhead
Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By: Joel Lawhead

Overview of this book

QGIS is a desktop geographic information system that facilitates data viewing, editing, and analysis. Paired with the most efficient scripting language—Python, we can write effective scripts that extend the core functionality of QGIS. Based on version QGIS 2.18, this book will teach you how to write Python code that works with spatial data to automate geoprocessing tasks in QGIS. It will cover topics such as querying and editing vector data and using raster data. You will also learn to create, edit, and optimize a vector layer for faster queries, reproject a vector layer, reduce the number of vertices in a vector layer without losing critical data, and convert a raster to a vector. Following this, you will work through recipes that will help you compose static maps, create heavily customized maps, and add specialized labels and annotations. As well as this, we’ll also share a few tips and tricks based on different aspects of QGIS.
Table of Contents (16 chapters)
QGIS Python Programming Cookbook - Second Edition
About the Author
About the Reviewer
Customer Feedback

Installing QGIS 2.18 for development

QGIS has a set of Python modules and libraries that can be accessed from the Python console within it. However, they can also be accessed from outside QGIS to write standalone applications. First, you must make sure that PyQGIS is installed on your platform, then set up some required environment variables.

In this recipe, we will walk you through the additional steps required beyond the normal installation process to prepare your system for development. The steps for each platform are provided, which also include the different styles of Linux package managers. The version of QGIS in your Linux package manager may be older. You can find additional detailed installation instructions at

Getting ready

QGIS uses different installation methods for Windows, GNU/Linux, and Mac OS X. The Windows installer installs everything you need for Python development, including Python itself.

However, on both Linux distributions and Mac OS X, you may need to manually install the Python modules to complete the installation process. On Mac OS X, you can download installers for some commonly used Python modules with QGIS; refer to

How to do it...

On Linux, you have an option to either compile from the source or just specify the Python interface to be installed through your package manager.

Installing PyQGIS using the Debian package manager

  1. For Linux distributions based on the Debian Linux package manager, which includes Ubuntu and Debian, use the following command in a shell:

      sudo apt-get update
  2. Next, install the QGIS, PyQGIS, and QGIS GRASS plugins:

      sudo apt-get install qgis python-qgis qgis-plugin-grass

Installing PyQGIS using the RPM package manager

  1. For Linux distributions based on the Red Hat Package Manager (RPM), first update the package manager as follows:

      sudo yum update
  2. Then, install the packages for the QGIS, PyQGIS, and QGIS GRASS plugins:

     sudo yum install qgis qgis-python qgis-grass

Setting the environment variables

Now we need to set PYTHONPATH to the PyQGIS directory. At the same time, append the path to this directory to the PATH variable so that you can use the PyQGIS modules with an external IDE.

Setting the environment variables on Windows

  1. Set the PYTHONPATH variable in a command prompt to the bin directory of the QGIS installation:

      set PYTHONPATH="C:\Program Files\QGIS 2.18\bin"
  2. Next, append QGIS's bin directories to the system's PATH variable:

      set PATH="C:\Program Files\QGIS 2.18\bin";"C:\Program Files\QGIS

Setting environment variables on Linux

  1. Set the PYTHONPATH variable in a command prompt to the bin directory of the QGIS installation:

      export PYTHONPATH=/usr/share/qgis/python
  2. Now append the QGIS shared library directory to a runtime search path. Note that this location can vary depending on your system configuration:

      export LD_LIBRARY_PATH=/usr/share/qgis/python

How it works...

The QGIS installation process and package managers set up the Python module's configuration in a way that it becomes internal to QGIS. When you use the Python console inside QGIS, it knows where all the PyQGIS modules are. However, if you want to use the PyQGIS API outside it by using a system Python installation on either Windows or Linux, it would be necessary for you set some system variables so that Python could find the required PyQGIS modules.

There's more...

This recipe uses the default QGIS paths on each platform. If you aren't sure which PyQGIS path is for your system, you can figure this out from the Python console in QGIS.

Finding the PyQGIS path on Windows

The libraries on Windows, as opposed to other platforms, are stored in a different location. To locate the path, you need to check the current working directory of the Python console:

  1. Start QGIS.

  2. Select Python Console from the Plugins menu, which will appear in the lower-right corner of the QGIS application window, as shown in the following screenshot. You can also use the keyboard shortcut Control + Alt + P to do this:

  3. Use the os module to get the current working directory:

            import os 
  4. Verify that the current working directory of the Python console is returned.

Finding the location of the QGIS Python installation on other platforms

Perform the following steps to find the path needed for this recipe on all the other platforms:

  1. Start QGIS.

  2. Start the QGIS Python console.

  3. Use the sys module to locate the PyQGIS path

            import sys 
  4. Python will return a list of paths.

  5. Find the path that ends in /python, which is the location of the Python installation used by QGIS.